Skip to content
Myr

Only render lines that have changed since the last render

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 c07cbd1 · Browse files · 2 files+38 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+38 added−19 removed

  • other:+38 added−19 removed

2 files+38 added−19 removed

modified:renderer.go+1 added
1package tea23// renderer is the interface for Bubble Tea renderers.4type renderer interface {5 start()6 stop()7 write(string)
3 unmodified lines
modified:standard_renderer.go+37 added-19 removed
14 unmodified lines
15 // update the view.16 defaultFramerate = time.Second / 6017)1819// renderer is a timer-based renderer, updating the view at a given framerate20// to avoid overloading the terminal emulator.19// standardRenderer is a framerate-based terminal renderer, updating the view20// at a given framerate to avoid overloading the terminal emulator.21//22// In cases where very high performance is needed the renderer can be told23// to exclude ranges of lines, allowing them to be written to directly.24type standardRenderer struct {
12 unmodified lines
37 // renderer dimensions; usually the size of the window38 width int39 height int4041 // lines not to render41 // lines explicitly set not to render42 ignoreLines map[int]struct{}43}4445// newRenderer creates a new renderer. Normally you'll want to initialize it
47 unmodified lines
93 // Nothing to do94 return95 }9697 // Output buffer98 out := new(bytes.Buffer)99100 newLines := strings.Split(r.buf.String(), "\n")101 oldLines := strings.Split(r.lastRender, "\n")102 skipLines := make(map[int]struct{})103104 // Clear any lines we painted in the last render.105 if r.linesRendered > 0 {106 for i := r.linesRendered - 1; i > 0; i-- {102 // Check if we should skip rendering for this line. Clearing the103 // line before painting is part of the standard rendering routine.104 if _, exists := r.ignoreLines[i]; !exists {107 // Determine if we should skip rendering for this line. We can skip108 // for two reasons:109 //110 // 1. We've explicitly set this line to be ignored so we can render111 // it elsewhere (for example, via the performance scroll methods112 // like insertTop and insertBottom).113 //114 // 2. The new line is the same as the old line, in which case we115 // can skip rendering for this line as a performance optimization.116 _, ignoreLine := r.ignoreLines[i]117 if ignoreLine || (len(newLines) > i && len(oldLines) > i && newLines[i] == oldLines[i]) {118 skipLines[i] = struct{}{}119 } else {120 clearLine(out)121 }122123 cursorUp(out)124 }125111 if _, exists := r.ignoreLines[0]; !exists {126 if _, exists := skipLines[0]; !exists {127 // We need to return to the start of the line here to properly128 // erase it. Going back the entire width of the terminal will129 // usually be farther than we need to go, but terminal emulators130 // will stop the cursor at the start of the line as a rule.
7 unmodified lines
138 }139 }140141 r.linesRendered = 0127 lines := strings.Split(r.buf.String(), "\n")142143 // Paint new lines130 for i := 0; i < len(lines); i++ {131 if _, exists := r.ignoreLines[r.linesRendered]; exists {132 cursorDown(out) // skip rendering for this line.144 for i := 0; i < len(newLines); i++ {145 if _, skip := skipLines[r.linesRendered]; skip {146 // Unless this is the last line, move the cursor down.147 if i < len(newLines)-1 {148 cursorDown(out)149 }150 } else {134 line := lines[i]151 line := newLines[i]152153 // Truncate lines wider than the width of the window to avoid137 // rendering troubles. If we don't have the width of the window138 // this will be ignored.154 // wrapping, which will mess up rendering. If we don't have the155 // width of the window this will be ignored.156 //140 // Note that on Windows we can't get the width of the window141 // (signal SIGWINCH is not supported), so this will be ignored.157 // Note that on Windows we only get the width of the window on158 // program initialization, so after a resize this won't perform159 // correctly (signal SIGWINCH is not supported on Windows).160 if r.width > 0 {161 line = truncate.String(line, uint(r.width))162 }163164 _, _ = io.WriteString(out, line)165148 if i != len(lines)-1 {166 if i != len(newLines)-1 {167 _, _ = io.WriteString(out, "\r\n")168 }169 }170 r.linesRendered++
1 unmodified line
172173 // Make sure the cursor is at the start of the last line to keep rendering174 // behavior consistent.175 if r.altScreenActive {158 // We need this case to fix a bug in macOS terminal. In other terminals159 // the below case seems to do the job regardless of whether or not we're176 // This case fixes a bug in macOS terminal. In other terminals the177 // other case seems to do the job regardless of whether or not we're178 // using the full terminal window.179 moveCursor(out, r.linesRendered, 0)180 } else {181 cursorBack(out, r.width)
235 unmodified lines