feat(utils): implement string elision trait

- introduce `Elide` trait for string truncation
- add `elide` function to truncate strings longer than a specified length, adding " ..." at the end
This commit is contained in:
Jeremiah Russell
2025-10-06 17:03:06 +01:00
committed by Jeremiah Russell
parent f42718328e
commit 65f8bf7fc5

View File

@@ -36,3 +36,19 @@ pub(crate) fn assure_config_dir_exists(dir: &str) -> Result<String, Error> {
Ok(expanded_config_dir)
}
pub(crate) trait Elide {
fn elide(&mut self, to: usize) -> &mut Self;
}
impl Elide for String {
fn elide(&mut self, to: usize) -> &mut Self {
if self.len() <= to {
self
} else {
let range = to - 4;
self.replace_range(range.., " ...");
self
}
}
}