tuile 0.1.0

Textual-grade widgets, design system, animation and app runtime on top of ratatui
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Markdown renderer for CommonMark subset with scrolling.
//!
//! ```
//! # use tuile::prelude::*;
//! # use tuile::widgets::markdown::*;
//! # let mut buf = Buffer::empty(Rect::new(0, 0, 60, 20));
//! # let mut state = MarkdownState::default();
//! let src = "# Hello\n\nThis is **bold** and *italic*.";
//! Markdown::new(src).render(Rect::new(0, 0, 60, 20), &mut buf, &mut state);
//! ```

use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{KeyCode, KeyEvent, MouseEvent};
use ratatui::layout::Rect;
use ratatui::style::Modifier;
use ratatui::text::{Line, Span};
use ratatui::widgets::StatefulWidget;

use crate::core::{Interactive, Outcome, is_press, wheel_delta};
use crate::draw::{put, st, wrap as text_wrap};
use crate::theme::{self, Theme};
use crate::widgets::scrollbar::{Scrollbar, ScrollbarState};

// Markdown state

/// Markdown view state with scrolling.
#[derive(Clone, Debug, Default)]
pub struct MarkdownState {
    pub scroll: usize,
    pub vbar: ScrollbarState,
}

impl MarkdownState {
    pub fn new() -> Self {
        Self::default()
    }
}

impl Interactive for MarkdownState {
    fn handle_key(&mut self, key: KeyEvent) -> Outcome {
        if !is_press(&key) {
            return Outcome::Ignored;
        }
        match key.code {
            KeyCode::Up => {
                self.scroll = self.scroll.saturating_sub(1);
                Outcome::Consumed
            }
            KeyCode::Down => {
                self.scroll = self.scroll.saturating_add(1);
                Outcome::Consumed
            }
            KeyCode::PageUp => {
                self.scroll = self.scroll.saturating_sub(10);
                Outcome::Consumed
            }
            KeyCode::PageDown => {
                self.scroll = self.scroll.saturating_add(10);
                Outcome::Consumed
            }
            KeyCode::Home => {
                self.scroll = 0;
                Outcome::Consumed
            }
            KeyCode::End => {
                self.scroll = usize::MAX;
                Outcome::Consumed
            }
            _ => Outcome::Ignored,
        }
    }

    fn handle_mouse(&mut self, m: MouseEvent) -> Outcome {
        let out = self.vbar.handle_mouse(m);
        if out.is_consumed() {
            self.scroll = self.vbar.offset;
            return out;
        }

        if let Some(delta) = wheel_delta(&m) {
            if delta > 0 {
                self.scroll = self.scroll.saturating_add(3);
            } else {
                self.scroll = self.scroll.saturating_sub(3);
            }
            return Outcome::Consumed;
        }

        Outcome::Ignored
    }
}

// Markdown widget

/// Markdown renderer widget.
#[derive(Clone, Debug)]
pub struct Markdown {
    src: String,
    theme: Option<Theme>,
}

impl Markdown {
    pub fn new(src: impl Into<String>) -> Self {
        Self {
            src: src.into(),
            theme: None,
        }
    }

    pub fn theme(mut self, th: &Theme) -> Self {
        self.theme = Some(*th);
        self
    }

    /// Render markdown to styled lines at a given width.
    pub fn lines(&self, width: usize, th: &Theme) -> Vec<Line<'static>> {
        let mut parser = MdParser::new(&self.src, width, th);
        parser.parse();
        parser.lines
    }

    /// Compute height when rendered at width.
    pub fn height(&self, width: usize, th: &Theme) -> usize {
        self.lines(width, th).len()
    }
}

impl StatefulWidget for Markdown {
    type State = MarkdownState;
    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if area.width < 5 || area.height < 2 {
            return;
        }
        let th = self.theme.unwrap_or_else(theme::current);
        // Don't fill - let the parent background show through
        let content_w = area.width.saturating_sub(1);
        let lines = self.lines(content_w as usize, &th);

        let total = lines.len();
        let viewport = area.height as usize;
        state.scroll = state.scroll.min(total.saturating_sub(viewport));

        // Render visible lines
        for (i, line) in lines.iter().enumerate().skip(state.scroll).take(viewport) {
            let y = area.y + (i - state.scroll) as u16;
            let mut x = area.x;
            for span in &line.spans {
                let w = span.content.len() as u16;
                if x + w > area.x + content_w {
                    break;
                }
                put(buf, x, y, &span.content, w, span.style);
                x += w;
            }
        }

        // Scrollbar
        let sb_area = Rect {
            x: area.x + content_w,
            y: area.y,
            width: 1,
            height: area.height,
        };
        Scrollbar::vertical(total, viewport)
            .offset(state.scroll)
            .theme(&th)
            .render(sb_area, buf, &mut state.vbar);
    }
}

// Markdown parser

struct MdParser<'a> {
    src: &'a str,
    width: usize,
    th: &'a Theme,
    lines: Vec<Line<'static>>,
    in_code_block: bool,
    code_lang: String,
}

impl<'a> MdParser<'a> {
    fn new(src: &'a str, width: usize, th: &'a Theme) -> Self {
        Self {
            src,
            width,
            th,
            lines: Vec::new(),
            in_code_block: false,
            code_lang: String::new(),
        }
    }

    fn parse(&mut self) {
        let raw_lines: Vec<&str> = self.src.lines().collect();
        let mut i = 0;
        while i < raw_lines.len() {
            let line = raw_lines[i];

            // code fence
            if let Some(rest) = line.strip_prefix("```") {
                if self.in_code_block {
                    // close
                    self.in_code_block = false;
                    self.lines.push(Line::raw(""));
                } else {
                    // open
                    self.in_code_block = true;
                    self.code_lang = rest.trim().to_string();
                    if !self.code_lang.is_empty() {
                        // lang label at top-right
                        let label = format!(" {} ", self.code_lang);
                        self.lines.push(Line::from(vec![
                            Span::raw(" ".repeat(self.width.saturating_sub(label.len()))),
                            Span::styled(
                                label,
                                st(self.th.text_muted, self.th.markdown_code_bg)
                                    .add_modifier(Modifier::DIM),
                            ),
                        ]));
                    }
                }
                i += 1;
                continue;
            }

            if self.in_code_block {
                // render code line with bg
                let padded = format!(" {} ", line);
                self.lines.push(Line::styled(
                    padded,
                    st(self.th.text, self.th.markdown_code_bg),
                ));
                i += 1;
                continue;
            }

            // headings
            if let Some(level) = heading_level(line) {
                let text = line[level..].trim();
                let (fg, underline) = match level {
                    1 => (self.th.text, true),
                    2 => (self.th.text, true),
                    3 => (self.th.text, false),
                    4 => (self.th.text_muted, false),
                    _ => (self.th.text, false),
                };
                // Use Span like paragraphs do, with both fg and bg
                let span = Span::styled(
                    text.to_string(),
                    st(fg, self.th.background).add_modifier(Modifier::BOLD),
                );
                self.lines.push(Line::from(vec![span]));
                if underline && level == 1 {
                    let rule_len = text.len().min(self.width);
                    let rule_span = Span::styled(
                        "▔".repeat(rule_len),
                        st(self.th.secondary, self.th.background),
                    );
                    self.lines.push(Line::from(vec![rule_span]));
                } else if underline && level == 2 {
                    let rule_len = text.len().min(self.width);
                    let rule_span = Span::styled(
                        "─".repeat(rule_len),
                        st(self.th.secondary, self.th.background),
                    );
                    self.lines.push(Line::from(vec![rule_span]));
                }
                self.lines.push(Line::raw(""));
                i += 1;
                continue;
            }

            // horizontal rule
            if line.trim() == "---" || line.trim() == "***" || line.trim() == "___" {
                self.lines.push(Line::styled(
                    "─".repeat(self.width),
                    st(self.th.border, self.th.surface),
                ));
                self.lines.push(Line::raw(""));
                i += 1;
                continue;
            }

            // blockquote
            if let Some(rest) = line.strip_prefix('>') {
                let text = rest.trim();
                let spans = vec![
                    Span::styled("▌ ", st(self.th.secondary, self.th.surface)),
                    Span::styled(
                        text.to_string(),
                        st(self.th.text_muted, self.th.surface).add_modifier(Modifier::ITALIC),
                    ),
                ];
                self.lines.push(Line::from(spans));
                i += 1;
                continue;
            }

            // unordered list
            if line.trim_start().starts_with("- ") || line.trim_start().starts_with("* ") {
                let indent = line.len() - line.trim_start().len();
                let depth = indent / 2;
                let text = line.trim_start()[1..].trim();
                let bullet = match depth % 3 {
                    0 => "•",
                    1 => "◦",
                    _ => "•",
                };
                let prefix = format!("{}{} ", " ".repeat(indent), bullet);
                let parsed = self.parse_inline(text);
                let mut spans = vec![Span::styled(prefix, st(self.th.text, self.th.surface))];
                spans.extend(parsed);
                self.lines.push(Line::from(spans));
                i += 1;
                continue;
            }

            // ordered list
            if let Some(num_end) = line.trim_start().find('.')
                && line.trim_start()[..num_end]
                    .chars()
                    .all(|c| c.is_ascii_digit())
            {
                let indent = line.len() - line.trim_start().len();
                let num = &line.trim_start()[..num_end];
                let text = line.trim_start()[num_end + 1..].trim();
                let prefix = format!("{}{}. ", " ".repeat(indent), num);
                let parsed = self.parse_inline(text);
                let mut spans = vec![Span::styled(prefix, st(self.th.text, self.th.surface))];
                spans.extend(parsed);
                self.lines.push(Line::from(spans));
                i += 1;
                continue;
            }

            // task list
            if line.trim_start().starts_with("- [") {
                let indent = line.len() - line.trim_start().len();
                let checkbox_end = line.find(']').unwrap_or(0);
                let checked = line.contains("[x]") || line.contains("[X]");
                let text = if checkbox_end > 0 {
                    line[checkbox_end + 1..].trim()
                } else {
                    ""
                };
                let checkbox = if checked { "[✓]" } else { "[ ]" };
                let prefix = format!("{}{} ", " ".repeat(indent), checkbox);
                let parsed = self.parse_inline(text);
                let mut spans = vec![Span::styled(
                    prefix,
                    st(self.th.text_muted, self.th.surface),
                )];
                spans.extend(parsed);
                self.lines.push(Line::from(spans));
                i += 1;
                continue;
            }

            // table
            if line.contains('|') && i + 1 < raw_lines.len() && raw_lines[i + 1].contains("---") {
                i = self.parse_table(&raw_lines, i);
                continue;
            }

            // blank line
            if line.trim().is_empty() {
                self.lines.push(Line::raw(""));
                i += 1;
                continue;
            }

            // paragraph
            let wrapped = text_wrap(line, self.width);
            for wline in wrapped {
                let spans = self.parse_inline(&wline);
                self.lines.push(Line::from(spans));
            }
            i += 1;
        }
    }

    fn parse_inline(&self, text: &str) -> Vec<Span<'static>> {
        let mut spans = Vec::new();
        let mut i = 0;
        let chars: Vec<char> = text.chars().collect();
        let mut current = String::new();
        let current_style = st(self.th.text, self.th.surface);

        while i < chars.len() {
            // **bold**
            if i + 1 < chars.len() && chars[i] == '*' && chars[i + 1] == '*' {
                if !current.is_empty() {
                    spans.push(Span::styled(current.clone(), current_style));
                    current.clear();
                }
                i += 2;
                let mut bold_text = String::new();
                while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '*') {
                    bold_text.push(chars[i]);
                    i += 1;
                }
                if i + 1 < chars.len() {
                    i += 2;
                }
                spans.push(Span::styled(
                    bold_text,
                    current_style.add_modifier(Modifier::BOLD),
                ));
                continue;
            }

            // *italic*
            if chars[i] == '*' {
                if !current.is_empty() {
                    spans.push(Span::styled(current.clone(), current_style));
                    current.clear();
                }
                i += 1;
                let mut italic_text = String::new();
                while i < chars.len() && chars[i] != '*' {
                    italic_text.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    i += 1;
                }
                spans.push(Span::styled(
                    italic_text,
                    current_style.add_modifier(Modifier::ITALIC),
                ));
                continue;
            }

            // `code`
            if chars[i] == '`' {
                if !current.is_empty() {
                    spans.push(Span::styled(current.clone(), current_style));
                    current.clear();
                }
                i += 1;
                let mut code_text = String::new();
                while i < chars.len() && chars[i] != '`' {
                    code_text.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    i += 1;
                }
                let code_padded = format!(" {} ", code_text);
                spans.push(Span::styled(
                    code_padded,
                    st(self.th.text, self.th.markdown_code_bg),
                ));
                continue;
            }

            // ~~strike~~
            if i + 1 < chars.len() && chars[i] == '~' && chars[i + 1] == '~' {
                if !current.is_empty() {
                    spans.push(Span::styled(current.clone(), current_style));
                    current.clear();
                }
                i += 2;
                let mut strike_text = String::new();
                while i + 1 < chars.len() && !(chars[i] == '~' && chars[i + 1] == '~') {
                    strike_text.push(chars[i]);
                    i += 1;
                }
                if i + 1 < chars.len() {
                    i += 2;
                }
                spans.push(Span::styled(
                    strike_text,
                    current_style.add_modifier(Modifier::CROSSED_OUT),
                ));
                continue;
            }

            // [text](url)
            if chars[i] == '[' {
                if !current.is_empty() {
                    spans.push(Span::styled(current.clone(), current_style));
                    current.clear();
                }
                i += 1;
                let mut link_text = String::new();
                while i < chars.len() && chars[i] != ']' {
                    link_text.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    i += 1;
                }
                // skip (url)
                if i < chars.len() && chars[i] == '(' {
                    while i < chars.len() && chars[i] != ')' {
                        i += 1;
                    }
                    if i < chars.len() {
                        i += 1;
                    }
                }
                spans.push(Span::styled(
                    link_text,
                    st(self.th.link, self.th.surface).add_modifier(Modifier::UNDERLINED),
                ));
                continue;
            }

            current.push(chars[i]);
            i += 1;
        }

        if !current.is_empty() {
            spans.push(Span::styled(current, current_style));
        }

        spans
    }

    fn parse_table(&mut self, raw_lines: &[&str], start: usize) -> usize {
        let header = raw_lines[start];
        let _sep = raw_lines[start + 1];
        let mut i = start + 2;

        // parse header
        let headers: Vec<&str> = header
            .split('|')
            .map(|s| s.trim())
            .filter(|s| !s.is_empty())
            .collect();
        if headers.is_empty() {
            return i;
        }

        let col_w = (self.width / headers.len()).max(5);

        // header row
        let mut header_spans = Vec::new();
        for (idx, h) in headers.iter().enumerate() {
            let display = crate::draw::fit(h, col_w);
            header_spans.push(Span::styled(
                display,
                st(self.th.text, self.th.surface).add_modifier(Modifier::BOLD),
            ));
            if idx < headers.len() - 1 {
                header_spans.push(Span::raw(" │ "));
            }
        }
        self.lines.push(Line::from(header_spans));

        // separator
        let sep_line = (0..headers.len())
            .map(|_| "─".repeat(col_w))
            .collect::<Vec<_>>()
            .join("─┼─");
        self.lines
            .push(Line::styled(sep_line, st(self.th.border, self.th.surface)));

        // data rows
        while i < raw_lines.len() {
            let row = raw_lines[i];
            if !row.contains('|') {
                break;
            }
            let cells: Vec<&str> = row
                .split('|')
                .map(|s| s.trim())
                .filter(|s| !s.is_empty())
                .collect();
            if cells.is_empty() {
                break;
            }

            let mut row_spans = Vec::new();
            for (idx, cell) in cells.iter().enumerate().take(headers.len()) {
                let display = crate::draw::fit(cell, col_w);
                row_spans.push(Span::raw(display));
                if idx < headers.len() - 1 {
                    row_spans.push(Span::raw(" │ "));
                }
            }
            self.lines.push(Line::from(row_spans));
            i += 1;
        }

        self.lines.push(Line::raw(""));
        i
    }
}

fn heading_level(line: &str) -> Option<usize> {
    let trimmed = line.trim_start();
    let hashes = trimmed.chars().take_while(|&c| c == '#').count();
    if hashes > 0 && hashes <= 6 && trimmed.chars().nth(hashes) == Some(' ') {
        Some(hashes)
    } else {
        None
    }
}

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

    #[test]
    fn heading_detected() {
        assert_eq!(heading_level("# Title"), Some(1));
        assert_eq!(heading_level("## Sub"), Some(2));
        assert_eq!(heading_level("### Third"), Some(3));
        assert_eq!(heading_level("#NoSpace"), None);
    }

    #[test]
    fn markdown_renders_bold() {
        let th = Theme::default();
        let md = Markdown::new("**bold**");
        let lines = md.lines(40, &th);
        assert_eq!(lines.len(), 1);
        assert!(
            lines[0].spans[0]
                .style
                .add_modifier
                .contains(Modifier::BOLD)
        );
    }

    #[test]
    fn markdown_wraps_paragraphs() {
        let th = Theme::default();
        let md = Markdown::new("hello world test long line");
        let lines = md.lines(10, &th);
        assert!(lines.len() >= 2);
    }

    #[test]
    fn markdown_renders_heading() {
        let th = Theme::default();
        let md = Markdown::new("# Title\n\ntext");
        let lines = md.lines(40, &th);
        // Debug: print what we got
        eprintln!("Lines generated: {}", lines.len());
        for (i, line) in lines.iter().enumerate() {
            eprintln!(
                "Line {}: spans={}, first_span={:?}",
                i,
                line.spans.len(),
                line.spans.first().map(|s| &s.content)
            );
        }
        assert!(
            lines.len() >= 3,
            "Expected heading + underline + blank + text"
        );
        assert!(
            lines[0].spans[0].content.contains("Title"),
            "First line should contain 'Title'"
        );
    }
}