| 1 | //go:build windows |
| 2 | |
| 3 | package cli |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | |
| 8 | "golang.org/x/sys/windows" |
| 9 | ) |
| 10 | |
| 11 | // ansiConsoleReady reports whether stdout can render SGR sequences, enabling |
| 12 | // VT processing when the console has it off (legacy conhost prints the escapes |
| 13 | // literally instead — see #3242). A non-console stdout is left to colorprofile. |
| 14 | func ansiConsoleReady() bool { |
| 15 | enableVirtualTerminal(os.Stderr) |
| 16 | return enableVirtualTerminal(os.Stdout) |
| 17 | } |
| 18 | |
| 19 | func enableVirtualTerminal(f *os.File) bool { |
| 20 | h := windows.Handle(f.Fd()) |
| 21 | var mode uint32 |
| 22 | if err := windows.GetConsoleMode(h, &mode); err != nil { |
| 23 | return true |
| 24 | } |
| 25 | if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 { |
| 26 | return true |
| 27 | } |
| 28 | return windows.SetConsoleMode(h, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == nil |
| 29 | } |
| 30 |