1pub fn display_fixed_width(value: &str, max_chars: usize) -> String {
7 let normalized = value.split_whitespace().collect::<Vec<_>>().join(" ");
8 let chars: Vec<char> = normalized.chars().collect();
9
10 if chars.len() <= max_chars {
11 return normalized;
12 }
13
14 if max_chars == 0 {
15 return String::new();
16 }
17
18 if max_chars == 1 {
19 return "…".to_owned();
20 }
21
22 let mut trimmed: String = chars.into_iter().take(max_chars.saturating_sub(1)).collect();
23 trimmed.push('…');
24 trimmed
25}
26
27#[cfg(not(target_arch = "wasm32"))]
28pub mod git_branch;
29#[cfg(not(target_arch = "wasm32"))]
30mod interactive;
31#[cfg(not(target_arch = "wasm32"))]
32pub use interactive::*;
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[rstest::rstest]
39 #[case("hello world", 20, "hello world")]
40 #[case("abcdefghijklmnopqrstuvwxyz", 5, "abcd…")]
41 #[case("abc", 1, "…")]
42 #[case("abc", 0, "")]
43 fn display_fixed_width_trims_as_expected(#[case] value: &str, #[case] max_chars: usize, #[case] expected: &str) {
44 pretty_assertions::assert_eq!(display_fixed_width(value, max_chars), expected);
45 }
46}