mirror of
https://github.com/jroimartin/gocui.git
synced 2025-05-05 19:29:37 +08:00
Merge pull request #7 from shanempope/make-build-on-windows
Move non-Windows code behind build tags
This commit is contained in:
commit
c5b8f884b6
64
gui.go
64
gui.go
@ -6,13 +6,8 @@ package gocui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
standardErrors "errors"
|
standardErrors "errors"
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
|
|
||||||
@ -100,13 +95,9 @@ type Gui struct {
|
|||||||
func NewGui(mode OutputMode, supportOverlaps bool) (*Gui, error) {
|
func NewGui(mode OutputMode, supportOverlaps bool) (*Gui, error) {
|
||||||
g := &Gui{}
|
g := &Gui{}
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
var err error
|
||||||
var err error
|
if g.maxX, g.maxY, err = g.getTermWindowSize(); err != nil {
|
||||||
if g.maxX, g.maxY, err = g.getTermWindowSize(); err != nil {
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
g.maxX, g.maxY = termbox.Size()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := termbox.Init(); err != nil {
|
if err := termbox.Init(); err != nil {
|
||||||
@ -858,52 +849,3 @@ func (g *Gui) loaderTick() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
61
gui_notwin.go
Normal file
61
gui_notwin.go
Normal file
@ -0,0 +1,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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
gui_win.go
Normal file
10
gui_win.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package gocui
|
||||||
|
|
||||||
|
import "github.com/jesseduffield/termbox-go"
|
||||||
|
|
||||||
|
func (g *Gui) getTermWindowSize() (int, int, error) {
|
||||||
|
x, y := termbox.Size()
|
||||||
|
return x, y, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user