github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xcolor/xcolor_terminal_win.go (about) 1 //go:build windows 2 // +build windows 3 4 package xcolor 5 6 import ( 7 "io" 8 "os" 9 "syscall" 10 ) 11 12 func checkTerminal(w io.Writer) bool { 13 if f, ok := w.(*os.File); ok { 14 h := syscall.Handle(f.Fd()) 15 err := enableVirtualTerminalProcessing(h, true) 16 return err == nil 17 } 18 return false 19 } 20 21 func enableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error { 22 const EnableVirtualTerminalProcessing uint32 = 0x4 23 24 var mode uint32 25 err := syscall.GetConsoleMode(syscall.Stdout, &mode) 26 if err != nil { 27 return err 28 } 29 30 if enable { 31 mode |= EnableVirtualTerminalProcessing 32 } else { 33 mode &^= EnableVirtualTerminalProcessing 34 } 35 36 kernel32Dll := syscall.NewLazyDLL("Kernel32.dll") 37 setConsoleMode := kernel32Dll.NewProc("SetConsoleMode") 38 ret, _, err := setConsoleMode.Call(uintptr(stream), uintptr(mode)) 39 if ret == 0 { 40 return err 41 } 42 43 return nil 44 }