Skip to content

Commit

Permalink
fix test error on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 6, 2021
1 parent 02c98f1 commit 1459bf6
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions detect_windows.go
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1459bf6

Please sign in to comment.