-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtty_windows.go
62 lines (51 loc) · 1.29 KB
/
tty_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package colog
import (
"syscall"
"unsafe"
)
// Use variable indirection for test stubbing
var isTerminal = isTerminalFunc
var terminalWidth = terminalWidthFunc
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
var procInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
var procMode = kernel32.NewProc("GetConsoleMode")
// Not applicable in windows
// define constant to avoid compilation error
const ioctlReadTermios = 0x0
// isTerminalFunc returns true if the given file descriptor is a terminal.
func isTerminalFunc(fd int) bool {
var st uint32
r, _, errno := syscall.Syscall(procMode.Addr(), 2, uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
if errno != 0 {
return false
}
return r != 0
}
type short int16
type word uint16
type coord struct {
x short
y short
}
type rectangle struct {
left short
top short
right short
bottom short
}
type termInfo struct {
size coord
cursorPosition coord
attributes word
window rectangle
maximumWindowSize coord
}
// terminalWidthFunc returns the width in characters of the terminal.
func terminalWidthFunc(fd int) (width int) {
var info termInfo
_, _, errno := syscall.Syscall(procInfo.Addr(), 2, uintptr(fd), uintptr(unsafe.Pointer(&info)), 0)
if errno != 0 {
return -1
}
return int(info.size.x)
}