Add platform-specific terminal init & restore

This commit is contained in:
Christian Muehlhaeuser 2020-01-25 07:15:29 +01:00
parent 47bfe2b5df
commit bc67e3896b
No known key found for this signature in database
GPG Key ID: 3CF9FA45CA1EBB7E
3 changed files with 44 additions and 14 deletions

18
tea.go
View File

@ -6,8 +6,6 @@ import (
"log"
"log/syslog"
"strings"
"github.com/pkg/term"
)
// Msg represents an action. It's used by Update to update the UI.
@ -39,7 +37,6 @@ type Program struct {
update Update
view View
subscriptions []Sub
rw io.ReadWriter
}
// ErrMsg is just a regular message containing an error. We handle it in Update
@ -93,18 +90,11 @@ func (p *Program) Start() error {
linesRendered int
)
tty, err := term.Open("/dev/tty")
err := initTerminal()
if err != nil {
return err
}
p.rw = tty
tty.SetRaw()
hideCursor()
defer func() {
showCursor()
tty.Restore()
}()
defer restoreTerminal()
// Initialize program
model, cmd = p.init()
@ -122,7 +112,7 @@ func (p *Program) Start() error {
// need that we're enabling it by default.
go func() {
for {
msg, _ := ReadKey(p.rw)
msg, _ := ReadKey(os.Stdin)
msgs <- KeyMsg(msg)
}
}()
@ -183,7 +173,7 @@ func (p *Program) render(model Model, linesRendered int) int {
if linesRendered > 0 {
clearLines(linesRendered)
}
io.WriteString(p.rw, view)
io.WriteString(os.Stdout, view)
return strings.Count(view, "\r\n")
}

28
tty_unix.go Normal file
View File

@ -0,0 +1,28 @@
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package tea
import (
"github.com/pkg/term"
)
var (
tty *term.Term
)
func initTerminal() error {
var err error
tty, err = term.Open("/dev/tty")
if err != nil {
return err
}
tty.SetRaw()
hideCursor()
return nil
}
func restoreTerminal() {
showCursor()
tty.Restore()
}

12
tty_windows.go Normal file
View File

@ -0,0 +1,12 @@
// +build windows
package tea
func initTerminal() error {
hideCursor()
return nil
}
func restoreTerminal() {
showCursor()
}