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

Improve get terminal window size in the docker container #15 #18

Merged
merged 2 commits into from Apr 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 58 additions & 2 deletions gui.go
Expand Up @@ -6,6 +6,11 @@ package gocui

import (
standardErrors "errors"
"os"
"os/signal"
"runtime"
"syscall"
"unsafe"

"github.com/go-errors/errors"

Expand Down Expand Up @@ -83,7 +88,8 @@ type Gui struct {

// NewGui returns a new Gui object with a given output mode.
func NewGui(mode OutputMode, supportOverlaps bool) (*Gui, error) {
if err := termbox.Init(); err != nil {
err := termbox.Init()
glvr182 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

Expand All @@ -97,7 +103,14 @@ func NewGui(mode OutputMode, supportOverlaps bool) (*Gui, error) {
g.tbEvents = make(chan termbox.Event, 20)
g.userEvents = make(chan userEvent, 20)

g.maxX, g.maxY = termbox.Size()
if runtime.GOOS != "windows" {
g.maxX, g.maxY, err = g.getTermWindowSize()
if err != nil {
return nil, err
}
} else {
g.maxX, g.maxY = termbox.Size()
}

g.BgColor, g.FgColor = ColorDefault, ColorDefault
g.SelBgColor, g.SelFgColor = ColorDefault, ColorDefault
Expand Down Expand Up @@ -740,3 +753,46 @@ func IsUnknownView(err error) bool {
func IsQuit(err error) bool {
return err.Error() == ErrQuit.Error()
}

glvr182 marked this conversation as resolved.
Show resolved Hide resolved
// 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) {
var sz struct {
rows uint16
cols uint16
}

var termw, termh int

out, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
glvr182 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, 0, err
}
defer out.Close()

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGWINCH, syscall.SIGINT)

for {
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
out.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
glvr182 marked this conversation as resolved.
Show resolved Hide resolved

// check terminal window size
termw, termh = int(sz.cols), int(sz.rows)
if termw > 0 && termh > 0 {
return termw, termh, nil
}

select {
glvr182 marked this conversation as resolved.
Show resolved Hide resolved
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("stop to get term window size")
}
}
}
}