Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore newlines when writing Bstrs #161

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 100 additions & 13 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,24 +353,111 @@ impl fmt::Display for DebugBuffer {
}
}

fn format_bytes(data: &[u8], f: &mut fmt::Formatter<'_>) -> fmt::Result {
const LINES_MIN_OVERFLOW: usize = 80;
const LINES_MAX_START: usize = 20;
const LINES_MAX_END: usize = 40;
const LINES_MAX_PRINTED: usize = LINES_MAX_START + LINES_MAX_END;

fn format_bytes(data: &[u8], f: &mut impl fmt::Write) -> fmt::Result {
#![allow(clippy::assertions_on_constants)]
const MIN_OVERFLOW: usize = 8192;
const MAX_START: usize = 2048;
const MAX_END: usize = 2048;
const MAX_PRINTED: usize = MAX_START + MAX_END;
assert!(MAX_PRINTED < MIN_OVERFLOW);

if data.len() >= MIN_OVERFLOW {
assert!(LINES_MAX_PRINTED < LINES_MIN_OVERFLOW);

const BYTES_MIN_OVERFLOW: usize = 8192;
const BYTES_MAX_START: usize = 2048;
const BYTES_MAX_END: usize = 2048;
const BYTES_MAX_PRINTED: usize = BYTES_MAX_START + BYTES_MAX_END;
assert!(BYTES_MAX_PRINTED < BYTES_MIN_OVERFLOW);

let lines_total = data.as_bstr().lines().count();
let multiline = 1 < lines_total;

if LINES_MIN_OVERFLOW <= lines_total {
let lines_omitted = lines_total - LINES_MAX_PRINTED;
let start_lines = data.as_bstr().lines().take(LINES_MAX_START);
let end_lines = data.as_bstr().lines().skip(LINES_MAX_START + lines_omitted);
writeln!(f, "<{} lines total>", lines_total)?;
write_debug_bstrs(f, true, start_lines)?;
writeln!(f, "<{} lines omitted>", lines_omitted)?;
write_debug_bstrs(f, true, end_lines)
} else if BYTES_MIN_OVERFLOW <= data.len() {
write!(
f,
"<{} bytes total>{:?}...<{} bytes omitted>...{:?}",
"<{} bytes total>{}",
data.len(),
data[..MAX_START].as_bstr(),
data.len() - MAX_PRINTED,
data[data.len() - MAX_END..].as_bstr(),
)
if multiline { "\n" } else { "" }
)?;
write_debug_bstrs(f, multiline, data[..BYTES_MAX_START].lines())?;
write!(
f,
"<{} bytes omitted>{}",
data.len() - BYTES_MAX_PRINTED,
if multiline { "\n" } else { "" }
)?;
write_debug_bstrs(f, multiline, data[data.len() - BYTES_MAX_END..].lines())
} else {
write!(f, "{:?}", data.as_bstr())
write_debug_bstrs(f, multiline, data.lines())
epage marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn write_debug_bstrs<'a>(
f: &mut impl std::fmt::Write,
multiline: bool,
mut lines: impl Iterator<Item = &'a [u8]>,
) -> std::fmt::Result {
if multiline {
writeln!(f, "```")?;
for line in lines {
let s = format!("{:?}", line.as_bstr());
write!(f, "{}", &s[1..s.len() - 1])?;
if multiline {
epage marked this conversation as resolved.
Show resolved Hide resolved
writeln!(f)?;
}
}
writeln!(f, "```")
} else {
write!(f, "{:?}", lines.next().unwrap_or(&[]).as_bstr())
}
}

#[cfg(test)]
mod test {
use super::{LINES_MAX_END, LINES_MAX_PRINTED, LINES_MAX_START, LINES_MIN_OVERFLOW};

#[test]
fn format_bytes() {
let mut s = String::new();
for i in 0..LINES_MIN_OVERFLOW {
s.push_str(&format!("{}\n", i));
}

let mut buf = String::new();
super::format_bytes(s.as_bytes(), &mut buf).unwrap();

let lines_omitted = LINES_MIN_OVERFLOW - LINES_MAX_PRINTED;

let mut lines = buf.lines();
assert_eq!(
format!("<{} lines total>", LINES_MIN_OVERFLOW),
lines.next().unwrap()
);
assert_eq!(format!("```"), lines.next().unwrap());
for i in 0..LINES_MAX_START {
assert_eq!(format!("{}", i), lines.next().unwrap());
}
assert_eq!(format!("```"), lines.next().unwrap());
assert_eq!(
format!("<{} lines omitted>", lines_omitted),
lines.next().unwrap()
);
assert_eq!(format!("```"), lines.next().unwrap());
for i in 0..LINES_MAX_END {
assert_eq!(
format!("{}", LINES_MAX_START + lines_omitted + i),
lines.next().unwrap()
);
}
assert_eq!(format!("```"), lines.next().unwrap());
assert_eq!(None, lines.next());
}
}