forked from jesseduffield/gocui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_notwin.go
61 lines (51 loc) · 1.2 KB
/
gui_notwin.go
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
// +build !windows
package gocui
import (
"os"
"os/signal"
"syscall"
"unsafe"
"github.com/go-errors/errors"
)
type windowSize struct {
rows uint16
cols uint16
xpixels uint16
ypixels uint16
}
// getTermWindowSize is get terminal window size on linux or unix.
// When gocui run inside the docker contaienr need to check and get the window size.
func (g *Gui) getTermWindowSize() (int, int, error) {
out, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return 0, 0, err
}
defer out.Close()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGWINCH, syscall.SIGINT)
defer signal.Stop(signalCh)
var sz windowSize
for {
_, _, err = syscall.Syscall(
syscall.SYS_IOCTL,
out.Fd(),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&sz)),
)
// check terminal window size
if sz.cols > 0 && sz.rows > 0 {
return int(sz.cols), int(sz.rows), nil
}
select {
case signal := <-signalCh:
switch signal {
// when the terminal window size is changed
case syscall.SIGWINCH:
continue
// ctrl + c to cancel
case syscall.SIGINT:
return 0, 0, errors.New("There was not enough window space to start the application")
}
}
}
}