-
Notifications
You must be signed in to change notification settings - Fork 93
/
util_windows.go
60 lines (49 loc) · 1.32 KB
/
util_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
//go:build windows
package main
import (
"log"
"os"
"unsafe"
"golang.org/x/sys/windows"
"github.com/mattn/go-colorable"
)
const (
ENABLE_QUICK_EDIT_MODE = uint32(0x0040)
ES_SYSTEM_REQUIRED = uint32(0x00000001)
ES_CONTINUOUS = uint32(0x80000000)
)
var (
kernel32 = windows.NewLazyDLL("kernel32.dll")
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
procSetThreadExecutionState = kernel32.NewProc("SetThreadExecutionState")
previousMode = uint32(0)
)
func Setup() {
const threadState = ES_CONTINUOUS | ES_SYSTEM_REQUIRED
procSetThreadExecutionState.Call(uintptr(threadState))
disableQuickEditMode()
colorable.EnableColorsStdout(nil)
log.SetOutput(colorable.NewColorableStderr())
}
func Exit(code int) {
procSetThreadExecutionState.Call(uintptr(ES_CONTINUOUS))
resetConsoleMode()
os.Exit(code)
}
func disableQuickEditMode() {
h := os.Stdin.Fd()
if r, _, _ := procGetConsoleMode.Call(h, uintptr(unsafe.Pointer(&previousMode))); r == 0 {
previousMode = 0
return
}
mode := previousMode & ^ENABLE_QUICK_EDIT_MODE
procSetConsoleMode.Call(h, uintptr(mode))
}
func resetConsoleMode() {
if previousMode == 0 {
return
}
h := os.Stdin.Fd()
procSetConsoleMode.Call(h, uintptr(previousMode))
}