Skip to main content

presentar_terminal/widgets/
ux.rs

1//! UX Utilities for presentar widgets.
2//!
3//! Implements requirements from lltop UX falsification checklist:
4//! - UX-001: Text truncation with ellipsis
5//! - UX-002: Health status indicators
6//! - UX-003: Empty state widget
7
8use std::borrow::Cow;
9
10// ============================================================================
11// UX-001: Text Truncation
12// ============================================================================
13
14/// Truncate text with ellipsis when it exceeds max characters.
15///
16/// # Examples
17/// ```
18/// use presentar_terminal::widgets::truncate;
19/// assert_eq!(truncate("Hello World", 8), "Hello W…");
20/// assert_eq!(truncate("Short", 10), "Short");
21/// ```
22#[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
37/// Truncate text from the middle, preserving start and end.
38///
39/// Useful for file paths: `/home/user/very/long/path` -> `/hom…ng/path`
40///
41/// # Examples
42/// ```
43/// use presentar_terminal::widgets::truncate_middle;
44/// // 25 char input, max 15: start=4 "/hom", end=10 "ects/myapp"
45/// assert_eq!(truncate_middle("/home/user/projects/myapp", 15), "/hom…ects/myapp");
46/// ```
47pub 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    // Split: keep more of the end (filename usually more important)
57    let start_len = (max - 1) / 3; // ~1/3 for start
58    let end_len = max - 1 - start_len; // ~2/3 for end
59
60    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
66/// Truncate text with custom ellipsis string.
67pub 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// ============================================================================
82// UX-002: Health Status Indicators
83// ============================================================================
84
85/// Health status for visual indicators.
86///
87/// Uses distinct Unicode symbols for accessibility:
88/// - Healthy: ✓ (check mark)
89/// - Warning: ⚠ (warning sign)
90/// - Critical: ✗ (x mark)
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92pub enum HealthStatus {
93    /// System is healthy - displays ✓
94    Healthy,
95    /// System has warnings - displays ⚠
96    Warning,
97    /// System is critical - displays ✗
98    Critical,
99    /// Status unknown - displays ?
100    Unknown,
101}
102
103impl HealthStatus {
104    /// Get the Unicode symbol for this status.
105    #[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    /// Get a colored symbol (ANSI escape codes).
116    /// Returns symbol with appropriate color prefix.
117    pub fn colored_symbol(&self) -> &'static str {
118        match self {
119            Self::Healthy => "\x1b[32m✓\x1b[0m",  // Green
120            Self::Warning => "\x1b[33m⚠\x1b[0m",  // Yellow
121            Self::Critical => "\x1b[31m✗\x1b[0m", // Red
122            Self::Unknown => "\x1b[90m?\x1b[0m",  // Gray
123        }
124    }
125
126    /// Get the label for this status.
127    #[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    /// Create from a percentage (0-100).
138    /// - >= 80%: Healthy
139    /// - >= 50%: Warning
140    /// - < 50%: Critical
141    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    /// Create from a score and maximum.
152    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// ============================================================================
168// UX-003: Empty State Widget
169// ============================================================================
170
171/// Empty state display for panes with no data.
172///
173/// Renders a centered message with:
174/// - Optional icon (emoji or Unicode)
175/// - Title text
176/// - Action hint (how to get data)
177///
178/// # Example
179/// ```
180/// use presentar_terminal::widgets::EmptyState;
181///
182/// let empty = EmptyState::new("No traces available")
183///     .icon("📊")
184///     .hint("Enable tracing with --trace flag");
185/// ```
186#[derive(Debug, Clone)]
187pub struct EmptyState {
188    /// Icon to display (emoji or Unicode symbol)
189    pub icon: Option<String>,
190    /// Main message title
191    pub title: String,
192    /// Action hint for user
193    pub hint: Option<String>,
194    /// Whether to center vertically
195    pub center_vertical: bool,
196}
197
198impl EmptyState {
199    /// Create a new empty state with title.
200    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    /// Add an icon.
210    pub fn icon(mut self, icon: impl Into<String>) -> Self {
211        self.icon = Some(icon.into());
212        self
213    }
214
215    /// Add an action hint.
216    pub fn hint(mut self, hint: impl Into<String>) -> Self {
217        self.hint = Some(hint.into());
218        self
219    }
220
221    /// Disable vertical centering.
222    pub fn top_aligned(mut self) -> Self {
223        self.center_vertical = false;
224        self
225    }
226
227    /// Render to lines for display.
228    ///
229    /// Returns lines that should be rendered, with the starting y offset
230    /// for vertical centering.
231    pub fn render_lines(&self, available_height: u16) -> (Vec<String>, u16) {
232        let mut lines = Vec::new();
233
234        // Add icon line
235        if let Some(ref icon) = self.icon {
236            lines.push(icon.clone());
237            lines.push(String::new()); // Spacer
238        }
239
240        // Add title
241        lines.push(self.title.clone());
242
243        // Add hint
244        if let Some(ref hint) = self.hint {
245            lines.push(String::new()); // Spacer
246            lines.push(hint.clone());
247        }
248
249        // Calculate y offset for centering
250        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 // Small top margin
259        };
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// ============================================================================
272// Tests
273// ============================================================================
274
275#[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        // 28 char input -> max 15: start=4 "/hom", end=10 "th/file.rs"
302        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); // icon, spacer, title, spacer, hint
342        assert!(offset > 0); // Should be centered
343    }
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        // Test with multi-byte Unicode characters
355        assert_eq!(truncate("你好世界", 3), "你好…");
356        assert_eq!(truncate("日本語", 5), "日本語");
357    }
358
359    #[test]
360    fn test_truncate_middle_short() {
361        // Short string - no truncation needed
362        assert_eq!(truncate_middle("abc", 10), "abc");
363        // Very short max - falls back to truncate
364        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        // Ellipsis longer than max
373        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        // Just test that colored symbols contain ANSI codes
392        let healthy = HealthStatus::Healthy.colored_symbol();
393        assert!(healthy.contains("\x1b[32m")); // Green
394        assert!(healthy.contains("✓"));
395
396        let warning = HealthStatus::Warning.colored_symbol();
397        assert!(warning.contains("\x1b[33m")); // Yellow
398        assert!(warning.contains("⚠"));
399
400        let critical = HealthStatus::Critical.colored_symbol();
401        assert!(critical.contains("\x1b[31m")); // Red
402        assert!(critical.contains("✗"));
403
404        let unknown = HealthStatus::Unknown.colored_symbol();
405        assert!(unknown.contains("\x1b[90m")); // Gray
406        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); // Only title
431        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); // icon, spacer, title
439        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); // title, spacer, hint
449        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); // Height smaller than content
458        assert_eq!(lines.len(), 5);
459        assert_eq!(offset, 0); // Can't center, content is bigger
460    }
461
462    #[test]
463    fn test_truncate_middle_exact_boundary() {
464        // Test exactly 4 chars which is the boundary (max <= 3 falls back)
465        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        // Test exact boundaries
472        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}