Skip to content

Commit

Permalink
ui: Do not clip strings for unattended output
Browse files Browse the repository at this point in the history
String clipping is only useful if we're rendering the UI. If output is
unattended or user has requested UI disabled, no need to clip output.
  • Loading branch information
danobi committed Dec 28, 2024
1 parent ade458a commit 00c841d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,22 @@ impl Stage {
// If output is attended, we do custom window with our own styling.
// Therefore, we need to strip away any existing formatting.
let stripped = strip_ansi_codes(trimmed_line);

// Clip output to fit in terminal.
//
// Note this _does_not_ handle characters that expand to multiple columns,
// like tabs or other fancy unicode. This is known to corrupt UI visuals. There
// is no real solution to this without doing a mini terminal emulator AFAIK.
// The workaround is to set VMTEST_NO_UI.
let width = self.term.size_checked().map(|(_, w)| w).unwrap_or(u16::MAX);
let clipped = truncate_str(&stripped, width as usize, "...");

// Apply styling
let styled = match &custom {
Some(s) => s.apply_to(stripped),
None => style(stripped).dim(),
Some(s) => s.apply_to(clipped),
None => style(clipped).dim(),
};

styled.to_string()
} else {
// If output is not attended, we do pass through
Expand All @@ -118,16 +130,9 @@ impl Stage {
// Unwrap should never fail b/c we're sizing with `min()`
let window = self.lines.windows(self.window_size()).last().unwrap();

// Get terminal width, if any
let width = match self.term.size_checked() {
Some((_, w)) => w,
_ => u16::MAX,
};

// Print visible lines
for line in window {
let clipped = truncate_str(line, width as usize - 3, "...");
self.term.write_line(&format!("{}", clipped)).unwrap();
self.term.write_line(line).unwrap();
}
}

Expand Down

0 comments on commit 00c841d

Please sign in to comment.