Skip to content

Commit 1459bf6

Browse files
committed
fix test error on windows
1 parent 02c98f1 commit 1459bf6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

detect_windows.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ func tryEnableVTP(enable bool) bool {
5555

5656
debugf("True-Color by enable VirtualTerminalProcessing on windows")
5757

58-
// load related windows dll
59-
kernel32 = syscall.NewLazyDLL("kernel32.dll")
60-
61-
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
62-
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
63-
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
58+
initKernel32Proc()
6459

6560
// enable colors on windows terminal
6661
if tryEnableOnCONOUT() {
@@ -70,6 +65,19 @@ func tryEnableVTP(enable bool) bool {
7065
return tryEnableOnStdout()
7166
}
7267

68+
func initKernel32Proc() {
69+
if kernel32 != nil {
70+
return
71+
}
72+
73+
// load related windows dll
74+
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
75+
kernel32 = syscall.NewLazyDLL("kernel32.dll")
76+
77+
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
78+
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
79+
}
80+
7381
func tryEnableOnCONOUT() bool {
7482
outHandle, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0)
7583
if err != nil {
@@ -213,6 +221,8 @@ func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
213221

214222
// IsTty returns true if the given file descriptor is a terminal.
215223
func IsTty(fd uintptr) bool {
224+
initKernel32Proc()
225+
216226
var st uint32
217227
r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0)
218228
return r != 0 && e == 0
@@ -225,6 +235,8 @@ func IsTty(fd uintptr) bool {
225235
// fd := uintptr(syscall.Stdout) // for windows
226236
// IsTerminal(fd)
227237
func IsTerminal(fd uintptr) bool {
238+
initKernel32Proc()
239+
228240
var st uint32
229241
r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0)
230242
return r != 0 && e == 0

0 commit comments

Comments
 (0)