-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_windows.go
73 lines (61 loc) · 1.44 KB
/
utils_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
63
64
65
66
67
68
69
70
71
72
73
package jantar
import (
"fmt"
"os"
"os/exec"
"syscall"
"unsafe"
)
type coord struct {
x int16
y int16
}
type smallRect struct {
left int16
top int16
right int16
bottom int16
}
type consoleScreenBufferInfo struct {
size coord
cursorPosition coord
attributes uint16
window smallRect
maximumWindowSize coord
}
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
)
// NOTE: assuming that all non standard terminals on windows support ansi escape codes
func isTerminal(fd uintptr) bool {
term := os.Getenv("TERM")
if term == "" || term == "cygwin" {
return false
}
return true
}
func getTerminalSize() (int16, int16) {
hOut, err1 := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
if err1 != nil {
return 0, 0
}
// try windows api call
var info consoleScreenBufferInfo
rc, _, _ := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(hOut), uintptr(unsafe.Pointer(&info)), 0)
if int(rc) != 0 {
return info.window.right - info.window.left + 1, info.window.bottom - info.window.top + 1
}
// try stty command
var width int16
var height int16
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err == nil {
if n, err := fmt.Sscanf(string(out), "%d %d", &height, &width); err == nil && n == 2 {
return width, height
}
}
return 0, 0
}