-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtruncate.rs
More file actions
218 lines (200 loc) · 6.57 KB
/
truncate.rs
File metadata and controls
218 lines (200 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Text truncation utilities.
//!
//! Provides centralized truncation functions used across the codebase.
use std::borrow::Cow;
/// Truncates a string to a maximum length, adding ellipsis if truncated.
///
/// # Arguments
/// * `s` - The string to truncate
/// * `max_len` - Maximum length (including ellipsis)
///
/// # Returns
/// The truncated string with "..." appended if truncation occurred.
///
/// # Examples
/// ```
/// use cortex_common::truncate::truncate_with_ellipsis;
///
/// assert_eq!(truncate_with_ellipsis("hello", 10), "hello");
/// assert_eq!(truncate_with_ellipsis("hello world", 8), "hello...");
/// ```
pub fn truncate_with_ellipsis(s: &str, max_len: usize) -> Cow<'_, str> {
if s.chars().count() <= max_len {
Cow::Borrowed(s)
} else {
let truncated: String = s.chars().take(max_len.saturating_sub(3)).collect();
Cow::Owned(format!("{}...", truncated))
}
}
/// Truncates a string to a maximum length, adding unicode ellipsis (…) if truncated.
///
/// # Arguments
/// * `s` - The string to truncate
/// * `max_len` - Maximum character count (including ellipsis)
///
/// # Returns
/// The truncated string with "…" appended if truncation occurred.
///
/// # Examples
/// ```
/// use cortex_common::truncate::truncate_with_unicode_ellipsis;
///
/// assert_eq!(truncate_with_unicode_ellipsis("hello", 10), "hello");
/// assert_eq!(truncate_with_unicode_ellipsis("hello world", 6), "hello…");
/// ```
pub fn truncate_with_unicode_ellipsis(s: &str, max_len: usize) -> Cow<'_, str> {
let char_count = s.chars().count();
if char_count <= max_len {
Cow::Borrowed(s)
} else {
let truncated: String = s.chars().take(max_len.saturating_sub(1)).collect();
Cow::Owned(format!("{}…", truncated))
}
}
/// Truncates an ID string to show first N characters with ellipsis.
///
/// Commonly used for displaying tool call IDs, session IDs, etc.
///
/// # Arguments
/// * `id` - The ID string to truncate
/// * `max_len` - Maximum length (default 10 if called with truncate_id_default)
///
/// # Returns
/// The truncated ID with "…" appended if truncation occurred.
pub fn truncate_id(id: &str, max_len: usize) -> Cow<'_, str> {
truncate_with_unicode_ellipsis(id, max_len)
}
/// Truncates an ID with default max length of 10.
pub fn truncate_id_default(id: &str) -> Cow<'_, str> {
truncate_id(id, 10)
}
/// Truncates text to a maximum length, taking only the first line.
///
/// Useful for displaying task descriptions, messages, etc.
///
/// # Arguments
/// * `text` - The text to truncate
/// * `max_len` - Maximum character count (including ellipsis)
///
/// # Returns
/// The first line truncated with "…" if it exceeds max_len.
pub fn truncate_first_line(text: &str, max_len: usize) -> Cow<'_, str> {
let first_line = text.lines().next().unwrap_or(text);
truncate_with_unicode_ellipsis(first_line, max_len)
}
/// Truncates a command string for display, preserving important parts.
///
/// # Arguments
/// * `command` - The command to truncate
/// * `max_len` - Maximum length
///
/// # Returns
/// The truncated command with "..." appended if truncation occurred.
pub fn truncate_command(command: &str, max_len: usize) -> Cow<'_, str> {
if command.len() <= max_len {
Cow::Borrowed(command)
} else {
// Take the first part up to max_len - 3 (for "...")
let truncated = &command[..max_len.saturating_sub(3).min(command.len())];
// Find last space to avoid cutting in middle of word
if let Some(last_space) = truncated.rfind(' ') {
Cow::Owned(format!("{}...", &truncated[..last_space]))
} else {
Cow::Owned(format!("{}...", truncated))
}
}
}
/// Truncates a string for display in UI widgets.
///
/// # Arguments
/// * `s` - The string to truncate
/// * `max_len` - Maximum character width
///
/// # Returns
/// The truncated string for display.
pub fn truncate_for_display(s: &str, max_len: usize) -> Cow<'_, str> {
truncate_with_ellipsis(s, max_len)
}
/// Truncates a model name for display in compact spaces.
///
/// # Arguments
/// * `name` - The model name
/// * `max_len` - Maximum length
///
/// # Returns
/// The truncated model name.
pub fn truncate_model_name(name: &str, max_len: usize) -> Cow<'_, str> {
if name.len() <= max_len {
Cow::Borrowed(name)
} else {
// Try to keep the model family prefix if possible
if let Some(slash_pos) = name.find('/') {
let prefix = &name[..slash_pos + 1];
if prefix.len() < max_len.saturating_sub(5) {
let remaining = max_len - prefix.len() - 3;
let suffix_start = name.len().saturating_sub(remaining);
return Cow::Owned(format!("{}...{}", prefix, &name[suffix_start..]));
}
}
truncate_with_ellipsis(name, max_len)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_truncate_with_ellipsis_short() {
assert_eq!(truncate_with_ellipsis("short", 10).as_ref(), "short");
}
#[test]
fn test_truncate_with_ellipsis_exact() {
assert_eq!(truncate_with_ellipsis("exactlen", 8).as_ref(), "exactlen");
}
#[test]
fn test_truncate_with_ellipsis_long() {
assert_eq!(
truncate_with_ellipsis("this is a long string", 10).as_ref(),
"this is..."
);
}
#[test]
fn test_truncate_with_unicode_ellipsis() {
assert_eq!(
truncate_with_unicode_ellipsis("hello", 10).as_ref(),
"hello"
);
assert_eq!(
truncate_with_unicode_ellipsis("hello world", 6).as_ref(),
"hello…"
);
}
#[test]
fn test_truncate_id() {
assert_eq!(truncate_id("bg-123456789", 10).as_ref(), "bg-123456…");
assert_eq!(truncate_id("bg-1", 10).as_ref(), "bg-1");
}
#[test]
fn test_truncate_first_line() {
assert_eq!(truncate_first_line("line1\nline2", 20).as_ref(), "line1");
assert_eq!(
truncate_first_line("very long first line", 10).as_ref(),
"very long…"
);
}
#[test]
fn test_truncate_command() {
assert_eq!(truncate_command("ls -la", 20).as_ref(), "ls -la");
assert_eq!(
truncate_command("npm install --save-dev typescript", 20).as_ref(),
"npm install..."
);
}
#[test]
fn test_truncate_model_name() {
assert_eq!(truncate_model_name("gpt-4", 10).as_ref(), "gpt-4");
assert_eq!(
truncate_model_name("anthropic/claude-3-opus-20240229", 20).as_ref(),
"anthropic/...20240229"
);
}
}