presentar_terminal/widgets/
ux.rs1use std::borrow::Cow;
9
10#[inline]
23pub fn truncate(s: &str, max: usize) -> Cow<'_, str> {
24 let char_count = s.chars().count();
25 if char_count <= max {
26 Cow::Borrowed(s)
27 } else if max == 0 {
28 Cow::Borrowed("")
29 } else if max == 1 {
30 Cow::Borrowed("…")
31 } else {
32 let truncated: String = s.chars().take(max - 1).collect();
33 Cow::Owned(format!("{truncated}…"))
34 }
35}
36
37pub fn truncate_middle(s: &str, max: usize) -> Cow<'_, str> {
48 let char_count = s.chars().count();
49 if char_count <= max {
50 return Cow::Borrowed(s);
51 }
52 if max <= 3 {
53 return truncate(s, max);
54 }
55
56 let start_len = (max - 1) / 3; let end_len = max - 1 - start_len; let start: String = s.chars().take(start_len).collect();
61 let end: String = s.chars().skip(char_count - end_len).collect();
62
63 Cow::Owned(format!("{start}…{end}"))
64}
65
66pub fn truncate_with<'a>(s: &'a str, max: usize, ellipsis: &str) -> Cow<'a, str> {
68 let char_count = s.chars().count();
69 let ellipsis_len = ellipsis.chars().count();
70
71 if char_count <= max {
72 Cow::Borrowed(s)
73 } else if max <= ellipsis_len {
74 Cow::Owned(ellipsis.chars().take(max).collect())
75 } else {
76 let truncated: String = s.chars().take(max - ellipsis_len).collect();
77 Cow::Owned(format!("{truncated}{ellipsis}"))
78 }
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92pub enum HealthStatus {
93 Healthy,
95 Warning,
97 Critical,
99 Unknown,
101}
102
103impl HealthStatus {
104 #[inline]
106 pub const fn symbol(&self) -> &'static str {
107 match self {
108 Self::Healthy => "✓",
109 Self::Warning => "⚠",
110 Self::Critical => "✗",
111 Self::Unknown => "?",
112 }
113 }
114
115 pub fn colored_symbol(&self) -> &'static str {
118 match self {
119 Self::Healthy => "\x1b[32m✓\x1b[0m", Self::Warning => "\x1b[33m⚠\x1b[0m", Self::Critical => "\x1b[31m✗\x1b[0m", Self::Unknown => "\x1b[90m?\x1b[0m", }
124 }
125
126 #[inline]
128 pub const fn label(&self) -> &'static str {
129 match self {
130 Self::Healthy => "Healthy",
131 Self::Warning => "Warning",
132 Self::Critical => "Critical",
133 Self::Unknown => "Unknown",
134 }
135 }
136
137 pub fn from_percentage(pct: f64) -> Self {
142 if pct >= 80.0 {
143 Self::Healthy
144 } else if pct >= 50.0 {
145 Self::Warning
146 } else {
147 Self::Critical
148 }
149 }
150
151 pub fn from_score(score: u32, max: u32) -> Self {
153 if max == 0 {
154 return Self::Unknown;
155 }
156 let pct = (score as f64 / max as f64) * 100.0;
157 Self::from_percentage(pct)
158 }
159}
160
161impl std::fmt::Display for HealthStatus {
162 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163 write!(f, "{}", self.symbol())
164 }
165}
166
167#[derive(Debug, Clone)]
187pub struct EmptyState {
188 pub icon: Option<String>,
190 pub title: String,
192 pub hint: Option<String>,
194 pub center_vertical: bool,
196}
197
198impl EmptyState {
199 pub fn new(title: impl Into<String>) -> Self {
201 Self {
202 icon: None,
203 title: title.into(),
204 hint: None,
205 center_vertical: true,
206 }
207 }
208
209 pub fn icon(mut self, icon: impl Into<String>) -> Self {
211 self.icon = Some(icon.into());
212 self
213 }
214
215 pub fn hint(mut self, hint: impl Into<String>) -> Self {
217 self.hint = Some(hint.into());
218 self
219 }
220
221 pub fn top_aligned(mut self) -> Self {
223 self.center_vertical = false;
224 self
225 }
226
227 pub fn render_lines(&self, available_height: u16) -> (Vec<String>, u16) {
232 let mut lines = Vec::new();
233
234 if let Some(ref icon) = self.icon {
236 lines.push(icon.clone());
237 lines.push(String::new()); }
239
240 lines.push(self.title.clone());
242
243 if let Some(ref hint) = self.hint {
245 lines.push(String::new()); lines.push(hint.clone());
247 }
248
249 let y_offset = if self.center_vertical {
251 let content_height = lines.len() as u16;
252 if available_height > content_height {
253 (available_height - content_height) / 2
254 } else {
255 0
256 }
257 } else {
258 1 };
260
261 (lines, y_offset)
262 }
263}
264
265impl Default for EmptyState {
266 fn default() -> Self {
267 Self::new("No data available")
268 }
269}
270
271#[cfg(test)]
276mod tests {
277 use super::*;
278
279 #[test]
280 fn test_truncate_short() {
281 assert_eq!(truncate("Hello", 10), "Hello");
282 assert_eq!(truncate("", 5), "");
283 }
284
285 #[test]
286 fn test_truncate_exact() {
287 assert_eq!(truncate("Hello", 5), "Hello");
288 }
289
290 #[test]
291 fn test_truncate_long() {
292 assert_eq!(truncate("Hello World", 8), "Hello W…");
293 assert_eq!(truncate("Hello World", 6), "Hello…");
294 assert_eq!(truncate("Hello World", 1), "…");
295 assert_eq!(truncate("Hello World", 0), "");
296 }
297
298 #[test]
299 fn test_truncate_middle() {
300 assert_eq!(truncate_middle("/home/user/path", 20), "/home/user/path");
301 assert_eq!(
303 truncate_middle("/home/user/long/path/file.rs", 15),
304 "/hom…th/file.rs"
305 );
306 }
307
308 #[test]
309 fn test_health_status_symbol() {
310 assert_eq!(HealthStatus::Healthy.symbol(), "✓");
311 assert_eq!(HealthStatus::Warning.symbol(), "⚠");
312 assert_eq!(HealthStatus::Critical.symbol(), "✗");
313 assert_eq!(HealthStatus::Unknown.symbol(), "?");
314 }
315
316 #[test]
317 fn test_health_from_percentage() {
318 assert_eq!(HealthStatus::from_percentage(100.0), HealthStatus::Healthy);
319 assert_eq!(HealthStatus::from_percentage(80.0), HealthStatus::Healthy);
320 assert_eq!(HealthStatus::from_percentage(79.0), HealthStatus::Warning);
321 assert_eq!(HealthStatus::from_percentage(50.0), HealthStatus::Warning);
322 assert_eq!(HealthStatus::from_percentage(49.0), HealthStatus::Critical);
323 assert_eq!(HealthStatus::from_percentage(0.0), HealthStatus::Critical);
324 }
325
326 #[test]
327 fn test_health_from_score() {
328 assert_eq!(HealthStatus::from_score(20, 20), HealthStatus::Healthy);
329 assert_eq!(HealthStatus::from_score(16, 20), HealthStatus::Healthy);
330 assert_eq!(HealthStatus::from_score(15, 20), HealthStatus::Warning);
331 assert_eq!(HealthStatus::from_score(10, 20), HealthStatus::Warning);
332 assert_eq!(HealthStatus::from_score(9, 20), HealthStatus::Critical);
333 assert_eq!(HealthStatus::from_score(0, 0), HealthStatus::Unknown);
334 }
335
336 #[test]
337 fn test_empty_state_render() {
338 let empty = EmptyState::new("No data").icon("📊").hint("Try refreshing");
339
340 let (lines, offset) = empty.render_lines(20);
341 assert_eq!(lines.len(), 5); assert!(offset > 0); }
344
345 #[test]
346 fn test_empty_state_top_aligned() {
347 let empty = EmptyState::new("No data").top_aligned();
348 let (_, offset) = empty.render_lines(20);
349 assert_eq!(offset, 1);
350 }
351
352 #[test]
353 fn test_truncate_unicode() {
354 assert_eq!(truncate("你好世界", 3), "你好…");
356 assert_eq!(truncate("日本語", 5), "日本語");
357 }
358
359 #[test]
360 fn test_truncate_middle_short() {
361 assert_eq!(truncate_middle("abc", 10), "abc");
363 assert_eq!(truncate_middle("abcdefgh", 3), "ab…");
365 assert_eq!(truncate_middle("abcdefgh", 2), "a…");
366 }
367
368 #[test]
369 fn test_truncate_with_custom_ellipsis() {
370 assert_eq!(truncate_with("Hello World", 10, "..."), "Hello W...");
371 assert_eq!(truncate_with("Hello", 10, "..."), "Hello");
372 assert_eq!(truncate_with("Hello World", 2, "..."), "..");
374 }
375
376 #[test]
377 fn test_truncate_with_empty_ellipsis() {
378 assert_eq!(truncate_with("Hello World", 5, ""), "Hello");
379 }
380
381 #[test]
382 fn test_health_status_label() {
383 assert_eq!(HealthStatus::Healthy.label(), "Healthy");
384 assert_eq!(HealthStatus::Warning.label(), "Warning");
385 assert_eq!(HealthStatus::Critical.label(), "Critical");
386 assert_eq!(HealthStatus::Unknown.label(), "Unknown");
387 }
388
389 #[test]
390 fn test_health_status_colored_symbol() {
391 let healthy = HealthStatus::Healthy.colored_symbol();
393 assert!(healthy.contains("\x1b[32m")); assert!(healthy.contains("✓"));
395
396 let warning = HealthStatus::Warning.colored_symbol();
397 assert!(warning.contains("\x1b[33m")); assert!(warning.contains("⚠"));
399
400 let critical = HealthStatus::Critical.colored_symbol();
401 assert!(critical.contains("\x1b[31m")); assert!(critical.contains("✗"));
403
404 let unknown = HealthStatus::Unknown.colored_symbol();
405 assert!(unknown.contains("\x1b[90m")); assert!(unknown.contains("?"));
407 }
408
409 #[test]
410 fn test_health_status_display() {
411 assert_eq!(format!("{}", HealthStatus::Healthy), "✓");
412 assert_eq!(format!("{}", HealthStatus::Warning), "⚠");
413 assert_eq!(format!("{}", HealthStatus::Critical), "✗");
414 assert_eq!(format!("{}", HealthStatus::Unknown), "?");
415 }
416
417 #[test]
418 fn test_empty_state_default() {
419 let empty = EmptyState::default();
420 assert_eq!(empty.title, "No data available");
421 assert!(empty.icon.is_none());
422 assert!(empty.hint.is_none());
423 assert!(empty.center_vertical);
424 }
425
426 #[test]
427 fn test_empty_state_no_icon_no_hint() {
428 let empty = EmptyState::new("Test message");
429 let (lines, _) = empty.render_lines(10);
430 assert_eq!(lines.len(), 1); assert_eq!(lines[0], "Test message");
432 }
433
434 #[test]
435 fn test_empty_state_with_icon_only() {
436 let empty = EmptyState::new("Test message").icon("🔍");
437 let (lines, _) = empty.render_lines(10);
438 assert_eq!(lines.len(), 3); assert_eq!(lines[0], "🔍");
440 assert_eq!(lines[1], "");
441 assert_eq!(lines[2], "Test message");
442 }
443
444 #[test]
445 fn test_empty_state_with_hint_only() {
446 let empty = EmptyState::new("Test message").hint("Try again");
447 let (lines, _) = empty.render_lines(10);
448 assert_eq!(lines.len(), 3); assert_eq!(lines[0], "Test message");
450 assert_eq!(lines[1], "");
451 assert_eq!(lines[2], "Try again");
452 }
453
454 #[test]
455 fn test_empty_state_render_small_height() {
456 let empty = EmptyState::new("Title").icon("📊").hint("Hint");
457 let (lines, offset) = empty.render_lines(3); assert_eq!(lines.len(), 5);
459 assert_eq!(offset, 0); }
461
462 #[test]
463 fn test_truncate_middle_exact_boundary() {
464 let result = truncate_middle("abcdefghij", 4);
466 assert!(result.len() <= 4 || result.chars().count() <= 4);
467 }
468
469 #[test]
470 fn test_health_from_percentage_edge_cases() {
471 assert_eq!(HealthStatus::from_percentage(80.0), HealthStatus::Healthy);
473 assert_eq!(HealthStatus::from_percentage(79.999), HealthStatus::Warning);
474 assert_eq!(HealthStatus::from_percentage(50.0), HealthStatus::Warning);
475 assert_eq!(
476 HealthStatus::from_percentage(49.999),
477 HealthStatus::Critical
478 );
479 }
480
481 #[test]
482 fn test_empty_state_builder_chain() {
483 let empty = EmptyState::new("Test")
484 .icon("🔧")
485 .hint("Fix it")
486 .top_aligned();
487
488 assert_eq!(empty.title, "Test");
489 assert_eq!(empty.icon, Some("🔧".to_string()));
490 assert_eq!(empty.hint, Some("Fix it".to_string()));
491 assert!(!empty.center_vertical);
492 }
493}