/
termenv_windows.go
59 lines (48 loc) · 1.44 KB
/
termenv_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//go:build windows
// +build windows
package termenv
import (
"golang.org/x/sys/windows"
)
func colorProfile() Profile {
return TrueColor
}
func foregroundColor() Color {
// default gray
return ANSIColor(7)
}
func backgroundColor() Color {
// default black
return ANSIColor(0)
}
// EnableWindowsANSI enables virtual terminal processing on Windows platforms.
// This allows the use of ANSI escape sequences in Windows console applications.
// Ensure this gets called before anything gets rendered with termenv.
// Returns the original console mode and an error if one occurred.
func EnableWindowsANSIConsole() (uint32, error) {
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err != nil {
return 0, err
}
var mode uint32
err = windows.GetConsoleMode(handle, &mode)
if err != nil {
return 0, err
}
// See https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {
vtpmode := mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
if err := windows.SetConsoleMode(handle, vtpmode); err != nil {
return 0, err
}
}
return mode, nil
}
// RestoreWindowsConsole restores the console mode to a previous state.
func RestoreWindowsConsole(mode uint32) error {
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err != nil {
return err
}
return windows.SetConsoleMode(handle, mode)
}