43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
//go:build linux
|
|||
|
|
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"syscall"
|
||
|
|
"unsafe"
|
||
|
|
)
|
||
|
|
|
||
|
|
func termWidth() int {
|
||
|
|
var ws struct{ Row, Col, Xpixel, Ypixel uint16 }
|
||
|
|
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(os.Stdin.Fd()), syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws)))
|
||
|
|
if e != 0 || ws.Col == 0 {
|
||
|
|
return 80
|
||
|
|
}
|
||
|
|
return int(ws.Col)
|
||
|
|
}
|
||
|
|
|
||
|
|
func isTerminal(fd int) bool {
|
||
|
|
var t syscall.Termios
|
||
|
|
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&t)))
|
||
|
|
return e == 0
|
||
|
|
}
|
||
|
|
|
||
|
|
func makeRaw(fd int) (func(), error) {
|
||
|
|
var old syscall.Termios
|
||
|
|
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&old))); e != 0 {
|
||
|
|
return nil, e
|
||
|
|
}
|
||
|
|
raw := old
|
||
|
|
raw.Iflag &^= syscall.ICRNL | syscall.IXON | syscall.BRKINT | syscall.INPCK | syscall.ISTRIP
|
||
|
|
raw.Oflag &^= syscall.OPOST
|
||
|
|
raw.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
|
||
|
|
raw.Cflag |= syscall.CS8
|
||
|
|
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TCSETS, uintptr(unsafe.Pointer(&raw))); e != 0 {
|
||
|
|
return nil, e
|
||
|
|
}
|
||
|
|
return func() {
|
||
|
|
syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TCSETS, uintptr(unsafe.Pointer(&old)))
|
||
|
|
}, nil
|
||
|
|
}
|