Skip to content

Commit 0fb5b1c

Browse files
committed
refactor: unify windowsansi into ansi
1 parent b22f972 commit 0fb5b1c

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed

ansi_windows.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
package godump
44

5-
import "github.com/goforj/godump/internal/windowsansi"
5+
import (
6+
"log"
7+
8+
"github.com/goforj/godump/internal/ansi"
9+
)
610

711
// init activates ANSI support on Windows terminals by calling the Enable
8-
// function from the internal windowsansi package.
12+
// function from the internal ansi package.
13+
// If enabling ANSI fails (e.g., not running in a real console), it logs
14+
// the error but continues execution, as colors are optional.
915
func init() {
10-
windowsansi.Enable()
11-
}
16+
if err := ansi.Enable(); err != nil {
17+
log.Printf("Warning: Failed to enable ANSI support: %v\n", err)
18+
}
19+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ go 1.18
44

55
require github.com/stretchr/testify v1.10.0
66

7-
require golang.org/x/sys v0.34.0 // indirect
7+
require golang.org/x/sys v0.30.0 // indirect
88

99
require (
1010
github.com/davecgh/go-spew v1.1.1 // indirect
1111
github.com/pmezard/go-difflib v1.0.0 // indirect
12-
golang.org/x/term v0.33.0
12+
golang.org/x/term v0.29.0
1313
gopkg.in/yaml.v3 v3.0.1 // indirect
1414
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
66
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7-
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
8-
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
9-
golang.org/x/term v0.33.0 h1:NuFncQrRcaRvVmgRkvM3j/F00gWIAlcmlB8ACEKmGIg=
10-
golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0=
7+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
8+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
9+
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
10+
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
1111
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1212
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1313
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
//go:build windows
22

3-
package windowsansi
3+
package ansi
44

55
import (
66
"os"
77
"syscall"
88
"unsafe"
99
)
1010

11+
const (
12+
SYS_CALL_FAILURE = 0
13+
enableVirtualTerminalProcessing = 0x0004
14+
)
15+
1116
// Enable activates ANSI support on Windows terminals by setting the
1217
// ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.
13-
// It fails silently if the output is not a console.
14-
func Enable() {
15-
const enableVirtualTerminalProcessing = 0x0004
18+
// Returns an error if the output is not a console or if setting the mode fails.
19+
func Enable() error {
1620

1721
// Load kernel32.dll and the necessary procedures dynamically.
1822
// This avoids a hard dependency and allows the program to run on non-Windows
@@ -26,16 +30,19 @@ func Enable() {
2630
var mode uint32
2731

2832
// GetConsoleMode fails if not in a real console.
29-
ret, _, _ := procGetConsoleMode.Call(uintptr(handle), uintptr(unsafe.Pointer(&mode)))
30-
if ret == 0 {
31-
return
33+
ret, _, err := procGetConsoleMode.Call(uintptr(handle), uintptr(unsafe.Pointer(&mode)))
34+
if ret == SYS_CALL_FAILURE {
35+
return err
3236
}
3337

3438
// Add the virtual terminal processing flag to the current mode.
3539
newMode := mode | enableVirtualTerminalProcessing
3640

3741
// Try to set the new console mode.
38-
// If this call fails, we also silently continue. The result will be
39-
// that colors are not rendered, which is an acceptable fallback.
40-
procSetConsoleMode.Call(uintptr(handle), uintptr(newMode))
42+
ret, _, err = procSetConsoleMode.Call(uintptr(handle), uintptr(newMode))
43+
if ret == SYS_CALL_FAILURE {
44+
return err
45+
}
46+
47+
return nil
4148
}

internal/windowsansi/winansi_test.go renamed to internal/ansi/ansi_windows_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//go:build windows
22

3-
package windowsansi
3+
package ansi
44

55
import (
66
"os"
@@ -11,17 +11,12 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14-
const (
15-
enableVirtualTerminalProcessing = 0x0004
16-
)
17-
1814
// getConsoleMode is a helper to retrieve the current console mode for a given handle.
1915
func getConsoleMode(handle syscall.Handle) (uint32, error) {
2016
var mode uint32
2117
ret, _, err := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleMode").Call(uintptr(handle), uintptr(unsafe.Pointer(&mode)))
22-
if ret == 0 {
23-
// In Go 1.16+, err is not nil on failure. On older versions, it might be.
24-
// So we return the error from the syscall call directly.
18+
if ret == SYS_CALL_FAILURE {
19+
// Note: err may be non-nil even on success, so we must check ret first.
2520
return 0, err
2621
}
2722
return mode, nil
@@ -30,7 +25,7 @@ func getConsoleMode(handle syscall.Handle) (uint32, error) {
3025
// setConsoleMode is a helper to set the console mode for a given handle.
3126
func setConsoleMode(handle syscall.Handle, mode uint32) error {
3227
ret, _, err := syscall.NewLazyDLL("kernel32.dll").NewProc("SetConsoleMode").Call(uintptr(handle), uintptr(mode))
33-
if ret == 0 {
28+
if ret == SYS_CALL_FAILURE {
3429
return err
3530
}
3631
return nil
@@ -57,7 +52,8 @@ func TestEnable(t *testing.T) {
5752
}
5853

5954
// Run the function we want to test.
60-
Enable()
55+
err = Enable()
56+
require.NoError(t, err, "Enable() should not return an error in a real console")
6157

6258
// After running Enable(), check the console mode again to see if the flag was set.
6359
newMode, err := getConsoleMode(handle)

0 commit comments

Comments
 (0)