pub fn elide_middle(text: &str, width: usize) -> String {
if text.chars().count() <= width {
return text.to_string();
}
if text.contains('/')
&& let Some(elided) = elide_path_components(text, width)
{
return elided;
}
elide_chars(text, width)
}
const REPO_MARKERS: [&str; 2] = [".claude", ".git"];
fn repo_anchor(components: &[&str]) -> Option<usize> {
components
.iter()
.rposition(|c| REPO_MARKERS.contains(c))
.filter(|&marker| marker > 0)
.map(|marker| marker - 1)
}
fn elide_path_components(text: &str, width: usize) -> Option<String> {
let components: Vec<&str> = text.split('/').filter(|c| !c.is_empty()).collect();
let anchor = repo_anchor(&components);
let contiguous = deepest_contiguous(&components, width);
let reaches_repo = |kept: usize| anchor.is_none_or(|a| a >= components.len() - kept);
if let Some((kept, rendered)) = &contiguous
&& reaches_repo(*kept)
{
return Some(rendered.clone());
}
match anchor.and_then(|a| anchored_on_repo(&components, a, width)) {
Some(anchored) => Some(anchored),
None => contiguous.map(|(_, rendered)| rendered),
}
}
fn deepest_contiguous(components: &[&str], width: usize) -> Option<(usize, String)> {
let mut kept = 0usize;
let mut rendered = String::new();
for count in 1..=components.len() {
let tail = components[components.len() - count..].join("/");
let candidate = format!("…/{tail}");
if candidate.chars().count() > width {
break;
}
kept = count;
rendered = candidate;
}
(kept >= 2).then_some((kept, rendered))
}
fn anchored_on_repo(components: &[&str], anchor: usize, width: usize) -> Option<String> {
let repo = components[anchor];
let mut rendered: Option<String> = None;
for count in 1..components.len() - anchor {
let tail = components[components.len() - count..].join("/");
let candidate = format!("…/{repo}/…/{tail}");
if candidate.chars().count() > width {
break;
}
rendered = Some(candidate);
}
rendered.or_else(|| {
let bare = format!("…/{repo}/…");
(bare.chars().count() <= width).then_some(bare)
})
}
fn elide_chars(text: &str, width: usize) -> String {
let chars: Vec<char> = text.chars().collect();
if chars.len() <= width {
return text.to_string();
}
if width <= 1 {
return "…".to_string();
}
let keep = width - 1;
let head = keep.div_ceil(2);
let tail = keep - head;
let mut out: String = chars[..head].iter().collect();
out.push('…');
out.extend(&chars[chars.len() - tail..]);
out
}
pub fn trim_surrounding_blank_lines(s: &str) -> String {
let trimmed_end = s.trim_end();
let mut start = 0usize;
let bytes = trimmed_end.as_bytes();
while start < bytes.len() {
let nl = bytes[start..]
.iter()
.position(|&b| b == b'\n')
.map(|p| start + p);
let line_end = nl.unwrap_or(bytes.len());
let line = &trimmed_end[start..line_end];
if line.trim().is_empty() {
start = match nl {
Some(p) => p + 1,
None => bytes.len(),
};
} else {
break;
}
}
trimmed_end[start..].to_string()
}
pub fn strip_interior_blank_lines(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut first = true;
for line in s.split('\n') {
if line.trim().is_empty() {
continue;
}
if !first {
out.push('\n');
}
out.push_str(line);
first = false;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn elide_middle_keeps_whole_trailing_path_components() {
let path = "/Users/masa/trusty-mpm-projects/bobmatnyc/trusty-tools/.claude/worktrees/agent-af14d84dd34577cee";
let out = elide_middle(path, 54);
assert!(out.chars().count() <= 54, "{out}");
assert!(out.starts_with("…/"), "{out}");
assert!(
out.ends_with("/worktrees/agent-af14d84dd34577cee"),
"the last two components must survive whole: {out}"
);
for segment in out.trim_start_matches("…/").split('/') {
assert!(
segment == "…" || path.split('/').any(|c| c == segment),
"{segment:?} in {out}"
);
}
assert!(out.contains("trusty-tools"), "{out}");
}
#[test]
fn elide_middle_keeps_the_repo_name_at_both_render_budgets() {
let path =
"/private/tmp/q8230/deep/trusty-tools-demo/.claude/worktrees/agent-0123456789abcdef";
for width in [54usize, 60] {
let out = elide_middle(path, width);
assert!(
out.chars().count() <= width,
"must fit {width}: {out} ({})",
out.chars().count()
);
assert!(
out.contains("trusty-tools-demo"),
"the repository name must survive at {width}: {out}"
);
assert!(
out.ends_with("agent-0123456789abcdef"),
"the worktree's own name must survive at {width}: {out}"
);
}
}
#[test]
fn elide_middle_drops_the_tail_before_the_repo_name() {
let path =
"/private/tmp/q8230/deep/trusty-tools-demo/.claude/worktrees/agent-0123456789abcdef";
let out = elide_middle(path, 26);
assert!(out.chars().count() <= 26, "{out}");
assert!(
out.contains("trusty-tools-demo"),
"the repo name outlives the tail: {out}"
);
assert_eq!(out, "…/trusty-tools-demo/…", "{out}");
}
#[test]
fn elide_middle_keeps_a_fitting_path_unchanged() {
let path = "/private/tmp/q8230/repoA";
assert_eq!(elide_middle(path, 60), path);
assert_eq!(elide_middle(path, path.chars().count()), path);
}
#[test]
fn elide_middle_keeps_a_repo_name_that_is_the_leaf() {
let path = "/Users/masa/trusty-mpm-projects/bobmatnyc/trusty-tools";
let out = elide_middle(path, 30);
assert!(out.chars().count() <= 30, "{out}");
assert!(out.ends_with("/trusty-tools"), "{out}");
assert!(!out.contains("/…/"), "no anchor splice is needed: {out}");
let deep = "/private/var/folders/zz/T/very/deeply/nested/scratch/workspace/repoZ";
let out = elide_middle(deep, 40);
assert!(out.chars().count() <= 40, "{out}");
assert!(
out.ends_with("/repoZ"),
"the final component survives: {out}"
);
}
#[test]
fn elide_middle_falls_back_to_character_elision() {
let out = elide_middle("abcdefghijklmnopqrstuvwxyz", 11);
assert_eq!(out.chars().count(), 11, "{out}");
assert_eq!(out, "abcde…vwxyz", "head and tail both kept");
assert!(out.contains('…'), "{out}");
let deep = "/a/averyveryverylongdirectoryname/anotherverylongleafname";
let out = elide_middle(deep, 20);
assert_eq!(out.chars().count(), 20, "{out}");
assert!(out.starts_with("/a/"), "{out}");
assert!(out.ends_with("leafname"), "{out}");
}
#[test]
fn elide_middle_handles_degenerate_widths() {
assert_eq!(elide_middle("/a/b/c", 0), "…");
assert_eq!(elide_middle("/a/b/c", 1), "…");
assert_eq!(elide_middle("short", 99), "short");
}
#[test]
fn trim_surrounding_blank_lines_strips_leading_and_trailing() {
let input = "\n\n \nhello\n\nworld\n\n \n";
let out = trim_surrounding_blank_lines(input);
assert_eq!(out, "hello\n\nworld");
}
#[test]
fn trim_surrounding_blank_lines_preserves_when_no_blanks() {
assert_eq!(trim_surrounding_blank_lines("hi"), "hi");
assert_eq!(trim_surrounding_blank_lines("a\nb"), "a\nb");
}
#[test]
fn trim_surrounding_blank_lines_empty_input() {
assert_eq!(trim_surrounding_blank_lines(""), "");
assert_eq!(trim_surrounding_blank_lines("\n\n \n"), "");
}
#[test]
fn strip_interior_blank_lines_drops_all_blanks() {
let input = "para1\n\n\npara2\n\n\n\npara3";
let out = strip_interior_blank_lines(input);
assert_eq!(out, "para1\npara2\npara3");
}
#[test]
fn strip_interior_blank_lines_drops_single_blank() {
let input = "para1\n\npara2";
assert_eq!(strip_interior_blank_lines(input), "para1\npara2");
}
#[test]
fn strip_interior_blank_lines_treats_whitespace_only_as_blank() {
let input = "a\n \n\t\nb";
assert_eq!(strip_interior_blank_lines(input), "a\nb");
}
}