Use same output for altscreen stuff as everything else
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent 05f3028 · Browse files · 2 files·+29 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+29 added−19 removed
- other:+29 added−19 removed
Provenance
- Commit
3a70ecd- Tree
a744d64- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
05f3028
2 files changed+29 added−19 removed
modified:renderer.go+3 added-2 removed
22 unmodified lines23 out io.Writer24 buf bytes.Buffer25 framerate time.Duration26 ticker *time.Ticker27- mtx sync.Mutex27+ mtx *sync.Mutex28 done chan struct{}29 lastRender string30 linesRendered int31 6 unmodified lines38 }39 40 // newRenderer creates a new renderer. Normally you'll want to initialize it41 // with os.Stdout as the argument.42-func newRenderer(out io.Writer) *renderer {42+func newRenderer(out io.Writer, mtx *sync.Mutex) *renderer {43 return &renderer{44 out: out,45+ mtx: mtx,46 framerate: defaultFramerate,47 }48 }49 313 unmodified linesmodified:tea.go+26 added-17 removed
1 package tea2 3 import (4+ "fmt"5 "os"6+ "sync"7 6- "github.com/muesli/termenv"8+ te "github.com/muesli/termenv"9 "golang.org/x/crypto/ssh/terminal"10 )11 12 // Msg represents an action and is usually the result of an IO operation. It's34 unmodified lines47 type Program struct {48 init Init49 update Update50 view View51+52+ mtx sync.Mutex53+ output *os.File // where to send output. this will usually be os.Stdout.54 }55 56 // Quit is a special command that tells the program to exit.57 func Quit() Msg {20 unmodified lines78 return &Program{79 init: init,80 update: update,81 view: view,82+83+ output: os.Stdout,84 }85 }86 87 // Start initializes the program.88 func (p *Program) Start() error {89 var (83- model Model84- cmd Cmd85- cmds = make(chan Cmd)86- msgs = make(chan Msg)87- errs = make(chan error)88- done = make(chan struct{})89-90- output *os.File = os.Stdout91- mrRenderer = newRenderer(output)90+ model Model91+ cmd Cmd92+ cmds = make(chan Cmd)93+ msgs = make(chan Msg)94+ errs = make(chan error)95+ done = make(chan struct{})96+ mrRenderer = newRenderer(p.output, &p.mtx)97 )98 99 err := initTerminal()100 if err != nil {27 unmodified lines128 }()129 130 // Get initial terminal size131 go func() {127- w, h, err := terminal.GetSize(int(output.Fd()))132+ w, h, err := terminal.GetSize(int(p.output.Fd()))133 if err != nil {134 errs <- err135 }136 msgs <- WindowSizeMsg{w, h}137 }()138 139 // Listen for window resizes135- go listenForResize(output, msgs, errs)140+ go listenForResize(p.output, msgs, errs)141 142 // Process commands143 go func() {144 for {42 unmodified lines187 }188 }189 }190 186-// AltScreen enters the alternate screen buffer.187-func AltScreen() {188- termenv.AltScreen()191+// EnterAltScreen enters the alternate screen buffer.192+func (p *Program) EnterAltScreen() {193+ p.mtx.Lock()194+ defer p.mtx.Unlock()195+ fmt.Fprintf(p.output, te.CSI+te.AltScreenSeq)196 }197 198 // ExitAltScreen exits the alternate screen buffer.192-func ExitAltScreen() {193- termenv.ExitAltScreen()199+func (p *Program) ExitAltScreen() {200+ p.mtx.Lock()201+ defer p.mtx.Unlock()202+ fmt.Fprintf(p.output, te.CSI+te.ExitAltScreenSeq)203 }