MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / truncate

Function truncate

crates/opencode-tui/src/ui/text.rs:4–25  ·  view source on GitHub ↗
(text: &str, max_width: usize)

Source from the content-addressed store, hash-verified

2use unicode_width::UnicodeWidthStr;
3
4pub fn truncate(text: &str, max_width: usize) -> String {
5 let width = UnicodeWidthStr::width(text);
6 if width <= max_width {
7 return text.to_string();
8 }
9
10 let mut result = String::new();
11 let mut current_width = 0;
12
13 for ch in text.chars() {
14 let ch_str = ch.to_string();
15 let ch_width = UnicodeWidthStr::width(ch_str.as_str());
16 if current_width + ch_width + 3 > max_width {
17 break;
18 }
19 result.push(ch);
20 current_width += ch_width;
21 }
22
23 result.push_str("...");
24 result
25}
26
27pub fn pad_left(text: &str, width: usize) -> String {
28 let text_width = text.width();

Callers

nothing calls this directly

Calls 2

newFunction · 0.85
as_strMethod · 0.45

Tested by

no test coverage detected