forked from AdRoll/baker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.go
More file actions
41 lines (31 loc) · 643 Bytes
/
term.go
File metadata and controls
41 lines (31 loc) · 643 Bytes
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
// +build !windows
package baker
import (
"syscall"
"unsafe"
)
func terminalWidth() uint {
const (
maxWidth = 140 // don't go over 140 chars anyway
defaultWidth = 110 // in case we can't get the terminal width
)
var w uint
defer func() {
if err := recover(); err != nil {
w = defaultWidth
}
}()
ws := &struct{ Row, Col, Xpixel, Ypixel uint16 }{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdout),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
if ws.Col > maxWidth {
return maxWidth
}
w = uint(ws.Col)
return w
}