Skip to main content

tea_cli/tui/
render_output.rs

1use ratatui::style::Style;
2use ratatui::text::{Line, Span};
3
4/// One width-bounded terminal row.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct RenderedLine {
7    pub(crate) text: String,
8    pub(crate) style: Style,
9    pub(crate) spans: Vec<RenderedSpan>,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub(crate) struct RenderedSpan {
14    pub(crate) text: String,
15    pub(crate) style: Style,
16    pub(crate) link: Option<String>,
17}
18
19impl RenderedSpan {
20    pub(crate) fn new(text: String, style: Style) -> Self {
21        Self {
22            text,
23            style,
24            link: None,
25        }
26    }
27
28    pub(crate) fn with_link(text: String, style: Style, link: String) -> Self {
29        Self {
30            text,
31            style,
32            link: Some(link),
33        }
34    }
35
36    pub(crate) fn text(&self) -> &str {
37        &self.text
38    }
39
40    pub(crate) const fn style(&self) -> Style {
41        self.style
42    }
43
44    pub(crate) fn link(&self) -> Option<&str> {
45        self.link.as_deref()
46    }
47}
48
49pub(crate) fn push_rendered_span(output: &mut Vec<RenderedSpan>, span: RenderedSpan) {
50    if let Some(last) = output.last_mut()
51        && last.style == span.style
52        && last.link == span.link
53    {
54        last.text.push_str(&span.text);
55    } else {
56        output.push(span);
57    }
58}
59
60impl RenderedLine {
61    /// Creates one renderer-owned terminal row.
62    #[must_use]
63    pub(crate) fn new(text: String, style: Style) -> Self {
64        Self {
65            text,
66            style,
67            spans: Vec::new(),
68        }
69    }
70
71    pub(crate) fn from_spans(style: Style, spans: Vec<RenderedSpan>) -> Self {
72        let text = spans.iter().map(|span| span.text.as_str()).collect();
73        Self { text, style, spans }
74    }
75
76    /// Returns row text.
77    #[must_use]
78    pub fn text(&self) -> &str {
79        &self.text
80    }
81
82    /// Returns semantic row style.
83    #[must_use]
84    pub const fn style(&self) -> Style {
85        self.style
86    }
87
88    pub(crate) fn as_ratatui_line(&self) -> Line<'_> {
89        if self.spans.is_empty() {
90            return Line::styled(self.text.as_str(), self.style);
91        }
92        Line::from(
93            self.spans
94                .iter()
95                .map(|span| Span::styled(span.text.as_str(), span.style))
96                .collect::<Vec<_>>(),
97        )
98    }
99
100    pub(crate) fn rendered_spans(&self) -> &[RenderedSpan] {
101        &self.spans
102    }
103
104    pub(crate) fn with_left_columns(self, columns: usize) -> Self {
105        if columns == 0 {
106            return self;
107        }
108        let mut spans = Vec::with_capacity(self.spans.len().saturating_add(2));
109        spans.push(RenderedSpan::new(" ".repeat(columns), Style::default()));
110        if self.spans.is_empty() {
111            spans.push(RenderedSpan::new(self.text, self.style));
112        } else {
113            spans.extend(self.spans);
114        }
115        Self::from_spans(Style::default(), spans)
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use ratatui::style::{Color, Style};
122
123    use super::{RenderedLine, RenderedSpan, push_rendered_span};
124
125    #[test]
126    fn adjacent_spans_merge_only_when_style_and_link_match() {
127        let style = Style::default().fg(Color::Green);
128        let other_style = Style::default().fg(Color::Red);
129        let mut spans = Vec::new();
130
131        push_rendered_span(
132            &mut spans,
133            RenderedSpan::with_link("a".to_owned(), style, "https://example.com/a".to_owned()),
134        );
135        push_rendered_span(
136            &mut spans,
137            RenderedSpan::with_link("b".to_owned(), style, "https://example.com/a".to_owned()),
138        );
139        push_rendered_span(
140            &mut spans,
141            RenderedSpan::with_link("c".to_owned(), style, "https://example.com/b".to_owned()),
142        );
143        push_rendered_span(&mut spans, RenderedSpan::new("d".to_owned(), other_style));
144
145        assert_eq!(spans.len(), 3);
146        assert_eq!(spans[0].text(), "ab");
147        assert_eq!(spans[0].link(), Some("https://example.com/a"));
148        assert_eq!(spans[1].text(), "c");
149        assert_eq!(spans[1].link(), Some("https://example.com/b"));
150        assert_eq!(spans[2].text(), "d");
151        assert_eq!(spans[2].style(), other_style);
152    }
153
154    #[test]
155    fn prepending_columns_preserves_link_metadata() {
156        let destination = "https://example.com/docs";
157        let style = Style::default().fg(Color::Blue);
158        let line = RenderedLine::from_spans(
159            style,
160            vec![RenderedSpan::with_link(
161                "docs".to_owned(),
162                style,
163                destination.to_owned(),
164            )],
165        )
166        .with_left_columns(2);
167
168        assert_eq!(line.text(), "  docs");
169        assert_eq!(line.rendered_spans().len(), 2);
170        assert_eq!(line.rendered_spans()[0].text(), "  ");
171        assert_eq!(line.rendered_spans()[0].link(), None);
172        assert_eq!(line.rendered_spans()[1].text(), "docs");
173        assert_eq!(line.rendered_spans()[1].link(), Some(destination));
174    }
175}