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
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
662
663
664
665
666
667
//! Diff viewer widget with unified and side-by-side display modes.
//!
//! Uses the [`similar`] crate to compute line-by-line diffs and displays
//! them with color-coded added/removed/unchanged styling.

use similar::{ChangeTag, TextDiff};

use crate::buffer::ScreenBuffer;
use crate::cell::Cell;
use crate::event::{Event, KeyCode, KeyEvent};
use crate::geometry::Rect;
use crate::style::Style;
use crate::text::truncate_to_display_width;

use super::{BorderStyle, EventResult, InteractiveWidget, Widget};

/// Display mode for the diff viewer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DiffMode {
    /// Unified diff: single column with +/- prefixes.
    Unified,
    /// Side-by-side diff: old on left, new on right.
    SideBySide,
}

/// A line in the computed diff.
#[derive(Clone, Debug)]
struct DiffLine {
    /// The diff tag for this line.
    tag: ChangeTag,
    /// The text content (without newline).
    text: String,
}

/// A pair of lines for side-by-side display.
#[derive(Clone, Debug)]
struct SideBySidePair {
    /// Left (old) line, if any.
    left: Option<DiffLine>,
    /// Right (new) line, if any.
    right: Option<DiffLine>,
}

/// A diff viewer widget that displays text differences.
///
/// Supports unified and side-by-side display modes with color-coded
/// added, removed, and unchanged lines.
pub struct DiffView {
    /// Original text.
    old_text: String,
    /// Modified text.
    new_text: String,
    /// Display mode.
    mode: DiffMode,
    /// Scroll offset (first visible line).
    scroll_offset: usize,
    /// Style for unchanged lines.
    unchanged_style: Style,
    /// Style for added lines.
    added_style: Style,
    /// Style for removed lines.
    removed_style: Style,
    /// Border style.
    border: BorderStyle,
    /// Cached unified diff lines.
    unified_lines: Vec<DiffLine>,
    /// Cached side-by-side pairs.
    sbs_pairs: Vec<SideBySidePair>,
}

impl DiffView {
    /// Create a new diff view comparing old and new text.
    pub fn new(old_text: &str, new_text: &str) -> Self {
        let mut view = Self {
            old_text: old_text.to_string(),
            new_text: new_text.to_string(),
            mode: DiffMode::Unified,
            scroll_offset: 0,
            unchanged_style: Style::default(),
            added_style: Style::default()
                .bg(crate::color::Color::Named(crate::color::NamedColor::Green)),
            removed_style: Style::default()
                .bg(crate::color::Color::Named(crate::color::NamedColor::Red)),
            border: BorderStyle::None,
            unified_lines: Vec::new(),
            sbs_pairs: Vec::new(),
        };
        view.compute_diff();
        view
    }

    /// Set the display mode.
    #[must_use]
    pub fn with_mode(mut self, mode: DiffMode) -> Self {
        self.mode = mode;
        self
    }

    /// Set the style for unchanged lines.
    #[must_use]
    pub fn with_unchanged_style(mut self, style: Style) -> Self {
        self.unchanged_style = style;
        self
    }

    /// Set the style for added lines.
    #[must_use]
    pub fn with_added_style(mut self, style: Style) -> Self {
        self.added_style = style;
        self
    }

    /// Set the style for removed lines.
    #[must_use]
    pub fn with_removed_style(mut self, style: Style) -> Self {
        self.removed_style = style;
        self
    }

    /// Set the border style.
    #[must_use]
    pub fn with_border(mut self, border: BorderStyle) -> Self {
        self.border = border;
        self
    }

    /// Set new texts and recompute the diff.
    pub fn set_texts(&mut self, old_text: &str, new_text: &str) {
        self.old_text = old_text.to_string();
        self.new_text = new_text.to_string();
        self.scroll_offset = 0;
        self.compute_diff();
    }

    /// Switch display mode.
    pub fn set_mode(&mut self, mode: DiffMode) {
        self.mode = mode;
        self.scroll_offset = 0;
    }

    /// Get the current display mode.
    pub fn mode(&self) -> DiffMode {
        self.mode
    }

    /// Get the total number of display lines for the current mode.
    pub fn line_count(&self) -> usize {
        match self.mode {
            DiffMode::Unified => self.unified_lines.len(),
            DiffMode::SideBySide => self.sbs_pairs.len(),
        }
    }

    /// Get the scroll offset.
    pub fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Compute the diff between old and new text.
    fn compute_diff(&mut self) {
        let diff = TextDiff::from_lines(&self.old_text, &self.new_text);

        // Build unified lines
        self.unified_lines.clear();
        for change in diff.iter_all_changes() {
            let text = change.to_string_lossy().trim_end_matches('\n').to_string();
            self.unified_lines.push(DiffLine {
                tag: change.tag(),
                text,
            });
        }

        // Build side-by-side pairs
        self.sbs_pairs.clear();
        let mut old_lines: Vec<DiffLine> = Vec::new();
        let mut new_lines: Vec<DiffLine> = Vec::new();

        for change in diff.iter_all_changes() {
            let text = change.to_string_lossy().trim_end_matches('\n').to_string();
            match change.tag() {
                ChangeTag::Equal => {
                    flush_sbs_pairs(&mut self.sbs_pairs, &mut old_lines, &mut new_lines);
                    self.sbs_pairs.push(SideBySidePair {
                        left: Some(DiffLine {
                            tag: ChangeTag::Equal,
                            text: text.clone(),
                        }),
                        right: Some(DiffLine {
                            tag: ChangeTag::Equal,
                            text,
                        }),
                    });
                }
                ChangeTag::Delete => {
                    old_lines.push(DiffLine {
                        tag: ChangeTag::Delete,
                        text,
                    });
                }
                ChangeTag::Insert => {
                    new_lines.push(DiffLine {
                        tag: ChangeTag::Insert,
                        text,
                    });
                }
            }
        }
        flush_sbs_pairs(&mut self.sbs_pairs, &mut old_lines, &mut new_lines);
    }

    /// Get the style for a given change tag.
    fn style_for_tag(&self, tag: ChangeTag) -> &Style {
        match tag {
            ChangeTag::Equal => &self.unchanged_style,
            ChangeTag::Insert => &self.added_style,
            ChangeTag::Delete => &self.removed_style,
        }
    }

    /// Get the prefix character for a given change tag.
    fn prefix_for_tag(tag: ChangeTag) -> &'static str {
        match tag {
            ChangeTag::Equal => " ",
            ChangeTag::Insert => "+",
            ChangeTag::Delete => "-",
        }
    }

    /// Render a single line of text into the buffer at the given position.
    fn render_line(
        &self,
        text: &str,
        style: &Style,
        x: u16,
        y: u16,
        max_width: usize,
        buf: &mut ScreenBuffer,
    ) {
        let truncated = truncate_to_display_width(text, max_width);
        let mut col: u16 = 0;
        for ch in truncated.chars() {
            let char_w = unicode_width::UnicodeWidthStr::width(ch.encode_utf8(&mut [0; 4]) as &str);
            if col as usize + char_w > max_width {
                break;
            }
            buf.set(x + col, y, Cell::new(ch.to_string(), style.clone()));
            col += char_w as u16;
        }
    }

    /// Render in unified mode.
    fn render_unified(&self, inner: Rect, buf: &mut ScreenBuffer) {
        let height = inner.size.height as usize;
        let width = inner.size.width as usize;
        let count = self.unified_lines.len();
        let max_offset = count.saturating_sub(height.max(1));
        let scroll = self.scroll_offset.min(max_offset);
        let end = (scroll + height).min(count);

        for (row, line_idx) in (scroll..end).enumerate() {
            let y = inner.position.y + row as u16;
            if let Some(line) = self.unified_lines.get(line_idx) {
                let style = self.style_for_tag(line.tag);
                let prefix = Self::prefix_for_tag(line.tag);

                // Fill the full row with the style
                for col in 0..inner.size.width {
                    buf.set(inner.position.x + col, y, Cell::new(" ", style.clone()));
                }

                // Render prefix
                if width > 0 {
                    buf.set(inner.position.x, y, Cell::new(prefix, style.clone()));
                }

                // Render text content (after prefix)
                if width > 1 {
                    self.render_line(
                        &line.text,
                        style,
                        inner.position.x + 1,
                        y,
                        width.saturating_sub(1),
                        buf,
                    );
                }
            }
        }
    }

    /// Render in side-by-side mode.
    fn render_side_by_side(&self, inner: Rect, buf: &mut ScreenBuffer) {
        let height = inner.size.height as usize;
        let total_width = inner.size.width as usize;
        let count = self.sbs_pairs.len();
        let max_offset = count.saturating_sub(height.max(1));
        let scroll = self.scroll_offset.min(max_offset);
        let end = (scroll + height).min(count);

        // Split width: left half | separator | right half
        if total_width < 3 {
            return;
        }
        let separator_col = total_width / 2;
        let left_width = separator_col;
        let right_width = total_width.saturating_sub(separator_col + 1);

        // Draw separator
        for row in 0..inner.size.height {
            buf.set(
                inner.position.x + separator_col as u16,
                inner.position.y + row,
                Cell::new("\u{2502}", self.unchanged_style.clone()), //            );
        }

        for (row, pair_idx) in (scroll..end).enumerate() {
            let y = inner.position.y + row as u16;
            if let Some(pair) = self.sbs_pairs.get(pair_idx) {
                // Left side
                if let Some(ref left) = pair.left {
                    let style = self.style_for_tag(left.tag);
                    // Fill left side with style
                    for col in 0..left_width {
                        buf.set(
                            inner.position.x + col as u16,
                            y,
                            Cell::new(" ", style.clone()),
                        );
                    }
                    self.render_line(&left.text, style, inner.position.x, y, left_width, buf);
                }

                // Right side
                if let Some(ref right) = pair.right {
                    let style = self.style_for_tag(right.tag);
                    let right_x = inner.position.x + separator_col as u16 + 1;
                    // Fill right side with style
                    for col in 0..right_width {
                        buf.set(right_x + col as u16, y, Cell::new(" ", style.clone()));
                    }
                    self.render_line(&right.text, style, right_x, y, right_width, buf);
                }
            }
        }
    }
}

impl Widget for DiffView {
    fn render(&self, area: Rect, buf: &mut ScreenBuffer) {
        if area.size.width == 0 || area.size.height == 0 {
            return;
        }

        super::border::render_border(area, self.border, self.unchanged_style.clone(), buf);

        let inner = super::border::inner_area(area, self.border);
        if inner.size.width == 0 || inner.size.height == 0 {
            return;
        }

        match self.mode {
            DiffMode::Unified => self.render_unified(inner, buf),
            DiffMode::SideBySide => self.render_side_by_side(inner, buf),
        }
    }
}

impl InteractiveWidget for DiffView {
    fn handle_event(&mut self, event: &Event) -> EventResult {
        let Event::Key(KeyEvent { code, .. }) = event else {
            return EventResult::Ignored;
        };

        let count = self.line_count();

        match code {
            KeyCode::Up => {
                if self.scroll_offset > 0 {
                    self.scroll_offset -= 1;
                }
                EventResult::Consumed
            }
            KeyCode::Down => {
                if count > 0 && self.scroll_offset < count.saturating_sub(1) {
                    self.scroll_offset += 1;
                }
                EventResult::Consumed
            }
            KeyCode::PageUp => {
                self.scroll_offset = self.scroll_offset.saturating_sub(20);
                EventResult::Consumed
            }
            KeyCode::PageDown => {
                if count > 0 {
                    self.scroll_offset = (self.scroll_offset + 20).min(count.saturating_sub(1));
                }
                EventResult::Consumed
            }
            KeyCode::Home => {
                self.scroll_offset = 0;
                EventResult::Consumed
            }
            KeyCode::End => {
                if count > 0 {
                    self.scroll_offset = count.saturating_sub(1);
                }
                EventResult::Consumed
            }
            KeyCode::Char('m') => {
                self.mode = match self.mode {
                    DiffMode::Unified => DiffMode::SideBySide,
                    DiffMode::SideBySide => DiffMode::Unified,
                };
                self.scroll_offset = 0;
                EventResult::Consumed
            }
            _ => EventResult::Ignored,
        }
    }
}

/// Flush accumulated old/new lines into side-by-side pairs.
fn flush_sbs_pairs(
    pairs: &mut Vec<SideBySidePair>,
    old_lines: &mut Vec<DiffLine>,
    new_lines: &mut Vec<DiffLine>,
) {
    let max_len = old_lines.len().max(new_lines.len());
    for i in 0..max_len {
        pairs.push(SideBySidePair {
            left: old_lines.get(i).cloned(),
            right: new_lines.get(i).cloned(),
        });
    }
    old_lines.clear();
    new_lines.clear();
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::geometry::Size;

    #[test]
    fn create_diff_view() {
        let dv = DiffView::new("hello\nworld\n", "hello\nrust\n");
        assert_eq!(dv.mode(), DiffMode::Unified);
        assert!(dv.line_count() > 0);
    }

    #[test]
    fn unified_prefixes() {
        let dv = DiffView::new("aaa\nbbb\n", "aaa\nccc\n");

        // Should have: " aaa", "-bbb", "+ccc"
        assert_eq!(dv.unified_lines.len(), 3);
        assert_eq!(dv.unified_lines[0].tag, ChangeTag::Equal);
        assert_eq!(dv.unified_lines[0].text, "aaa");
        assert_eq!(dv.unified_lines[1].tag, ChangeTag::Delete);
        assert_eq!(dv.unified_lines[1].text, "bbb");
        assert_eq!(dv.unified_lines[2].tag, ChangeTag::Insert);
        assert_eq!(dv.unified_lines[2].text, "ccc");
    }

    #[test]
    fn side_by_side_pairs() {
        let dv = DiffView::new("aaa\nbbb\n", "aaa\nccc\n").with_mode(DiffMode::SideBySide);

        assert_eq!(dv.mode(), DiffMode::SideBySide);
        // Pair 1: aaa | aaa (equal)
        // Pair 2: bbb | ccc (delete | insert)
        assert_eq!(dv.sbs_pairs.len(), 2);

        assert!(dv.sbs_pairs[0].left.is_some());
        assert!(dv.sbs_pairs[0].right.is_some());
        assert_eq!(
            dv.sbs_pairs[0].left.as_ref().map(|l| l.tag),
            Some(ChangeTag::Equal)
        );

        assert!(dv.sbs_pairs[1].left.is_some());
        assert!(dv.sbs_pairs[1].right.is_some());
        assert_eq!(
            dv.sbs_pairs[1].left.as_ref().map(|l| l.tag),
            Some(ChangeTag::Delete)
        );
        assert_eq!(
            dv.sbs_pairs[1].right.as_ref().map(|l| l.tag),
            Some(ChangeTag::Insert)
        );
    }

    #[test]
    fn scroll_up_down() {
        let mut dv = DiffView::new("a\nb\nc\nd\ne\nf\n", "a\nb\nc\nd\ne\nf\n");

        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });
        let up = Event::Key(KeyEvent {
            code: KeyCode::Up,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(dv.scroll_offset(), 0);
        dv.handle_event(&down);
        assert_eq!(dv.scroll_offset(), 1);
        dv.handle_event(&up);
        assert_eq!(dv.scroll_offset(), 0);
        // Up at 0 stays 0
        dv.handle_event(&up);
        assert_eq!(dv.scroll_offset(), 0);
    }

    #[test]
    fn page_up_down() {
        let mut dv = DiffView::new(
            "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline14\nline15\nline16\nline17\nline18\nline19\nline20\nline21\nline22\nline23\nline24\nline25\n",
            "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline14\nline15\nline16\nline17\nline18\nline19\nline20\nline21\nline22\nline23\nline24\nline25\n",
        );

        let pgdn = Event::Key(KeyEvent {
            code: KeyCode::PageDown,
            modifiers: crate::event::Modifiers::NONE,
        });
        let pgup = Event::Key(KeyEvent {
            code: KeyCode::PageUp,
            modifiers: crate::event::Modifiers::NONE,
        });

        dv.handle_event(&pgdn);
        assert_eq!(dv.scroll_offset(), 20);
        dv.handle_event(&pgup);
        assert_eq!(dv.scroll_offset(), 0);
    }

    #[test]
    fn home_end() {
        let mut dv = DiffView::new("a\nb\nc\nd\ne\n", "a\nb\nc\nd\ne\n");

        let end_key = Event::Key(KeyEvent {
            code: KeyCode::End,
            modifiers: crate::event::Modifiers::NONE,
        });
        let home_key = Event::Key(KeyEvent {
            code: KeyCode::Home,
            modifiers: crate::event::Modifiers::NONE,
        });

        dv.handle_event(&end_key);
        assert_eq!(dv.scroll_offset(), dv.line_count().saturating_sub(1));
        dv.handle_event(&home_key);
        assert_eq!(dv.scroll_offset(), 0);
    }

    #[test]
    fn toggle_mode_with_m() {
        let mut dv = DiffView::new("a\n", "b\n");
        assert_eq!(dv.mode(), DiffMode::Unified);

        let m = Event::Key(KeyEvent {
            code: KeyCode::Char('m'),
            modifiers: crate::event::Modifiers::NONE,
        });

        dv.handle_event(&m);
        assert_eq!(dv.mode(), DiffMode::SideBySide);
        dv.handle_event(&m);
        assert_eq!(dv.mode(), DiffMode::Unified);
    }

    #[test]
    fn empty_diff_identical_texts() {
        let dv = DiffView::new("same\n", "same\n");
        assert_eq!(dv.unified_lines.len(), 1);
        assert_eq!(dv.unified_lines[0].tag, ChangeTag::Equal);
    }

    #[test]
    fn all_added_old_empty() {
        let dv = DiffView::new("", "new1\nnew2\n");
        for line in &dv.unified_lines {
            assert_eq!(line.tag, ChangeTag::Insert);
        }
    }

    #[test]
    fn all_removed_new_empty() {
        let dv = DiffView::new("old1\nold2\n", "");
        for line in &dv.unified_lines {
            assert_eq!(line.tag, ChangeTag::Delete);
        }
    }

    #[test]
    fn mixed_changes() {
        let dv = DiffView::new("a\nb\nc\n", "a\nB\nc\nd\n");
        // a = equal, b = delete, B = insert, c = equal, d = insert
        let tags: Vec<ChangeTag> = dv.unified_lines.iter().map(|l| l.tag).collect();
        assert!(tags.contains(&ChangeTag::Equal));
        assert!(tags.contains(&ChangeTag::Delete));
        assert!(tags.contains(&ChangeTag::Insert));
    }

    #[test]
    fn render_unified_mode() {
        let dv = DiffView::new("old\n", "new\n");
        let mut buf = ScreenBuffer::new(Size::new(30, 5));
        dv.render(Rect::new(0, 0, 30, 5), &mut buf);

        // First line should have "-" prefix (delete)
        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("-"));
        // Second line should have "+" prefix (insert)
        assert_eq!(buf.get(0, 1).map(|c| c.grapheme.as_str()), Some("+"));
    }

    #[test]
    fn render_side_by_side_mode() {
        let dv = DiffView::new("old\n", "new\n").with_mode(DiffMode::SideBySide);
        let mut buf = ScreenBuffer::new(Size::new(20, 5));
        dv.render(Rect::new(0, 0, 20, 5), &mut buf);

        // Separator at column 10 (width/2)
        assert_eq!(
            buf.get(10, 0).map(|c| c.grapheme.as_str()),
            Some("\u{2502}")
        );
    }

    #[test]
    fn set_texts_recomputes() {
        let mut dv = DiffView::new("a\n", "b\n");
        let initial_count = dv.line_count();

        dv.set_texts("x\ny\nz\n", "x\nw\nz\n");
        // Should have recomputed
        assert!(dv.line_count() > 0);
        // Scroll should be reset
        assert_eq!(dv.scroll_offset(), 0);
        // Count may differ
        let _ = initial_count;
    }

    #[test]
    fn border_rendering() {
        let dv = DiffView::new("a\n", "b\n").with_border(BorderStyle::Single);
        let mut buf = ScreenBuffer::new(Size::new(30, 10));
        dv.render(Rect::new(0, 0, 30, 10), &mut buf);

        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("\u{250c}"));
    }

    #[test]
    fn utf8_safe_diff() {
        let dv = DiffView::new("你好\n", "世界\n");
        assert_eq!(dv.line_count(), 2); // one delete, one insert

        let mut buf = ScreenBuffer::new(Size::new(20, 5));
        dv.render(Rect::new(0, 0, 20, 5), &mut buf);

        // Should render without panic
        assert!(buf.get(0, 0).is_some());
    }
}