1// Copyright 2014 The gocui Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build !windows
6
7package gocui
8
9import (
10 "os"
11 "os/signal"
12 "syscall"
13 "unsafe"
14
15 "github.com/go-errors/errors"
16)
17
18// getTermWindowSize is get terminal window size on linux or unix.
19// When gocui run inside the docker contaienr need to check and get the window size.
20func (g *Gui) getTermWindowSize() (int, int, error) {
21 var sz struct {
22 rows uint16
23 cols uint16
24 _ [2]uint16 // to match underlying syscall; see https://github.com/awesome-gocui/gocui/issues/33
25 }
26
27 var termw, termh int
28
29 out, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
30 if err != nil {
31 return 0, 0, err
32 }
33 defer out.Close()
34
35 signalCh := make(chan os.Signal, 1)
36 signal.Notify(signalCh, syscall.SIGWINCH, syscall.SIGINT)
37
38 for {
39 _, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
40 out.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
41
42 // check terminal window size
43 termw, termh = int(sz.cols), int(sz.rows)
44 if termw > 0 && termh > 0 {
45 return termw, termh, nil
46 }
47
48 select {
49 case signal := <-signalCh:
50 switch signal {
51 // when the terminal window size is changed
52 case syscall.SIGWINCH:
53 continue
54 // ctrl + c to cancel
55 case syscall.SIGINT:
56 return 0, 0, errors.New("stop to get term window size")
57 }
58 }
59 }
60}