Skip to content
This repository was archived by the owner on Aug 3, 2022. It is now read-only.

Commit d2e7fff

Browse files
committed
Fix colors in Windows
1 parent 3f75b0f commit d2e7fff

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

info/info_windows.go

+41
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,49 @@
1515

1616
package info
1717

18+
import (
19+
"fmt"
20+
"os"
21+
22+
"golang.org/x/sys/windows"
23+
)
24+
1825
// Information about the current system
1926
const (
2027
OsName = "Windows"
2128
OsDir = "windows"
2229
)
30+
31+
// Windows by default ignores ASCII escape codes,
32+
// however we can change this using this.
33+
// Why is this not the default? No idea...
34+
func init() {
35+
// Get a handle to the console
36+
stdOutHandle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
37+
38+
if err != nil {
39+
fmt.Println("Failed to get a handle for standard input, please open an issue, this should work...")
40+
fmt.Println(err)
41+
os.Exit(1)
42+
}
43+
44+
// Get the current console settings
45+
var consoleMode uint32 = 0
46+
err = windows.GetConsoleMode(stdOutHandle, &consoleMode)
47+
48+
if err != nil {
49+
fmt.Println("Failed to get current terminal mode, please open an issue, this should work...")
50+
fmt.Println(err)
51+
os.Exit(1)
52+
}
53+
54+
// Add support for escape codes to those settings
55+
consoleMode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
56+
err = windows.SetConsoleMode(stdOutHandle, consoleMode)
57+
58+
if err != nil {
59+
fmt.Println("Failed to enable ASCII escape sequences, please open an issue, this should work...")
60+
fmt.Println(err)
61+
os.Exit(1)
62+
}
63+
}

0 commit comments

Comments
 (0)