From 1459bf6721d7922eb65ecf2eff7de95819a35fa2 Mon Sep 17 00:00:00 2001 From: inhere Date: Tue, 6 Apr 2021 19:18:22 +0800 Subject: [PATCH] fix test error on windows --- detect_windows.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/detect_windows.go b/detect_windows.go index 008147e..3cfb487 100644 --- a/detect_windows.go +++ b/detect_windows.go @@ -55,12 +55,7 @@ func tryEnableVTP(enable bool) bool { debugf("True-Color by enable VirtualTerminalProcessing on windows") - // load related windows dll - kernel32 = syscall.NewLazyDLL("kernel32.dll") - - // https://docs.microsoft.com/en-us/windows/console/setconsolemode - procGetConsoleMode = kernel32.NewProc("GetConsoleMode") - procSetConsoleMode = kernel32.NewProc("SetConsoleMode") + initKernel32Proc() // enable colors on windows terminal if tryEnableOnCONOUT() { @@ -70,6 +65,19 @@ func tryEnableVTP(enable bool) bool { return tryEnableOnStdout() } +func initKernel32Proc() { + if kernel32 != nil { + return + } + + // load related windows dll + // https://docs.microsoft.com/en-us/windows/console/setconsolemode + kernel32 = syscall.NewLazyDLL("kernel32.dll") + + procGetConsoleMode = kernel32.NewProc("GetConsoleMode") + procSetConsoleMode = kernel32.NewProc("SetConsoleMode") +} + func tryEnableOnCONOUT() bool { outHandle, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0) if err != nil { @@ -213,6 +221,8 @@ func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error { // IsTty returns true if the given file descriptor is a terminal. func IsTty(fd uintptr) bool { + initKernel32Proc() + var st uint32 r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0) return r != 0 && e == 0 @@ -225,6 +235,8 @@ func IsTty(fd uintptr) bool { // fd := uintptr(syscall.Stdout) // for windows // IsTerminal(fd) func IsTerminal(fd uintptr) bool { + initKernel32Proc() + var st uint32 r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0) return r != 0 && e == 0