Skip to content
Myr

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

2 files+29 added−19 removed

modified:renderer.go+3 added-2 removed
22 unmodified lines
23 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 lines
38}3940// newRenderer creates a new renderer. Normally you'll want to initialize it41// with os.Stdout as the argument.42func newRenderer(out io.Writer) *renderer {42func 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 lines
modified:tea.go+26 added-17 removed
1package tea23import (4 "fmt"5 "os"6 "sync"76 "github.com/muesli/termenv"8 te "github.com/muesli/termenv"9 "golang.org/x/crypto/ssh/terminal"10)1112// Msg represents an action and is usually the result of an IO operation. It's
34 unmodified lines
47type Program struct {48 init Init49 update Update50 view View5152 mtx sync.Mutex53 output *os.File // where to send output. this will usually be os.Stdout.54}5556// Quit is a special command that tells the program to exit.57func Quit() Msg {
20 unmodified lines
78 return &Program{79 init: init,80 update: update,81 view: view,8283 output: os.Stdout,84 }85}8687// Start initializes the program.88func (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{})8990 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 )9899 err := initTerminal()100 if err != nil {
27 unmodified lines
128 }()129130 // 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 }()138139 // Listen for window resizes135 go listenForResize(output, msgs, errs)140 go listenForResize(p.output, msgs, errs)141142 // Process commands143 go func() {144 for {
42 unmodified lines
187 }188 }189}190186// AltScreen enters the alternate screen buffer.187func AltScreen() {188 termenv.AltScreen()191// EnterAltScreen enters the alternate screen buffer.192func (p *Program) EnterAltScreen() {193 p.mtx.Lock()194 defer p.mtx.Unlock()195 fmt.Fprintf(p.output, te.CSI+te.AltScreenSeq)196}197198// ExitAltScreen exits the alternate screen buffer.192func ExitAltScreen() {193 termenv.ExitAltScreen()199func (p *Program) ExitAltScreen() {200 p.mtx.Lock()201 defer p.mtx.Unlock()202 fmt.Fprintf(p.output, te.CSI+te.ExitAltScreenSeq)203}