MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / truncate_string

Function truncate_string

atomic-cli/src/commands/log/command.rs:596–606  ·  view source on GitHub ↗

Truncate a string to a maximum length, adding ellipsis if needed. This function handles Unicode characters correctly by counting grapheme clusters rather than bytes, ensuring we never split a multi-byte character. # Arguments `s` - The string to truncate `max_len` - Maximum character length including ellipsis # Returns The truncated string. # Example ```rust,ignore assert_eq!(truncate_strin

(s: &str, max_len: usize)

Source from the content-addressed store, hash-verified

594/// assert_eq!(truncate_string("Short", 8), "Short");
595/// ```
596pub fn truncate_string(s: &str, max_len: usize) -> String {
597 let char_count = s.chars().count();
598 if char_count <= max_len {
599 s.to_string()
600 } else if max_len <= 3 {
601 s.chars().take(max_len).collect()
602 } else {
603 let truncated: String = s.chars().take(max_len - 3).collect();
604 format!("{}...", truncated)
605 }
606}
607
608/// Format an author for display from an `Author` struct.
609///

Callers 3

format_onelineMethod · 0.70

Calls 2

countMethod · 0.45
collectMethod · 0.45

Tested by 1