Skip to content
Myr

More functional rendering approach

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 8c9360e · Browse files · 1 file+13 added−13 removed
This commit

State

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

Diff stats

1 file+13 added−13 removed

  • other:+13 added−13 removed

1 file+13 added−13 removed

modified:tea.go+13 added-13 removed
38 unmodified lines
39 update Update40 view View41 subscriptions []Sub42 rw io.ReadWriter43 linesRendered int43}4445// ErrMsg is just a regular message containing an error. We handle it in Update46// just like a regular message by case switching. Of course, the developer
37 unmodified lines
8485// Start initializes the program86func (p *Program) Start() error {87 var (89 model Model90 cmd Cmd91 cmds = make(chan Cmd)92 msgs = make(chan Msg)93 done = make(chan struct{})88 model Model89 cmd Cmd90 cmds = make(chan Cmd)91 msgs = make(chan Msg)92 done = make(chan struct{})93 linesRendered int94 )9596 tty, err := term.Open("/dev/tty")97 if err != nil {
16 unmodified lines
114 }()115 }116117 // Render initial view118 p.render(model)118 linesRendered = p.render(model, linesRendered)119120 // Subscribe to user input. We could move this out of here and offer it121 // as a subscription, but it blocks nicely and seems to be a common enough122 // need that we're enabling it by default.
43 unmodified lines
166 }167168 model, cmd = p.update(msg, model)169 cmds <- cmd // process command (if any)170 p.render(model)170 linesRendered = p.render(model, linesRendered)171 }172 }173}174175// Render a view to the terminal176func (p *Program) render(model Model) {175// Render a view to the terminal. Returns the number of lines rendered.176func (p *Program) render(model Model, linesRendered int) int {177 view := p.view(model) + "\n"178179 // We need to add carriage returns to ensure that the cursor travels to the180 // start of a column after a newline181 view = strings.Replace(view, "\n", "\r\n", -1)182183 if p.linesRendered > 0 {184 clearLines(p.linesRendered)183 if linesRendered > 0 {184 clearLines(linesRendered)185 }186 io.WriteString(p.rw, view)187 p.linesRendered = strings.Count(view, "\r\n")187 return strings.Count(view, "\r\n")188}189190// UseSysLog sets up logging to log the system log. This becomes helpful when191// debugging since we can't easily print to the terminal since our TUI is
11 unmodified lines