pub(crate) const FLOOR: usize = 16;
pub(crate) fn middle(s: &str, max: usize) -> String {
let max = max.max(FLOOR);
let total = s.chars().count();
if total <= max {
return s.to_owned();
}
let keep = max - 1;
let tail_len = keep / 2;
let head_len = keep - tail_len;
let head: String = s.chars().take(head_len).collect();
let tail: String = s.chars().skip(total - tail_len).collect();
format!("{head}…{tail}")
}
#[cfg(test)]
mod tests {
use super::{FLOOR, middle};
#[test]
fn a_machine_string_keeps_the_verb_and_the_thing_that_tells_the_row_apart() {
let argv = "lernie prompt --name growing \
/home/u/.cache/yog-drive/quality-20260807T214407Z/data/yog/workspaces/home \
20260807T214551Z-2a1181a3";
let cut = middle(argv, 100);
assert_eq!(cut.chars().count(), 100, "the cut honours its cap: {cut}");
assert!(cut.starts_with("lernie prompt --name growing"), "{cut}");
assert!(cut.ends_with("20260807T214551Z-2a1181a3"), "{cut}");
assert!(cut.contains('…'), "the cut is marked: {cut}");
assert!(
!cut.contains("/quality-20260807T214407Z/data/"),
"the invariant run is what went: {cut}"
);
}
#[test]
fn two_rows_sharing_a_prefix_do_not_cut_to_the_same_string() {
let prefix = "/home/u/.cache/yog-drive/quality-20260807T214407Z/data/yog/workspaces/";
let a = middle(&format!("{prefix}home-alpha"), 40);
let b = middle(&format!("{prefix}home-beta"), 40);
assert_ne!(a, b, "the rows must not collapse to one string");
}
#[test]
fn a_string_within_the_cap_passes_through_whole() {
assert_eq!(middle("lernie stop", 100), "lernie stop");
let exact: String = std::iter::repeat_n('x', 20).collect();
assert_eq!(middle(&exact, 20), exact);
assert_eq!(middle(&exact, 19).chars().count(), 19);
}
#[test]
fn a_cap_below_the_floor_is_raised_to_the_floor() {
let long: String = std::iter::repeat_n('x', 200).collect();
assert_eq!(middle(&long, 0).chars().count(), FLOOR);
assert_eq!(middle(&long, 3).chars().count(), FLOOR);
assert_eq!(middle(&long, FLOOR).chars().count(), FLOOR);
}
#[test]
fn the_cut_lands_on_character_boundaries() {
let wide = "→ мойка ✉ 20260807T214551Z-2a1181a3 · окончание вычисления · home-δ";
let cut = middle(wide, 30);
assert_eq!(cut.chars().count(), 30, "{cut}");
assert!(cut.ends_with("home-δ"), "{cut}");
}
}