Skip to content

Commit

Permalink
don't truncate multi-line printf in std renderer
Browse files Browse the repository at this point in the history
When logging something into the scrollback history of the standard renderer, the current approach temporarially increases the size of the UI by the number of logged messages. This has 2 issues:

- log lines are truncated according to the current width of the terminal UI. This truncation behaviour is desireable when writing into the TUI's controlled space, but is not desireable when Printf()ing messages into the scrollback, since the contents of the logged line will be truncated with no way to recover the history.
- User code pre-wrapping messages they want to print to the scrollback don't behave the way that you would want/expect log lines in a terminal to behave:
  - Resizing the terminal emulator does not reflow the lines to the new terminal's width
  - Manual wrapping breaks most terminal emulator's detection of clickable URLs and file paths

This change replaces that approach with a full clear of the previos UI + printing unwrapped messages into the terminal before the next render

this has a minor performance impact because it necessitates a full clear of the terminal UI on each Printf(). This breaks duplicate line detection between renders. However, the current approach will rarely hit duplicate line detection, since adding log messages to the front of the printout buffer offsets the new buffer. Therefore, the current line detection logic will only trigger when the buffer contains duplicate lines offset by the number of messages being logged in this render pass.
  • Loading branch information
Max Huang-Hobbs authored and Max Huang-Hobbs committed Feb 8, 2024
1 parent da49e8f commit c7b351c
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,18 @@ func (r *standardRenderer) flush() {
skipLines := make(map[int]struct{})
flushQueuedMessages := len(r.queuedMessageLines) > 0 && !r.altScreenActive

// Add any queued messages to this render
if flushQueuedMessages {
newLines = append(r.queuedMessageLines, newLines...)
r.queuedMessageLines = []string{}
}

// Clear any lines we painted in the last render.
if r.linesRendered > 0 {
for i := r.linesRendered - 1; i > 0; i-- {
// If the number of lines we want to render hasn't increased and
// new line is the same as the old line we can skip rendering for
// this line as a performance optimization.
if (len(newLines) <= len(oldLines)) && (len(newLines) > i && len(oldLines) > i) && (newLines[i] == oldLines[i]) {
// if we are clearing queued messages, we want to clear all lines, since
// printing messages allows for native terminal word-wrap, we
// don't have control over the queued lines
if flushQueuedMessages {
out.ClearLine()
} else if (len(newLines) <= len(oldLines)) && (len(newLines) > i && len(oldLines) > i) && (newLines[i] == oldLines[i]) {
// If the number of lines we want to render hasn't increased and
// new line is the same as the old line we can skip rendering for
// this line as a performance optimization.
skipLines[i] = struct{}{}
} else if _, exists := r.ignoreLines[i]; !exists {
out.ClearLine()
Expand Down Expand Up @@ -215,6 +214,16 @@ func (r *standardRenderer) flush() {
skipLines[k] = v
}

if flushQueuedMessages {
// Dump the lines we've queued up for printing
for _, line := range r.queuedMessageLines {
_, _ = out.WriteString(line)
_, _ = out.WriteString("\r\n")
}
// clear the queued message lines
r.queuedMessageLines = []string{}
}

// Paint new lines
for i := 0; i < len(newLines); i++ {
if _, skip := skipLines[i]; skip {
Expand Down Expand Up @@ -671,6 +680,7 @@ func ScrollDown(newLines []string, topBoundary, bottomBoundary int) Cmd {

type printLineMessage struct {
messageBody string
// if true, skips truncation of the message
}

// Println prints above the Program. This output is unmanaged by the program and
Expand Down

0 comments on commit c7b351c

Please sign in to comment.