const MAX_LINES: usize = 600;
const HEAD_LINES: usize = 350;
const TAIL_LINES: usize = 200;
const MAX_LINE_CHARS: usize = 2000;
const MIN_REPEATS: usize = 3;
pub(crate) fn strip_ansi(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
while let Some(c) = chars.next() {
if c != '\x1b' {
out.push(c);
continue;
}
match chars.next() {
Some('[') => {
for c in chars.by_ref() {
if ('\x40'..='\x7e').contains(&c) {
break;
}
}
}
Some(']') => {
while let Some(c) = chars.next() {
if c == '\x07' {
break;
}
if c == '\x1b' {
chars.next();
break;
}
}
}
_ => {}
}
}
out
}
pub(crate) fn collapse_redraws(text: &str) -> String {
if !text.contains('\r') {
return text.to_string();
}
let text = text.replace("\r\n", "\n");
let mut out = String::with_capacity(text.len());
for (i, line) in text.split('\n').enumerate() {
if i > 0 {
out.push('\n');
}
let shown = line.rsplit('\r').find(|s| !s.is_empty()).unwrap_or("");
out.push_str(shown);
}
out
}
pub(crate) fn clean(text: &str) -> String {
collapse_redraws(&strip_ansi(text))
}
pub(crate) fn cached(
cell: &'static std::sync::OnceLock<regex::Regex>,
pattern: &str,
) -> &'static regex::Regex {
cell.get_or_init(|| regex::Regex::new(pattern).expect("literal pattern"))
}
pub(crate) fn omitted(n: usize, what: &str) -> String {
format!("... {n} {what} omitted ...")
}
#[derive(Default)]
pub(crate) struct Dropped {
slot: Option<usize>,
n: usize,
}
impl Dropped {
pub(crate) fn one(&mut self, out: &mut Vec<String>) {
if self.slot.is_none() {
self.slot = Some(out.len());
out.push(String::new());
}
self.n += 1;
}
pub(crate) fn finish(self, out: &mut [String], what: &str) {
if let Some(slot) = self.slot {
out[slot] = omitted(self.n, what);
}
}
}
impl Dropped {
pub(crate) fn finish_run(&mut self, out: &mut [String], what: &str) {
std::mem::take(self).finish(out, what);
}
}
pub(crate) fn drop_lines(text: &str, what: &str, is_noise: impl Fn(&str) -> bool) -> String {
let mut out: Vec<String> = Vec::new();
let mut dropped = Dropped::default();
for line in text.lines() {
if is_noise(line) {
dropped.one(&mut out);
} else {
out.push(line.to_string());
}
}
dropped.finish(&mut out, what);
join(&out)
}
pub(crate) fn squeeze(text: &str) -> String {
let mut lines: Vec<String> = Vec::new();
let mut prev_blank = false;
let mut run: Option<(String, usize)> = None;
let flush = |run: &mut Option<(String, usize)>, lines: &mut Vec<String>| {
if let Some((line, n)) = run.take() {
if n >= MIN_REPEATS {
lines.push(format!("{line} (x{n})"));
} else {
lines.extend(std::iter::repeat_n(line, n));
}
}
};
for raw in text.lines() {
let line = cut_line(raw, MAX_LINE_CHARS);
let blank = line.trim().is_empty();
if blank {
flush(&mut run, &mut lines);
if !prev_blank {
lines.push(String::new());
}
prev_blank = true;
continue;
}
prev_blank = false;
match &mut run {
Some((last, n)) if *last == line => *n += 1,
_ => {
flush(&mut run, &mut lines);
run = Some((line, 1));
}
}
}
flush(&mut run, &mut lines);
limit(lines)
}
pub(crate) fn cap(text: &str) -> String {
limit(text.lines().map(|l| cut_line(l, MAX_LINE_CHARS)).collect())
}
fn limit(mut lines: Vec<String>) -> String {
while lines.last().is_some_and(|l| l.trim().is_empty()) {
lines.pop();
}
if lines.len() > MAX_LINES {
let omitted_n = lines.len() - HEAD_LINES - TAIL_LINES;
let tail = lines.split_off(lines.len() - TAIL_LINES);
lines.truncate(HEAD_LINES);
lines.push(omitted(omitted_n, "lines"));
lines.extend(tail);
}
join(&lines)
}
pub(crate) fn cut_line(line: &str, max: usize) -> String {
let total = line.chars().count();
if total <= max {
return line.to_string();
}
let kept: String = line.chars().take(max).collect();
format!("{kept} ... [{} more chars]", total - max)
}
pub(crate) fn join<S: AsRef<str>>(lines: &[S]) -> String {
let mut out = String::new();
for line in lines {
out.push_str(line.as_ref());
out.push('\n');
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ansi_colors_titles_and_cursor_moves_are_stripped() {
assert_eq!(strip_ansi("\x1b[31mred\x1b[0m plain"), "red plain");
assert_eq!(strip_ansi("a\x1b[2K\x1b[1Gb"), "ab");
assert_eq!(strip_ansi("\x1b]0;title\x07text"), "text");
assert_eq!(
strip_ansi("\x1b]8;;http://x\x1b\\link\x1b]8;;\x1b\\"),
"link"
);
assert_eq!(strip_ansi("no escapes é"), "no escapes é");
assert_eq!(strip_ansi("tail\x1b[3"), "tail");
}
#[test]
fn progress_redraws_keep_only_the_last_state() {
assert_eq!(collapse_redraws("10%\r50%\r100%\ndone\n"), "100%\ndone\n");
assert_eq!(collapse_redraws("a\r\nb\r\n"), "a\nb\n");
assert_eq!(collapse_redraws("x\r"), "x");
assert_eq!(collapse_redraws("plain\n"), "plain\n");
}
#[test]
fn repeated_lines_fold_with_a_count_from_three_on() {
assert_eq!(squeeze("a\na\nb\n"), "a\na\nb\n");
assert_eq!(squeeze("a\na\na\nb\n"), "a (x3)\nb\n");
assert_eq!(squeeze("x\n".repeat(50).as_str()), "x (x50)\n");
assert_eq!(squeeze("a\nb\na\nb\na\nb\n"), "a\nb\na\nb\na\nb\n");
}
#[test]
fn blank_runs_fold_to_one_and_trailing_blanks_go() {
assert_eq!(squeeze("a\n\n\n\n\nb\n\n\n"), "a\n\nb\n");
assert_eq!(squeeze("a\n \n\t\nb"), "a\n\nb\n");
assert_eq!(squeeze(""), "");
assert_eq!(squeeze("\n\n"), "");
}
#[test]
fn a_long_output_keeps_its_head_and_tail_with_a_marker() {
let text: String = (0..2000).map(|i| format!("line {i}\n")).collect();
let out = squeeze(&text);
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), HEAD_LINES + 1 + TAIL_LINES);
assert_eq!(lines[0], "line 0");
assert_eq!(lines[HEAD_LINES - 1], format!("line {}", HEAD_LINES - 1));
assert_eq!(
lines[HEAD_LINES],
format!("... {} lines omitted ...", 2000 - HEAD_LINES - TAIL_LINES)
);
assert_eq!(*lines.last().unwrap(), "line 1999");
}
#[test]
fn cap_keeps_every_line_as_it_is_and_only_bounds_the_output() {
let text = "a\na\na\n \n \nb \n\n\n";
assert_eq!(cap(text), "a\na\na\n \n \nb \n");
let long: String = (0..1000).map(|i| format!("l{i}\n")).collect();
let out = cap(&long);
assert_eq!(out.lines().count(), HEAD_LINES + 1 + TAIL_LINES);
assert!(out.contains("... 450 lines omitted ..."));
assert!(cap(&format!("{}\n", "z".repeat(MAX_LINE_CHARS + 5))).contains("[5 more chars]"));
}
#[test]
fn an_output_at_the_limit_is_left_whole() {
let text: String = (0..MAX_LINES).map(|i| format!("l{i}\n")).collect();
assert_eq!(squeeze(&text), text);
}
#[test]
fn an_over_long_line_is_cut_with_a_count() {
let long = "x".repeat(MAX_LINE_CHARS + 10);
let out = squeeze(&format!("{long}\n"));
assert!(out.starts_with(&"x".repeat(MAX_LINE_CHARS)));
assert!(out.contains("[10 more chars]"), "{out}");
let wide = "é".repeat(MAX_LINE_CHARS + 1);
assert!(squeeze(&wide).contains("[1 more chars]"));
}
}