saorsa-tui 0.4.0

Retained-mode, CSS-styled terminal UI framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Streaming markdown renderer for styled terminal output.
//!
//! Uses [`pulldown_cmark`] to parse CommonMark and produce styled
//! [`Segment`] lines suitable for rendering in a terminal. Designed
//! for incremental rendering of streaming LLM output.

use crate::color::{Color, NamedColor};
use crate::segment::Segment;
use crate::style::Style;
use crate::text::truncate_to_display_width;
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
use unicode_width::UnicodeWidthStr;

/// A block-level element in rendered markdown.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MarkdownBlock {
    /// A paragraph of text.
    Paragraph,
    /// A heading (level 1-6).
    Heading(u8),
    /// A fenced or indented code block with optional language.
    CodeBlock(Option<String>),
    /// A list item with its nesting depth.
    ListItem(usize),
    /// A block quote.
    BlockQuote,
    /// A horizontal rule / thematic break.
    ThematicBreak,
    /// A table.
    Table,
}

/// A stateful incremental markdown renderer.
///
/// Text is pushed in chunks via [`push_str`](MarkdownRenderer::push_str)
/// and the full rendered output is produced by
/// [`render_to_lines`](MarkdownRenderer::render_to_lines). Incomplete
/// markdown is handled gracefully — the renderer re-parses the
/// accumulated text on each call.
pub struct MarkdownRenderer {
    text: String,
}

impl MarkdownRenderer {
    /// Create a new empty markdown renderer.
    pub fn new() -> Self {
        Self {
            text: String::new(),
        }
    }

    /// Append a text chunk (supports streaming).
    pub fn push_str(&mut self, text: &str) {
        self.text.push_str(text);
    }

    /// Clear all accumulated text and reset the renderer.
    pub fn clear(&mut self) {
        self.text.clear();
    }

    /// Render the current accumulated text to styled lines.
    ///
    /// Each line is a `Vec<Segment>`. The text is word-wrapped to
    /// the given width. Styles are applied for headings, bold, italic,
    /// inline code, code blocks, and list items.
    pub fn render_to_lines(&self, width: u16) -> Vec<Vec<Segment>> {
        let w = width as usize;
        if w == 0 || self.text.is_empty() {
            return Vec::new();
        }

        let mut lines: Vec<Vec<Segment>> = Vec::new();
        let mut style_stack: Vec<Style> = vec![Style::default()];
        let mut current_line: Vec<Segment> = Vec::new();
        let mut current_width: usize = 0;
        let mut in_code_block = false;
        let mut list_depth: usize = 0;
        let mut in_list_item = false;

        let opts = Options::empty();
        let parser = Parser::new_ext(&self.text, opts);

        for event in parser {
            match event {
                Event::Start(tag) => match tag {
                    Tag::Heading { level, .. } => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        let level_num = level as u8;
                        let heading_style = heading_style(level_num);
                        style_stack.push(heading_style);
                    }
                    Tag::Paragraph => {
                        // Add blank line before paragraph (unless at start)
                        if !lines.is_empty() {
                            lines.push(Vec::new());
                        }
                    }
                    Tag::CodeBlock(kind) => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        in_code_block = true;
                        let _lang = match kind {
                            pulldown_cmark::CodeBlockKind::Fenced(lang) => {
                                let l = lang.to_string();
                                if l.is_empty() { None } else { Some(l) }
                            }
                            pulldown_cmark::CodeBlockKind::Indented => None,
                        };
                        style_stack.push(code_block_style());
                    }
                    Tag::Emphasis => {
                        let base = current_style(&style_stack);
                        style_stack.push(base.italic(true));
                    }
                    Tag::Strong => {
                        let base = current_style(&style_stack);
                        style_stack.push(base.bold(true));
                    }
                    Tag::List(_) => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        list_depth += 1;
                    }
                    Tag::Item => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        in_list_item = true;
                        // Add list marker
                        let indent = "  ".repeat(list_depth.saturating_sub(1));
                        let marker = format!("{indent}- ");
                        let marker_w = UnicodeWidthStr::width(marker.as_str());
                        current_line.push(Segment::new(marker));
                        current_width = marker_w;
                    }
                    Tag::BlockQuote(_) => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        let base = current_style(&style_stack);
                        style_stack.push(base.dim(true));
                    }
                    _ => {}
                },
                Event::End(tag_end) => match tag_end {
                    TagEnd::Heading(_) => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        style_stack.pop();
                    }
                    TagEnd::Paragraph => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                    }
                    TagEnd::CodeBlock => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        in_code_block = false;
                        style_stack.pop();
                    }
                    TagEnd::Emphasis | TagEnd::Strong => {
                        style_stack.pop();
                    }
                    TagEnd::List(_) => {
                        list_depth = list_depth.saturating_sub(1);
                    }
                    TagEnd::Item => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        in_list_item = false;
                    }
                    TagEnd::BlockQuote(_) => {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                        style_stack.pop();
                    }
                    _ => {}
                },
                Event::Text(text) => {
                    let style = current_style(&style_stack);
                    if in_code_block {
                        // Code blocks: render each line as-is (no word wrap)
                        for (i, line) in text.lines().enumerate() {
                            if i > 0 {
                                flush_line(&mut lines, &mut current_line, &mut current_width);
                            }
                            let truncated = truncate_to_display_width(line, w);
                            current_line
                                .push(Segment::styled(truncated.to_string(), style.clone()));
                            current_width += UnicodeWidthStr::width(truncated);
                        }
                        if text.ends_with('\n') {
                            flush_line(&mut lines, &mut current_line, &mut current_width);
                        }
                    } else {
                        // Normal text: word wrap
                        let mut state = WrapState {
                            lines: &mut lines,
                            current_line: &mut current_line,
                            current_width: &mut current_width,
                            in_list_item,
                            list_depth,
                        };
                        wrap_text_into(&text, &style, w, &mut state);
                    }
                }
                Event::Code(code) => {
                    let style = inline_code_style();
                    let code_str = format!("`{code}`");
                    let code_w = UnicodeWidthStr::width(code_str.as_str());
                    if current_width + code_w > w && !current_line.is_empty() {
                        flush_line(&mut lines, &mut current_line, &mut current_width);
                    }
                    current_line.push(Segment::styled(code_str, style));
                    current_width += code_w;
                }
                Event::SoftBreak => {
                    // Treat as space
                    let style = current_style(&style_stack);
                    if current_width < w {
                        current_line.push(Segment::styled(" ".to_string(), style));
                        current_width += 1;
                    }
                }
                Event::HardBreak => {
                    flush_line(&mut lines, &mut current_line, &mut current_width);
                }
                Event::Rule => {
                    flush_line(&mut lines, &mut current_line, &mut current_width);
                    let rule = "".repeat(w.min(80));
                    lines.push(vec![Segment::styled(rule, Style::new().dim(true))]);
                }
                _ => {}
            }
        }

        // Flush remaining content
        flush_line(&mut lines, &mut current_line, &mut current_width);

        lines
    }
}

impl Default for MarkdownRenderer {
    fn default() -> Self {
        Self::new()
    }
}

/// Flush the current line segments into the lines list.
fn flush_line(
    lines: &mut Vec<Vec<Segment>>,
    current_line: &mut Vec<Segment>,
    current_width: &mut usize,
) {
    if !current_line.is_empty() {
        lines.push(std::mem::take(current_line));
    }
    *current_width = 0;
}

/// Get the current style from the style stack.
fn current_style(stack: &[Style]) -> Style {
    stack.last().cloned().unwrap_or_default()
}

/// Style for headings by level.
fn heading_style(level: u8) -> Style {
    match level {
        1 => Style::new().bold(true).fg(Color::Named(NamedColor::Cyan)),
        2 => Style::new().bold(true).fg(Color::Named(NamedColor::Green)),
        3 => Style::new().bold(true).fg(Color::Named(NamedColor::Yellow)),
        _ => Style::new().bold(true),
    }
}

/// Style for inline code.
fn inline_code_style() -> Style {
    Style::new().fg(Color::Named(NamedColor::Yellow))
}

/// Style for code blocks.
fn code_block_style() -> Style {
    Style::new().dim(true)
}

/// Mutable rendering state for word wrapping.
struct WrapState<'a> {
    lines: &'a mut Vec<Vec<Segment>>,
    current_line: &'a mut Vec<Segment>,
    current_width: &'a mut usize,
    in_list_item: bool,
    list_depth: usize,
}

/// Word-wrap text and push segments into the output.
fn wrap_text_into(text: &str, style: &Style, width: usize, state: &mut WrapState<'_>) {
    for word in text.split_whitespace() {
        let word_w = UnicodeWidthStr::width(word);

        // If adding this word would overflow, wrap
        let space_needed = if state.current_line.is_empty() || *state.current_width == 0 {
            0
        } else {
            1
        };

        if *state.current_width + space_needed + word_w > width && !state.current_line.is_empty() {
            flush_line(state.lines, state.current_line, state.current_width);
            // Add indent for continuation lines in list items
            if state.in_list_item {
                let indent = "  ".repeat(state.list_depth.saturating_sub(1));
                let cont_indent = format!("{indent}  ");
                let indent_w = UnicodeWidthStr::width(cont_indent.as_str());
                state.current_line.push(Segment::new(cont_indent));
                *state.current_width = indent_w;
            }
        }

        // Add space before word (unless at start of line)
        if !state.current_line.is_empty() && *state.current_width > 0 {
            state
                .current_line
                .push(Segment::styled(" ".to_string(), style.clone()));
            *state.current_width += 1;
        }

        state
            .current_line
            .push(Segment::styled(word.to_string(), style.clone()));
        *state.current_width += word_w;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn plain_text_renders() {
        let mut r = MarkdownRenderer::new();
        r.push_str("Hello world");
        let lines = r.render_to_lines(80);
        assert!(!lines.is_empty());
        let text: String = lines[0].iter().map(|s| &*s.text).collect();
        assert!(text.contains("Hello"));
        assert!(text.contains("world"));
    }

    #[test]
    fn bold_italic_styled() {
        let mut r = MarkdownRenderer::new();
        r.push_str("**bold** and *italic*");
        let lines = r.render_to_lines(80);
        assert!(!lines.is_empty());
        // Check that bold segment exists
        let has_bold = lines[0].iter().any(|s| s.style.bold);
        assert!(has_bold);
        let has_italic = lines[0].iter().any(|s| s.style.italic);
        assert!(has_italic);
    }

    #[test]
    fn heading_styles() {
        let mut r = MarkdownRenderer::new();
        r.push_str("# Heading 1\n\n## Heading 2\n\n### Heading 3");
        let lines = r.render_to_lines(80);
        assert!(!lines.is_empty());
        // H1 should be bold and cyan
        let h1_line = &lines[0];
        assert!(h1_line.iter().any(|s| s.style.bold));
    }

    #[test]
    fn inline_code_styled() {
        let mut r = MarkdownRenderer::new();
        r.push_str("Use `code` here");
        let lines = r.render_to_lines(80);
        assert!(!lines.is_empty());
        let has_code = lines[0].iter().any(|s| s.text.contains("`code`"));
        assert!(has_code);
    }

    #[test]
    fn code_block_rendered() {
        let mut r = MarkdownRenderer::new();
        r.push_str("```rust\nfn main() {}\n```");
        let lines = r.render_to_lines(80);
        // Should contain the code line
        let all_text: String = lines
            .iter()
            .flat_map(|l| l.iter())
            .map(|s| &*s.text)
            .collect();
        assert!(all_text.contains("fn main()"));
    }

    #[test]
    fn list_items_with_markers() {
        let mut r = MarkdownRenderer::new();
        r.push_str("- item one\n- item two\n- item three");
        let lines = r.render_to_lines(80);
        assert!(lines.len() >= 3);
        let first_text: String = lines[0].iter().map(|s| &*s.text).collect();
        assert!(first_text.contains("-"));
        assert!(first_text.contains("item"));
    }

    #[test]
    fn incremental_push_str() {
        let mut r = MarkdownRenderer::new();
        r.push_str("Hello ");
        let lines1 = r.render_to_lines(80);
        r.push_str("world");
        let lines2 = r.render_to_lines(80);
        // After pushing "world", the output should include it
        let text: String = lines2
            .iter()
            .flat_map(|l| l.iter())
            .map(|s| &*s.text)
            .collect();
        assert!(text.contains("world"));
        // First render should only have "Hello"
        let text1: String = lines1
            .iter()
            .flat_map(|l| l.iter())
            .map(|s| &*s.text)
            .collect();
        assert!(text1.contains("Hello"));
    }

    #[test]
    fn width_wrapping() {
        let mut r = MarkdownRenderer::new();
        r.push_str("This is a long paragraph that should be wrapped to fit");
        let lines = r.render_to_lines(20);
        assert!(lines.len() > 1);
    }

    #[test]
    fn empty_input() {
        let r = MarkdownRenderer::new();
        let lines = r.render_to_lines(80);
        assert!(lines.is_empty());
    }

    #[test]
    fn clear_resets() {
        let mut r = MarkdownRenderer::new();
        r.push_str("some text");
        r.clear();
        let lines = r.render_to_lines(80);
        assert!(lines.is_empty());
    }

    #[test]
    fn mixed_content() {
        let mut r = MarkdownRenderer::new();
        r.push_str("# Title\n\nSome **bold** text.\n\n```\ncode\n```\n\n- list");
        let lines = r.render_to_lines(80);
        assert!(lines.len() >= 4);
    }
}