#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Rung {
Strip,
Dedup,
Elide,
Outline,
}
impl Rung {
pub(crate) fn label(self) -> &'static str {
match self {
Self::Strip => "strip",
Self::Dedup => "dedup",
Self::Elide => "elide",
Self::Outline => "outline",
}
}
pub(crate) fn parse(flag: &str) -> Option<Self> {
match flag {
"--strip" => Some(Self::Strip),
"--dedup" => Some(Self::Dedup),
"--elide" => Some(Self::Elide),
"--outline" => Some(Self::Outline),
_ => None,
}
}
pub(crate) fn apply(self, text: &str) -> String {
match self {
Self::Strip => strip(text),
Self::Dedup => dedup(text),
Self::Elide => elide(text),
Self::Outline => outline(text),
}
}
}
fn strip(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for line in text.split_inclusive('\n') {
let ends = line.ends_with('\n');
let bare = strip_ansi(line.trim_end_matches('\n'));
out.push_str(bare.trim_end_matches([' ', '\t']));
if ends {
out.push('\n');
}
}
out
}
fn strip_ansi(line: &str) -> String {
let mut out = String::with_capacity(line.len());
let mut chars = line.chars();
while let Some(c) = chars.next() {
if c != '\u{1b}' {
out.push(c);
continue;
}
for next in chars.by_ref() {
if next.is_ascii_alphabetic() || next == '\u{7}' {
break;
}
}
}
out
}
fn dedup(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut runs: Vec<(&str, u64)> = Vec::new();
for line in text.split_inclusive('\n') {
match runs.last_mut() {
Some((prev, n)) if *prev == line => *n = n.saturating_add(1),
_ => runs.push((line, 1)),
}
}
for (line, n) in runs {
out.push_str(&run_line(line, n));
}
out
}
fn run_line(line: &str, n: u64) -> String {
if n < 2 {
return line.to_owned();
}
let body = line.trim_end_matches('\n');
let tail = if line.ends_with('\n') { "\n" } else { "" };
format!("{body} x{n}{tail}")
}
const BLOB: usize = 200;
fn elide(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for line in text.split_inclusive('\n') {
let body = line.trim_end_matches('\n');
let tail = if line.ends_with('\n') { "\n" } else { "" };
if blob(body) {
let n = body.len();
out.push_str(&format!("<elided: {n} bytes of data>{tail}"));
} else {
out.push_str(line);
}
}
out
}
fn blob(body: &str) -> bool {
if body.len() < BLOB {
return false;
}
let dense = body
.chars()
.filter(|c| c.is_ascii_alphanumeric() || "+/=_-".contains(*c))
.count();
dense.saturating_mul(10) >= body.len().saturating_mul(9)
}
fn outline(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut dropped = 0u64;
for line in text.split_inclusive('\n') {
if keeps(line) {
out.push_str(&flush(dropped));
dropped = 0;
out.push_str(line);
} else {
dropped = dropped.saturating_add(1);
}
}
out.push_str(&flush(dropped));
out
}
fn flush(dropped: u64) -> String {
if dropped == 0 {
return String::new();
}
format!("... {dropped} line(s) elided\n")
}
fn keeps(line: &str) -> bool {
let body = line.trim_end_matches('\n');
body.trim().is_empty() || !body.starts_with([' ', '\t'])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_enum_order_is_the_ladder_lossless_first() {
let mut sorted = [Rung::Outline, Rung::Strip, Rung::Elide, Rung::Dedup];
sorted.sort_unstable();
assert_eq!(
sorted,
[Rung::Strip, Rung::Dedup, Rung::Elide, Rung::Outline]
);
}
#[test]
fn every_rung_parses_from_its_flag_and_names_itself() {
for rung in [Rung::Strip, Rung::Dedup, Rung::Elide, Rung::Outline] {
let flag = format!("--{}", rung.label());
assert_eq!(Rung::parse(&flag), Some(rung));
}
assert_eq!(Rung::parse("--nonesuch"), None);
}
#[test]
fn strip_removes_escapes_and_trailing_space() {
let got = strip("\u{1b}[32mgreen\u{1b}[0m \nplain\t\n");
assert_eq!(got, "green\nplain\n");
}
#[test]
fn strip_keeps_a_final_line_without_a_newline() {
assert_eq!(strip("tail "), "tail");
}
#[test]
fn dedup_collapses_runs_and_keeps_the_count() {
assert_eq!(dedup("a\na\na\nb\n"), "a x3\nb\n");
}
#[test]
fn dedup_only_collapses_neighbours() {
assert_eq!(dedup("a\nb\na\n"), "a\nb\na\n");
}
#[test]
fn elide_replaces_a_long_dense_line_with_its_size() {
let blob = "A".repeat(400);
let got = elide(&format!("keep\n{blob}\n"));
assert!(got.starts_with("keep\n"), "{got}");
assert!(got.contains("<elided: 400 bytes of data>"), "{got}");
}
#[test]
fn elide_leaves_long_prose_alone() {
let prose = "the quick brown fox jumps over the lazy dog. ".repeat(9);
assert_eq!(elide(&prose), prose);
}
#[test]
fn elide_leaves_short_data_alone() {
let short = format!("{}\n", "A".repeat(BLOB - 1));
assert_eq!(elide(&short), short);
}
#[test]
fn outline_keeps_signatures_and_counts_what_it_dropped() {
let code = "fn a() {\n let x = 1;\n x\n}\nfn b() {\n";
let got = outline(code);
assert!(got.contains("fn a() {"), "{got}");
assert!(got.contains("... 2 line(s) elided"), "{got}");
assert!(got.contains("fn b() {"), "{got}");
assert!(!got.contains("let x"), "{got}");
}
#[test]
fn outline_keeps_blank_lines_as_structure() {
assert_eq!(outline("a\n\nb\n"), "a\n\nb\n");
}
#[test]
fn every_rung_is_idempotent() {
let text = "\u{1b}[31mx\u{1b}[0m \nx\nx\n body\nfn f() {\n";
for rung in [Rung::Strip, Rung::Dedup, Rung::Elide, Rung::Outline] {
let once = rung.apply(text);
assert_eq!(
rung.apply(&once),
once,
"{} is not idempotent",
rung.label()
);
}
}
}