-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterminal_windows.go
More file actions
52 lines (43 loc) · 1.05 KB
/
terminal_windows.go
File metadata and controls
52 lines (43 loc) · 1.05 KB
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
package devlog
import (
"io"
"os"
"golang.org/x/sys/windows"
)
// IsColorTerminal checks if the given writer is a terminal with ANSI color support.
// It respects [NO_COLOR], [FORCE_COLOR] and TERM=dumb environment variables.
//
// [NO_COLOR]: https://no-color.org/
// [FORCE_COLOR]: https://force-color.org/
func IsColorTerminal(output io.Writer) bool {
if os.Getenv("NO_COLOR") != "" {
return false
}
if os.Getenv("FORCE_COLOR") != "" {
return true
}
if os.Getenv("TERM") == "dumb" {
return false
}
if output == nil {
return false
}
file, isFile := output.(*os.File)
if !isFile {
return false
}
console := windows.Handle(file.Fd())
var consoleMode uint32
if err := windows.GetConsoleMode(console, &consoleMode); err != nil {
return false
}
var wantedMode uint32 = windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
if (consoleMode & wantedMode) == wantedMode {
return true
}
consoleMode |= wantedMode
if err := windows.SetConsoleMode(console, consoleMode); err != nil {
return false
}
return true
}