-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
termenv_windows.go
89 lines (73 loc) · 2.04 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//go:build windows
// +build windows
package termenv
import (
"os"
"strconv"
"golang.org/x/sys/windows"
)
func colorProfile() Profile {
if os.Getenv("ConEmuANSI") == "ON" {
return TrueColor
}
winVersion, _, buildNumber := windows.RtlGetNtVersionNumbers()
if buildNumber < 10586 || winVersion < 10 {
// No ANSI support before Windows 10 build 10586.
if os.Getenv("ANSICON") != "" {
conVersion := os.Getenv("ANSICON_VER")
cv, err := strconv.ParseInt(conVersion, 10, 64)
if err != nil || cv < 181 {
// No 8 bit color support before v1.81 release.
return ANSI
}
return ANSI256
}
return Ascii
}
if buildNumber < 14931 {
// No true color support before build 14931.
return ANSI256
}
return TrueColor
}
func foregroundColor() Color {
// default gray
return ANSIColor(7)
}
func backgroundColor() Color {
// default black
return ANSIColor(0)
}
// EnableWindowsANSIConsole 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)
}