@@ -55,12 +55,7 @@ func tryEnableVTP(enable bool) bool {
55
55
56
56
debugf ("True-Color by enable VirtualTerminalProcessing on windows" )
57
57
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 ()
64
59
65
60
// enable colors on windows terminal
66
61
if tryEnableOnCONOUT () {
@@ -70,6 +65,19 @@ func tryEnableVTP(enable bool) bool {
70
65
return tryEnableOnStdout ()
71
66
}
72
67
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
+
73
81
func tryEnableOnCONOUT () bool {
74
82
outHandle , err := syscall .Open ("CONOUT$" , syscall .O_RDWR , 0 )
75
83
if err != nil {
@@ -213,6 +221,8 @@ func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
213
221
214
222
// IsTty returns true if the given file descriptor is a terminal.
215
223
func IsTty (fd uintptr ) bool {
224
+ initKernel32Proc ()
225
+
216
226
var st uint32
217
227
r , _ , e := syscall .Syscall (procGetConsoleMode .Addr (), 2 , fd , uintptr (unsafe .Pointer (& st )), 0 )
218
228
return r != 0 && e == 0
@@ -225,6 +235,8 @@ func IsTty(fd uintptr) bool {
225
235
// fd := uintptr(syscall.Stdout) // for windows
226
236
// IsTerminal(fd)
227
237
func IsTerminal (fd uintptr ) bool {
238
+ initKernel32Proc ()
239
+
228
240
var st uint32
229
241
r , _ , e := syscall .Syscall (procGetConsoleMode .Addr (), 2 , fd , uintptr (unsafe .Pointer (& st )), 0 )
230
242
return r != 0 && e == 0
0 commit comments