syncable_cli/analyzer/display/
utils.rs1pub fn visual_width(s: &str) -> usize {
5 let mut width = 0;
6 let mut chars = s.chars().peekable();
7
8 while let Some(ch) = chars.next() {
9 if ch == '\x1b' {
10 if chars.peek() == Some(&'[') {
12 chars.next(); for c in chars.by_ref() {
14 if c.is_ascii_alphabetic() {
15 break; }
17 }
18 }
19 } else {
20 width += char_width(ch);
23 }
24 }
25
26 width
27}
28
29pub fn char_width(ch: char) -> usize {
31 match ch {
32 '\u{0000}'..='\u{001F}' | '\u{007F}' => 0,
34 '\u{0300}'..='\u{036F}' => 0,
36 '\u{2600}'..='\u{26FF}' | '\u{2700}'..='\u{27BF}' | '\u{1F000}'..='\u{1F02F}' | '\u{1F030}'..='\u{1F09F}' | '\u{1F0A0}'..='\u{1F0FF}' | '\u{1F100}'..='\u{1F1FF}' | '\u{1F200}'..='\u{1F2FF}' | '\u{1F300}'..='\u{1F5FF}' | '\u{1F600}'..='\u{1F64F}' | '\u{1F650}'..='\u{1F67F}' | '\u{1F680}'..='\u{1F6FF}' | '\u{1F700}'..='\u{1F77F}' | '\u{1F780}'..='\u{1F7FF}' | '\u{1F800}'..='\u{1F8FF}' | '\u{1F900}'..='\u{1F9FF}' | '\u{1100}'..='\u{115F}' | '\u{2E80}'..='\u{2EFF}' | '\u{2F00}'..='\u{2FDF}' | '\u{2FF0}'..='\u{2FFF}' | '\u{3000}'..='\u{303E}' | '\u{3041}'..='\u{3096}' | '\u{30A1}'..='\u{30FA}' | '\u{3105}'..='\u{312D}' | '\u{3131}'..='\u{318E}' | '\u{3190}'..='\u{31BA}' | '\u{31C0}'..='\u{31E3}' | '\u{31F0}'..='\u{31FF}' | '\u{3200}'..='\u{32FF}' | '\u{3300}'..='\u{33FF}' | '\u{3400}'..='\u{4DBF}' | '\u{4E00}'..='\u{9FFF}' | '\u{A000}'..='\u{A48C}' | '\u{A490}'..='\u{A4C6}' | '\u{AC00}'..='\u{D7AF}' | '\u{F900}'..='\u{FAFF}' | '\u{FE10}'..='\u{FE19}' | '\u{FE30}'..='\u{FE6F}' | '\u{FF00}'..='\u{FF60}' | '\u{FFE0}'..='\u{FFE6}' => 2,
77 _ => 1,
79 }
80}
81
82pub fn truncate_to_width(s: &str, max_width: usize) -> String {
84 let current_visual_width = visual_width(s);
85 if current_visual_width <= max_width {
86 return s.to_string();
87 }
88
89 if s.contains('\x1b') {
91 let stripped = strip_ansi_codes(s);
93 if visual_width(&stripped) <= max_width {
94 return s.to_string();
95 }
96
97 let mut result = String::new();
99 let mut width = 0;
100 for ch in stripped.chars() {
101 let ch_width = char_width(ch);
102 if width + ch_width > max_width.saturating_sub(3) {
103 result.push_str("...");
104 break;
105 }
106 result.push(ch);
107 width += ch_width;
108 }
109 return result;
110 }
111
112 let mut result = String::new();
114 let mut width = 0;
115
116 for ch in s.chars() {
117 let ch_width = char_width(ch);
118 if width + ch_width > max_width.saturating_sub(3) {
119 result.push_str("...");
120 break;
121 }
122 result.push(ch);
123 width += ch_width;
124 }
125
126 result
127}
128
129pub fn get_terminal_width() -> usize {
131 term_size::dimensions().map(|(w, _)| w).unwrap_or(100)
132}
133
134pub fn smart_truncate(s: &str, max_width: usize) -> String {
136 let current_width = visual_width(s);
137 if current_width <= max_width {
138 return s.to_string();
139 }
140
141 let mut result = String::new();
143 let mut width = 0;
144 let target_width = max_width.saturating_sub(1); for ch in strip_ansi_codes(s).chars() {
147 let ch_width = char_width(ch);
148 if width + ch_width > target_width {
149 break;
150 }
151 result.push(ch);
152 width += ch_width;
153 }
154 result.push('…');
155 result
156}
157
158pub fn format_ports_smart(ports: &[u16], max_show: usize) -> String {
160 if ports.is_empty() {
161 return "-".to_string();
162 }
163
164 let mut unique_ports: Vec<u16> = ports.to_vec();
166 unique_ports.sort_unstable();
167 unique_ports.dedup();
168
169 if unique_ports.len() <= max_show {
170 unique_ports
171 .iter()
172 .map(|p| p.to_string())
173 .collect::<Vec<_>>()
174 .join(", ")
175 } else {
176 let shown: Vec<String> = unique_ports
177 .iter()
178 .take(max_show)
179 .map(|p| p.to_string())
180 .collect();
181 let remaining = unique_ports.len() - max_show;
182 format!("{} +{}", shown.join(", "), remaining)
183 }
184}
185
186pub fn format_list_smart(items: &[String], max_show: usize, max_item_width: usize) -> String {
189 if items.is_empty() {
190 return "-".to_string();
191 }
192
193 let mut seen = std::collections::HashSet::new();
195 let unique: Vec<&String> = items
196 .iter()
197 .filter(|item| seen.insert(item.as_str()))
198 .collect();
199
200 if unique.len() <= max_show {
201 unique
202 .iter()
203 .map(|s| {
204 if visual_width(s) > max_item_width {
205 smart_truncate(s, max_item_width)
206 } else {
207 s.to_string()
208 }
209 })
210 .collect::<Vec<_>>()
211 .join(", ")
212 } else {
213 let shown: Vec<String> = unique
214 .iter()
215 .take(max_show)
216 .map(|s| {
217 if visual_width(s) > max_item_width {
218 smart_truncate(s, max_item_width)
219 } else {
220 s.to_string()
221 }
222 })
223 .collect();
224 let remaining = unique.len() - max_show;
225 format!("{} +{}", shown.join(", "), remaining)
226 }
227}
228
229pub fn strip_ansi_codes(s: &str) -> String {
231 let mut result = String::new();
232 let mut chars = s.chars().peekable();
233
234 while let Some(ch) = chars.next() {
235 if ch == '\x1b' {
236 if chars.peek() == Some(&'[') {
238 chars.next(); for c in chars.by_ref() {
240 if c.is_ascii_alphabetic() {
241 break; }
243 }
244 }
245 } else {
246 result.push(ch);
247 }
248 }
249
250 result
251}
252
253#[cfg(test)]
254mod tests {
255 use super::*;
256
257 #[test]
258 fn test_visual_width_basic() {
259 assert_eq!(visual_width("hello"), 5);
260 assert_eq!(visual_width(""), 0);
261 assert_eq!(visual_width("123"), 3);
262 }
263
264 #[test]
265 fn test_visual_width_with_ansi() {
266 assert_eq!(visual_width("\x1b[31mhello\x1b[0m"), 5);
267 assert_eq!(visual_width("\x1b[1;32mtest\x1b[0m"), 4);
268 }
269
270 #[test]
271 fn test_truncate_to_width() {
272 assert_eq!(truncate_to_width("hello world", 5), "he...");
273 assert_eq!(truncate_to_width("hello", 10), "hello");
274 assert_eq!(truncate_to_width("hello world", 8), "hello...");
275 }
276
277 #[test]
278 fn test_strip_ansi_codes() {
279 assert_eq!(strip_ansi_codes("\x1b[31mhello\x1b[0m"), "hello");
280 assert_eq!(strip_ansi_codes("plain text"), "plain text");
281 assert_eq!(
282 strip_ansi_codes("\x1b[1;32mgreen\x1b[0m text"),
283 "green text"
284 );
285 }
286}