File: util.go

package info (click to toggle)
golang-github-elisescu-pty 1.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176 kB
  • sloc: sh: 14; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download | duplicates (2)
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
// +build !windows

package pty

import (
	"os"
	"syscall"
	"unsafe"
)

// Getsize returns the number of rows (lines) and cols (positions
// in each line) in terminal t.
func Getsize(t *os.File) (rows, cols int, err error) {
	var ws winsize
	err = getwindowrect(&ws, t.Fd())
	return int(ws.ws_row), int(ws.ws_col), err
}

// Setsize sets the number of rows (lines) and cols (positions
// in each line) in terminal t. Both rows and cols have to be
// positive integers.
func Setsize(t *os.File, rows, cols int) error {
	ws := winsize{
		ws_col:    uint16(cols),
		ws_row:    uint16(rows),
		ws_xpixel: uint16(0), // not used
		ws_ypixel: uint16(0), // not used
	}

	return setwindowrect(&ws, t.Fd())
}

type winsize struct {
	ws_row    uint16
	ws_col    uint16
	ws_xpixel uint16
	ws_ypixel uint16
}

func getwindowrect(ws *winsize, fd uintptr) error {
	_, _, errno := syscall.Syscall(
		syscall.SYS_IOCTL,
		fd,
		syscall.TIOCGWINSZ,
		uintptr(unsafe.Pointer(ws)),
	)
	if errno != 0 {
		return syscall.Errno(errno)
	}
	return nil
}

func setwindowrect(ws *winsize, fd uintptr) error {
	_, _, errno := syscall.Syscall(
		syscall.SYS_IOCTL,
		fd,
		syscall.TIOCSWINSZ,
		uintptr(unsafe.Pointer(ws)),
	)
	if errno != 0 {
		return syscall.Errno(errno)
	}
	return nil
}