Skip to content
Myr

Add platform-specific terminal init & restore

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Muehlhaeuser · committed by Christian Muehlhaeuser · parent 47bfe2b · Browse files · 3 files+44 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+44 added−14 removed

  • other:+44 added−14 removed

3 files+44 added−14 removed

modified:tea.go+4 added-14 removed
4 unmodified lines
5 "io"6 "log"7 "log/syslog"8 "strings"910 "github.com/pkg/term"9)1011// Msg represents an action. It's used by Update to update the UI.12type Msg interface{}
23 unmodified lines
36 init Init37 update Update38 view View39 subscriptions []Sub42 rw io.ReadWriter40}4142// ErrMsg is just a regular message containing an error. We handle it in Update43// just like a regular message by case switching. Of course, the developer
45 unmodified lines
89 done = make(chan struct{})90 linesRendered int91 )9296 tty, err := term.Open("/dev/tty")93 err := initTerminal()94 if err != nil {95 return err96 }97 defer restoreTerminal()98101 p.rw = tty102 tty.SetRaw()103 hideCursor()104 defer func() {105 showCursor()106 tty.Restore()107 }()10899 // Initialize program100 model, cmd = p.init()101 if cmd != nil {102 go func() {
8 unmodified lines
111 // as a subscription, but it blocks nicely and seems to be a common enough112 // need that we're enabling it by default.113 go func() {114 for {125 msg, _ := ReadKey(p.rw)115 msg, _ := ReadKey(os.Stdin)116 msgs <- KeyMsg(msg)117 }118 }()119
52 unmodified lines
172173 if linesRendered > 0 {174 clearLines(linesRendered)175 }186 io.WriteString(p.rw, view)176 io.WriteString(os.Stdout, view)177 return strings.Count(view, "\r\n")178}179180// UseSysLog sets up logging to log the system log. This becomes helpful when
12 unmodified lines
added:tty_unix.go+28 added
1// +build darwin dragonfly freebsd linux netbsd openbsd solaris23package tea45import (6 "github.com/pkg/term"7)89var (10 tty *term.Term11)1213func initTerminal() error {14 var err error15 tty, err = term.Open("/dev/tty")16 if err != nil {17 return err18 }1920 tty.SetRaw()21 hideCursor()22 return nil23}2425func restoreTerminal() {26 showCursor()27 tty.Restore()28}
added:tty_windows.go+12 added
1// +build windows23package tea45func initTerminal() error {6 hideCursor()7 return nil8}910func restoreTerminal() {11 showCursor()12}