Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IoctlGetWinsize to get term dimensions #1

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions status/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,15 @@ func (t *term) Clear(w io.Writer) {

// Dim returns the current dimensions of the terminal.
func (t *term) Dim() (width, height int) {
ws, err := getWinsize(t.fd)
ws, err := unix.IoctlGetWinsize(int(t.fd), unix.TIOCGWINSZ)
if err != nil {
return 80, 20
}
return int(ws.Width), int(ws.Height)
return int(ws.Col), int(ws.Row)
}

func isTerminal(fd uintptr) bool {
var t syscall.Termios
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), termios, uintptr(unsafe.Pointer(&t)), 0, 0, 0)
return err == 0
}

type winsize struct {
Height, Width uint16
// We pad the struct to give us plenty of headroom. (In practice,
// darwin only has 8 additional bytes.) The proper way to do this
// would be to use cgo to get the proper struct definition, but I'd
// like to avoid this if possible.
Pad [128]byte
}

func getWinsize(fd uintptr) (*winsize, error) {
w := new(winsize)
_, _, err := unix.Syscall(syscall.SYS_IOCTL, fd, uintptr(unix.TIOCGWINSZ), uintptr(unsafe.Pointer(w)))
if err == 0 {
return w, nil
}
return w, err
}