Skip to content

Commit b456762

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

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
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=

internal/windowsansi/winansi.go renamed to internal/ansi/ansi_windows.go

Lines changed: 11 additions & 8 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"
@@ -10,8 +10,8 @@ import (
1010

1111
// Enable activates ANSI support on Windows terminals by setting the
1212
// ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.
13-
// It fails silently if the output is not a console.
14-
func Enable() {
13+
// Returns an error if the output is not a console or if setting the mode fails.
14+
func Enable() error {
1515
const enableVirtualTerminalProcessing = 0x0004
1616

1717
// Load kernel32.dll and the necessary procedures dynamically.
@@ -26,16 +26,19 @@ func Enable() {
2626
var mode uint32
2727

2828
// GetConsoleMode fails if not in a real console.
29-
ret, _, _ := procGetConsoleMode.Call(uintptr(handle), uintptr(unsafe.Pointer(&mode)))
29+
ret, _, err := procGetConsoleMode.Call(uintptr(handle), uintptr(unsafe.Pointer(&mode)))
3030
if ret == 0 {
31-
return
31+
return err
3232
}
3333

3434
// Add the virtual terminal processing flag to the current mode.
3535
newMode := mode | enableVirtualTerminalProcessing
3636

3737
// 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))
38+
ret, _, err = procSetConsoleMode.Call(uintptr(handle), uintptr(newMode))
39+
if ret == 0 {
40+
return err
41+
}
42+
43+
return nil
4144
}

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

Lines changed: 4 additions & 4 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"
@@ -20,8 +20,7 @@ func getConsoleMode(handle syscall.Handle) (uint32, error) {
2020
var mode uint32
2121
ret, _, err := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleMode").Call(uintptr(handle), uintptr(unsafe.Pointer(&mode)))
2222
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.
23+
// Note: err may be non-nil even on success, so we must check ret first.
2524
return 0, err
2625
}
2726
return mode, nil
@@ -57,7 +56,8 @@ func TestEnable(t *testing.T) {
5756
}
5857

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

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

0 commit comments

Comments
 (0)