revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Terminal backend using crossterm

mod core;
mod helper;
mod render;
mod types;

pub use helper::stdout_terminal;
pub use types::Terminal;

#[cfg(test)]
mod tests {
    use crate::layout::Rect;
    use crate::render::cell::{Cell, Modifier};
    use crate::render::Buffer;
    use crate::style::Color;
    use crossterm::style::Color as CrosstermColor;
    use std::io::{self, Write};

    // Helper function to convert Color to CrosstermColor (duplicated from render.rs)
    fn to_crossterm_color(color: Color) -> CrosstermColor {
        // Check if it's a named color (using helper methods)
        let Color { r, g, b, a: _ } = color;
        CrosstermColor::Rgb { r, g, b }
    }

    // Mock writer for testing
    struct MockWriter {
        buffer: Vec<u8>,
    }

    impl MockWriter {
        fn new() -> Self {
            Self { buffer: Vec::new() }
        }

        fn contents(&self) -> &[u8] {
            &self.buffer
        }

        fn as_string(&self) -> String {
            String::from_utf8_lossy(&self.buffer).to_string()
        }
    }

    impl Write for MockWriter {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.buffer.extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    // RenderState is internal, so we use a minimal test struct
    #[derive(Default)]
    struct RenderState {
        fg: Option<Color>,
        bg: Option<Color>,
        modifier: Modifier,
        hyperlink_id: Option<u16>,
        cursor: Option<(u16, u16)>,
    }

    // RenderState tests
    #[test]
    fn test_render_state_default() {
        let state = RenderState::default();
        assert!(state.fg.is_none());
        assert!(state.bg.is_none());
        assert!(state.modifier.is_empty());
        assert!(state.hyperlink_id.is_none());
    }

    // Color conversion tests
    #[test]
    fn test_to_crossterm_color() {
        let color = Color::rgb(255, 128, 64);
        let ct_color = to_crossterm_color(color);

        match ct_color {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 255);
                assert_eq!(g, 128);
                assert_eq!(b, 64);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_constants_conversion() {
        let red = to_crossterm_color(Color::RED);
        match red {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 255);
                assert_eq!(g, 0);
                assert_eq!(b, 0);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_green_conversion() {
        let green = to_crossterm_color(Color::GREEN);
        match green {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 0);
                assert_eq!(g, 255);
                assert_eq!(b, 0);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_blue_conversion() {
        let blue = to_crossterm_color(Color::BLUE);
        match blue {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 0);
                assert_eq!(g, 0);
                assert_eq!(b, 255);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_white_conversion() {
        let white = to_crossterm_color(Color::WHITE);
        match white {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 255);
                assert_eq!(g, 255);
                assert_eq!(b, 255);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_black_conversion() {
        let black = to_crossterm_color(Color::BLACK);
        match black {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 0);
                assert_eq!(g, 0);
                assert_eq!(b, 0);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_cyan_conversion() {
        let cyan = to_crossterm_color(Color::CYAN);
        match cyan {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 0);
                assert_eq!(g, 255);
                assert_eq!(b, 255);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_magenta_conversion() {
        let magenta = to_crossterm_color(Color::MAGENTA);
        match magenta {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 255);
                assert_eq!(g, 0);
                assert_eq!(b, 255);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_yellow_conversion() {
        let yellow = to_crossterm_color(Color::YELLOW);
        match yellow {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 255);
                assert_eq!(g, 255);
                assert_eq!(b, 0);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_color_gray_conversion() {
        let gray = to_crossterm_color(Color::rgb(128, 128, 128));
        match gray {
            CrosstermColor::Rgb { r, g, b } => {
                assert_eq!(r, 128);
                assert_eq!(g, 128);
                assert_eq!(b, 128);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    // Hyperlink escape sequence tests
    #[test]
    fn test_hyperlink_start_escape() {
        let mut writer = MockWriter::new();
        let url = "https://example.com";
        write!(writer, "\x1b]8;;{}\x1b\\", url).unwrap();
        let output = writer.as_string();
        assert!(output.contains("8;;"));
        assert!(output.contains("https://example.com"));
    }

    #[test]
    fn test_hyperlink_end_escape() {
        let mut writer = MockWriter::new();
        write!(writer, "\x1b]8;;\x1b\\").unwrap();
        let output = writer.as_string();
        assert!(output.contains("8;;"));
    }

    // MockWriter tests
    #[test]
    fn test_mock_writer_write() {
        let mut writer = MockWriter::new();
        let bytes_written = writer.write(b"hello").unwrap();
        assert_eq!(bytes_written, 5);
        assert_eq!(writer.contents(), b"hello");
    }

    #[test]
    fn test_mock_writer_multiple_writes() {
        let mut writer = MockWriter::new();
        writer.write(b"hello").unwrap();
        writer.write(b" ").unwrap();
        writer.write(b"world").unwrap();
        assert_eq!(writer.as_string(), "hello world");
    }

    #[test]
    fn test_mock_writer_flush() {
        let mut writer = MockWriter::new();
        assert!(writer.flush().is_ok());
    }

    // Modifier tests
    #[test]
    fn test_modifier_empty() {
        let modifier = Modifier::empty();
        assert!(modifier.is_empty());
        assert!(!modifier.contains(Modifier::BOLD));
        assert!(!modifier.contains(Modifier::ITALIC));
    }

    #[test]
    fn test_modifier_bold() {
        let modifier = Modifier::BOLD;
        assert!(!modifier.is_empty());
        assert!(modifier.contains(Modifier::BOLD));
    }

    #[test]
    fn test_modifier_combined() {
        let modifier = Modifier::BOLD | Modifier::ITALIC;
        assert!(modifier.contains(Modifier::BOLD));
        assert!(modifier.contains(Modifier::ITALIC));
        assert!(!modifier.contains(Modifier::UNDERLINE));
    }

    // =========================================================================
    // Issue #175: Cursor tracking tests
    // =========================================================================

    #[test]
    fn test_render_state_cursor_default() {
        let state = RenderState::default();
        assert!(state.cursor.is_none());
    }

    #[test]
    fn test_render_state_cursor_tracking() {
        let mut state = RenderState::default();

        // Initially no cursor position
        assert!(state.cursor.is_none());

        // After setting cursor position
        state.cursor = Some((5, 10));
        assert_eq!(state.cursor, Some((5, 10)));

        // After advancing cursor (simulating char print)
        state.cursor = Some((6, 10));
        assert_eq!(state.cursor, Some((6, 10)));
    }

    #[test]
    fn test_cursor_position_after_normal_char() {
        // Normal ASCII character has width 1
        let ch = 'A';
        let width = crate::utils::unicode::char_width(ch) as u16;
        assert_eq!(width, 1);

        // Cursor should advance by 1
        let x: u16 = 5;
        let new_x = x.saturating_add(width);
        assert_eq!(new_x, 6);
    }

    #[test]
    fn test_cursor_position_after_wide_char() {
        // CJK character has width 2
        let ch = '한'; // Korean character
        let width = crate::utils::unicode::char_width(ch) as u16;
        assert_eq!(width, 2);

        // Cursor should advance by 2
        let x: u16 = 5;
        let new_x = x.saturating_add(width);
        assert_eq!(new_x, 7);
    }

    #[test]
    fn test_cursor_position_saturating_add() {
        // Test that cursor position doesn't overflow
        let x: u16 = u16::MAX - 1;
        let width: u16 = 2;
        let new_x = x.saturating_add(width);
        assert_eq!(new_x, u16::MAX);
    }

    #[test]
    fn test_cursor_skip_moveto_same_position() {
        let state = RenderState {
            cursor: Some((5, 10)),
            ..RenderState::default()
        };

        // When cursor is at (5, 10) and we want to draw at (5, 10),
        // MoveTo should be skipped
        let target = (5u16, 10u16);
        let should_skip = state.cursor == Some(target);
        assert!(should_skip);
    }

    #[test]
    fn test_cursor_emit_moveto_different_position() {
        let state = RenderState {
            cursor: Some((5, 10)),
            ..RenderState::default()
        };

        // When cursor is at (5, 10) and we want to draw at (6, 10),
        // MoveTo should NOT be skipped (different x)
        let target = (6u16, 10u16);
        let should_skip = state.cursor == Some(target);
        assert!(!should_skip);

        // Different row also needs MoveTo
        let target_diff_row = (5u16, 11u16);
        let should_skip_row = state.cursor == Some(target_diff_row);
        assert!(!should_skip_row);
    }

    #[test]
    fn test_cursor_emit_moveto_when_none() {
        let state = RenderState::default();

        // When cursor is None, MoveTo should always be emitted
        let target = (5u16, 10u16);
        let should_skip = state.cursor == Some(target);
        assert!(!should_skip);
    }

    #[test]
    fn test_contiguous_cells_cursor_tracking() {
        // Simulate drawing "ABC" at (0, 0)
        let mut state = RenderState::default();

        // First char 'A' at (0, 0) - MoveTo needed (cursor is None)
        assert!(state.cursor != Some((0, 0)));
        state.cursor = Some((1, 0)); // After 'A', cursor at (1, 0)

        // Second char 'B' at (1, 0) - MoveTo NOT needed
        assert!(state.cursor == Some((1, 0)));
        state.cursor = Some((2, 0)); // After 'B', cursor at (2, 0)

        // Third char 'C' at (2, 0) - MoveTo NOT needed
        assert!(state.cursor == Some((2, 0)));
        // After 'C', cursor would be at (3, 0)
    }

    // =========================================================================
    // Issue #174: render_dirty tests
    // =========================================================================

    #[test]
    fn test_render_dirty_only_diffs_dirty_regions() {
        let buf1 = Buffer::new(20, 20);
        let mut buf2 = Buffer::new(20, 20);

        // Make changes at two different locations
        buf2.set(5, 5, Cell::new('X')); // Inside dirty rect
        buf2.set(15, 15, Cell::new('Y')); // Outside dirty rect

        // Only diff the region containing (5, 5)
        let dirty_rects = vec![Rect::new(0, 0, 10, 10)];
        let changes = crate::render::diff::diff(&buf1, &buf2, &dirty_rects);

        // Should only find the change at (5, 5), not (15, 15)
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].x, 5);
        assert_eq!(changes[0].y, 5);
    }

    #[test]
    fn test_render_dirty_multiple_regions() {
        let buf1 = Buffer::new(20, 20);
        let mut buf2 = Buffer::new(20, 20);

        // Make changes in two different dirty regions
        buf2.set(2, 2, Cell::new('X'));
        buf2.set(15, 15, Cell::new('Y'));

        // Two dirty regions covering both changes
        let dirty_rects = vec![
            Rect::new(0, 0, 5, 5),   // Contains (2, 2)
            Rect::new(14, 14, 5, 5), // Contains (15, 15)
        ];
        let changes = crate::render::diff::diff(&buf1, &buf2, &dirty_rects);

        // Should find both changes
        assert_eq!(changes.len(), 2);
    }

    #[test]
    fn test_render_dirty_no_changes_in_dirty_region() {
        let buf1 = Buffer::new(20, 20);
        let mut buf2 = Buffer::new(20, 20);

        // Make change outside the dirty region
        buf2.set(15, 15, Cell::new('Z'));

        // Dirty region doesn't include the change
        let dirty_rects = vec![Rect::new(0, 0, 10, 10)];
        let changes = crate::render::diff::diff(&buf1, &buf2, &dirty_rects);

        // No changes detected (change is outside dirty region)
        assert!(changes.is_empty());
    }

    #[test]
    fn test_render_dirty_empty_dirty_rects_fallback() {
        // When no dirty rects provided, should fall back to full-screen diff
        let buf1 = Buffer::new(10, 10);
        let mut buf2 = Buffer::new(10, 10);
        buf2.set(5, 5, Cell::new('X'));

        let changes = crate::render::diff::diff(&buf1, &buf2, &[]);

        // Should find the change (full-screen fallback)
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].x, 5);
        assert_eq!(changes[0].y, 5);
    }

    #[test]
    fn test_render_dirty_overlapping_regions() {
        let buf1 = Buffer::new(20, 20);
        let mut buf2 = Buffer::new(20, 20);
        buf2.set(5, 5, Cell::new('X'));

        // Overlapping dirty rects both containing (5, 5)
        let dirty_rects = vec![Rect::new(0, 0, 10, 10), Rect::new(3, 3, 10, 10)];
        let changes = crate::render::diff::diff(&buf1, &buf2, &dirty_rects);

        // Should only report the change once (no duplicates)
        assert_eq!(changes.len(), 1);
    }
}