-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.go
More file actions
58 lines (51 loc) · 1.39 KB
/
terminal.go
File metadata and controls
58 lines (51 loc) · 1.39 KB
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
package main
import (
"os"
"strconv"
"syscall"
"unsafe"
)
// ─── Terminal ──────────────────────────────────────────────────────────────
// getTerminalWidth returns the terminal width in columns.
// Priority: COLUMNS env var → ioctl TIOCGWINSZ (stdout, stderr, /dev/tty) → 80 default.
func getTerminalWidth() int {
// Try COLUMNS env var first
if s := os.Getenv("COLUMNS"); s != "" {
if v, err := strconv.Atoi(s); err == nil && v > 0 {
return v
}
}
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
var ws winsize
// Try stdout, then stderr (stdout may be piped when Claude Code captures output)
for _, fd := range []uintptr{uintptr(syscall.Stdout), uintptr(syscall.Stderr)} {
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&ws)),
)
if errno == 0 && ws.Col > 0 {
return int(ws.Col)
}
}
// Try /dev/tty as last resort (works even when both stdout and stderr are piped)
if f, err := os.Open("/dev/tty"); err == nil {
defer f.Close()
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&ws)),
)
if errno == 0 && ws.Col > 0 {
return int(ws.Col)
}
}
return 80
}