Skip to main content

rusticity_term/ui/
expanded_view.rs

1use super::labeled_field;
2
3/// Format multiple fields as lines
4pub fn format_fields(fields: &[(&str, &str)]) -> Vec<ratatui::text::Line<'static>> {
5    fields
6        .iter()
7        .map(|(label, value)| labeled_field(&format!("{}: ", label), *value))
8        .collect()
9}
10
11/// Format key-value pairs for expansion (used in table rows)
12pub fn format_expansion_text(fields: &[(&str, String)]) -> Vec<(String, ratatui::prelude::Style)> {
13    fields
14        .iter()
15        .map(|(label, value)| {
16            let display_value = if value.is_empty() { "-" } else { value };
17            (
18                format!("{}: {}", label, display_value),
19                ratatui::prelude::Style::default(),
20            )
21        })
22        .collect()
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn test_labeled_field() {
31        assert_eq!(labeled_field("Name", "test-value").spans.len(), 2);
32    }
33
34    #[test]
35    fn test_labeled_field_empty() {
36        let text: String = labeled_field("Name", "")
37            .spans
38            .iter()
39            .map(|s| s.content.as_ref())
40            .collect();
41        assert!(text.contains("-"));
42    }
43
44    #[test]
45    fn test_format_fields() {
46        assert_eq!(
47            format_fields(&[("Name", "test"), ("Status", "active")]).len(),
48            2
49        );
50    }
51
52    #[test]
53    fn test_format_expansion_text() {
54        let lines = format_expansion_text(&[
55            ("Name", "test".to_string()),
56            ("Status", "active".to_string()),
57        ]);
58        assert_eq!(lines.len(), 2);
59        assert_eq!(lines[0].0, "Name: test");
60        assert_eq!(lines[1].0, "Status: active");
61    }
62
63    #[test]
64    fn test_format_expansion_text_empty() {
65        let lines = format_expansion_text(&[("Name", String::new())]);
66        assert_eq!(lines.len(), 1);
67        assert_eq!(lines[0].0, "Name: -");
68    }
69}