Skip to content
Myr

feat(render): remove flickering

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

authored by Konstantin Antonov · committed by Christian Rocha · parent 129d5eb · Browse files · 1 file+17 added−47 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+17 added−47 removed

  • other:+17 added−47 removed

1 file+17 added−47 removed

modified:standard_renderer.go+17 added-47 removed
166 unmodified lines
167168 // Output buffer169 buf := &bytes.Buffer{}170171 // Moving to the begining of the section, that we rendered.172 buf.WriteString(ansi.CursorUp(r.linesRendered-1) + ansi.CursorLeft(r.width))173174 newLines := strings.Split(r.buf.String(), "\n")175176 // If we know the output's height, we can use it to determine how many177 // lines we can render. We drop lines from the top of the render buffer if
3 unmodified lines
181 newLines = newLines[len(newLines)-r.height:]182 }183184 numLinesThisFlush := len(newLines)182 oldLines := strings.Split(r.lastRender, "\n")183 skipLines := make(map[int]struct{})185 flushQueuedMessages := len(r.queuedMessageLines) > 0 && !r.altScreenActive186186 // Clear any lines we painted in the last render.187 if r.linesRendered > 0 {188 for i := r.linesRendered - 1; i > 0; i-- {189 // if we are clearing queued messages, we want to clear all lines, since190 // printing messages allows for native terminal word-wrap, we191 // don't have control over the queued lines192 if flushQueuedMessages {193 buf.WriteString(ansi.EraseEntireLine)194 } else if (len(newLines) <= len(oldLines)) && (len(newLines) > i && len(oldLines) > i) && (newLines[i] == oldLines[i]) {195 // If the number of lines we want to render hasn't increased and196 // new line is the same as the old line we can skip rendering for197 // this line as a performance optimization.198 skipLines[i] = struct{}{}199 } else if _, exists := r.ignoreLines[i]; !exists {200 buf.WriteString(ansi.EraseEntireLine)201 }202203 buf.WriteString(ansi.CursorUp1)204 }205206 if _, exists := r.ignoreLines[0]; !exists {207 // We need to return to the start of the line here to properly208 // erase it. Going back the entire width of the terminal will209 // usually be farther than we need to go, but terminal emulators210 // will stop the cursor at the start of the line as a rule.211 //212 // We use this sequence in particular because it's part of the ANSI213 // standard (whereas others are proprietary to, say, VT100/VT52).214 // If cursor previous line (ESC[ + <n> + F) were better supported215 // we could use that above to eliminate this step.216 buf.WriteString(ansi.CursorLeft(r.width))217 buf.WriteString(ansi.EraseEntireLine)218 }219 }220221 // Merge the set of lines we're skipping as a rendering optimization with222 // the set of lines we've explicitly asked the renderer to ignore.223 for k, v := range r.ignoreLines {224 skipLines[k] = v225 }226187 if flushQueuedMessages {228 // Dump the lines we've queued up for printing188 // Dump the lines we've queued up for printing.189 for _, line := range r.queuedMessageLines {190191 // Removing previousy rendered content at the end of line.192 line = line + ansi.EraseLineRight193194 _, _ = buf.WriteString(line)195 _, _ = buf.WriteString("\r\n")196 }233 // clear the queued message lines197 // Clear the queued message lines.198 r.queuedMessageLines = []string{}199 }200237 // Paint new lines201 // Paint new lines.202 for i := 0; i < len(newLines); i++ {239 if _, skip := skipLines[i]; skip {203 if _, skip := r.ignoreLines[i]; skip {204 // Unless this is the last line, move the cursor down.205 if i < len(newLines)-1 {206 buf.WriteString(ansi.CursorDown1)207 }
5 unmodified lines
213 }214215 line := newLines[i]216217 // Removing left over content from previous render at the end of the line.218 line = line + ansi.EraseLineRight219220 // Truncate lines wider than the width of the window to avoid221 // wrapping, which will mess up rendering. If we don't have the222 // width of the window this will be ignored.223 //
12 unmodified lines
236 }237 }238 r.linesRendered = numLinesThisFlush239240 // Clearing left over content from last render (if our new frame contains less lines).241 buf.WriteString(ansi.EraseDisplayRight)242243 // Make sure the cursor is at the start of the last line to keep rendering244 // behavior consistent.245 if r.altScreenActive {246 // This case fixes a bug in macOS terminal. In other terminals the
493 unmodified lines