Skip to main content

presentar_terminal/direct/
diff_renderer.rs

1//! Differential renderer for optimized terminal I/O.
2//!
3//! Minimizes terminal escape sequences and syscalls by:
4//! - Only rendering dirty cells
5//! - Batching output to a buffer
6//! - Skipping redundant cursor moves
7//! - Caching current style state
8
9use super::cell_buffer::{CellBuffer, Modifiers};
10use crate::color::ColorMode;
11use crossterm::cursor::MoveTo;
12use crossterm::style::{
13    Attribute, Color as CrosstermColor, Print, ResetColor, SetAttribute, SetBackgroundColor,
14    SetForegroundColor,
15};
16use crossterm::{queue, QueueableCommand};
17use presentar_core::Color;
18use std::io::{self, BufWriter, Write};
19
20/// Current terminal style state.
21#[derive(Clone, Copy, Debug, PartialEq)]
22struct StyleState {
23    fg: Color,
24    bg: Color,
25    modifiers: Modifiers,
26}
27
28impl Default for StyleState {
29    fn default() -> Self {
30        Self {
31            fg: Color::WHITE,
32            bg: Color::BLACK,
33            modifiers: Modifiers::NONE,
34        }
35    }
36}
37
38/// Differential renderer that minimizes terminal I/O.
39///
40/// Tracks the current cursor position and style state to avoid
41/// redundant escape sequences.
42#[derive(Debug)]
43pub struct DiffRenderer {
44    /// Color mode for conversion.
45    color_mode: ColorMode,
46    /// Last known cursor position (`u16::MAX` = unknown).
47    cursor_x: u16,
48    cursor_y: u16,
49    /// Last known style state.
50    last_style: StyleState,
51    /// Statistics: number of cells written.
52    cells_written: usize,
53    /// Statistics: number of cursor moves.
54    cursor_moves: usize,
55    /// Statistics: number of style changes.
56    style_changes: usize,
57}
58
59impl Default for DiffRenderer {
60    fn default() -> Self {
61        Self::new()
62    }
63}
64
65impl DiffRenderer {
66    /// Create a new renderer.
67    #[must_use]
68    pub fn new() -> Self {
69        Self {
70            color_mode: ColorMode::detect(),
71            cursor_x: u16::MAX,
72            cursor_y: u16::MAX,
73            last_style: StyleState::default(),
74            cells_written: 0,
75            cursor_moves: 0,
76            style_changes: 0,
77        }
78    }
79
80    /// Create a renderer with specific color mode.
81    #[must_use]
82    pub fn with_color_mode(color_mode: ColorMode) -> Self {
83        Self {
84            color_mode,
85            cursor_x: u16::MAX,
86            cursor_y: u16::MAX,
87            last_style: StyleState::default(),
88            cells_written: 0,
89            cursor_moves: 0,
90            style_changes: 0,
91        }
92    }
93
94    /// Set the color mode.
95    pub fn set_color_mode(&mut self, mode: ColorMode) {
96        self.color_mode = mode;
97    }
98
99    /// Get the color mode.
100    #[must_use]
101    pub const fn color_mode(&self) -> ColorMode {
102        self.color_mode
103    }
104
105    /// Reset renderer state (call after terminal resize or clear).
106    pub fn reset(&mut self) {
107        self.cursor_x = u16::MAX;
108        self.cursor_y = u16::MAX;
109        self.last_style = StyleState::default();
110        self.cells_written = 0;
111        self.cursor_moves = 0;
112        self.style_changes = 0;
113    }
114
115    /// Get cells written in last flush.
116    #[must_use]
117    pub const fn cells_written(&self) -> usize {
118        self.cells_written
119    }
120
121    /// Get cursor moves in last flush.
122    #[must_use]
123    pub const fn cursor_moves(&self) -> usize {
124        self.cursor_moves
125    }
126
127    /// Get style changes in last flush.
128    #[must_use]
129    pub const fn style_changes(&self) -> usize {
130        self.style_changes
131    }
132
133    /// Convert presentar Color to crossterm Color.
134    fn to_crossterm_color(&self, color: Color) -> CrosstermColor {
135        self.color_mode.to_crossterm(color)
136    }
137
138    /// Flush dirty cells to the writer.
139    ///
140    /// Returns the number of cells written.
141    ///
142    /// # Errors
143    ///
144    /// Returns an error if writing to the writer fails.
145    pub fn flush<W: Write>(
146        &mut self,
147        buffer: &mut CellBuffer,
148        writer: &mut W,
149    ) -> io::Result<usize> {
150        debug_assert!(buffer.width() > 0, "buffer width must be positive");
151        debug_assert!(buffer.height() > 0, "buffer height must be positive");
152
153        // Reset statistics
154        self.cells_written = 0;
155        self.cursor_moves = 0;
156        self.style_changes = 0;
157
158        // Use buffered writer to batch syscalls
159        let mut buf_writer = BufWriter::with_capacity(8192, writer);
160
161        // Reset colors at start for clean state
162        queue!(buf_writer, ResetColor)?;
163        self.last_style = StyleState::default();
164
165        let width = buffer.width();
166
167        for idx in buffer.iter_dirty() {
168            let (x, y) = buffer.coords(idx);
169            let cell = &buffer.cells()[idx];
170
171            // Skip continuation cells
172            if cell.is_continuation() {
173                continue;
174            }
175
176            // Move cursor if needed
177            if self.cursor_x != x || self.cursor_y != y {
178                queue!(buf_writer, MoveTo(x, y))?;
179                self.cursor_x = x;
180                self.cursor_y = y;
181                self.cursor_moves += 1;
182            }
183
184            // Update style if needed
185            let new_style = StyleState {
186                fg: cell.fg,
187                bg: cell.bg,
188                modifiers: cell.modifiers,
189            };
190
191            if new_style != self.last_style {
192                self.apply_style(&mut buf_writer, new_style)?;
193                self.last_style = new_style;
194                self.style_changes += 1;
195            }
196
197            // Print symbol
198            queue!(buf_writer, Print(&cell.symbol))?;
199
200            // Update cursor position
201            self.cursor_x = self.cursor_x.saturating_add(cell.width() as u16);
202            if self.cursor_x >= width {
203                self.cursor_x = u16::MAX; // Unknown after wrap
204            }
205
206            self.cells_written += 1;
207        }
208
209        // Clear dirty flags
210        buffer.clear_dirty();
211
212        // Final flush
213        buf_writer.flush()?;
214
215        Ok(self.cells_written)
216    }
217
218    /// Apply style changes to the writer.
219    fn apply_style<W: Write>(&self, writer: &mut W, style: StyleState) -> io::Result<()> {
220        // Reset attributes FIRST (before setting colors!)
221        writer.queue(SetAttribute(Attribute::Reset))?;
222
223        // Foreground color
224        let fg = self.to_crossterm_color(style.fg);
225        writer.queue(SetForegroundColor(fg))?;
226
227        // Background color
228        let bg = self.to_crossterm_color(style.bg);
229        writer.queue(SetBackgroundColor(bg))?;
230
231        // Apply modifiers
232        if style.modifiers.contains(Modifiers::BOLD) {
233            writer.queue(SetAttribute(Attribute::Bold))?;
234        }
235        if style.modifiers.contains(Modifiers::ITALIC) {
236            writer.queue(SetAttribute(Attribute::Italic))?;
237        }
238        if style.modifiers.contains(Modifiers::UNDERLINE) {
239            writer.queue(SetAttribute(Attribute::Underlined))?;
240        }
241        if style.modifiers.contains(Modifiers::STRIKETHROUGH) {
242            writer.queue(SetAttribute(Attribute::CrossedOut))?;
243        }
244        if style.modifiers.contains(Modifiers::DIM) {
245            writer.queue(SetAttribute(Attribute::Dim))?;
246        }
247        if style.modifiers.contains(Modifiers::BLINK) {
248            writer.queue(SetAttribute(Attribute::SlowBlink))?;
249        }
250        if style.modifiers.contains(Modifiers::REVERSE) {
251            writer.queue(SetAttribute(Attribute::Reverse))?;
252        }
253        if style.modifiers.contains(Modifiers::HIDDEN) {
254            writer.queue(SetAttribute(Attribute::Hidden))?;
255        }
256
257        Ok(())
258    }
259
260    /// Render a full frame (marks all dirty then flushes).
261    ///
262    /// # Errors
263    ///
264    /// Returns an error if writing fails.
265    pub fn render_full<W: Write>(
266        &mut self,
267        buffer: &mut CellBuffer,
268        writer: &mut W,
269    ) -> io::Result<usize> {
270        buffer.mark_all_dirty();
271        self.flush(buffer, writer)
272    }
273}
274
275#[cfg(test)]
276mod tests {
277    use super::*;
278
279    #[test]
280    fn test_renderer_creation() {
281        let renderer = DiffRenderer::new();
282        assert_eq!(renderer.cursor_x, u16::MAX);
283        assert_eq!(renderer.cursor_y, u16::MAX);
284    }
285
286    #[test]
287    fn test_renderer_with_color_mode() {
288        let renderer = DiffRenderer::with_color_mode(ColorMode::Color256);
289        assert_eq!(renderer.color_mode(), ColorMode::Color256);
290    }
291
292    #[test]
293    fn test_renderer_set_color_mode() {
294        let mut renderer = DiffRenderer::new();
295        renderer.set_color_mode(ColorMode::Color16);
296        assert_eq!(renderer.color_mode(), ColorMode::Color16);
297    }
298
299    #[test]
300    fn test_renderer_reset() {
301        let mut renderer = DiffRenderer::new();
302        renderer.cursor_x = 10;
303        renderer.cursor_y = 5;
304        renderer.cells_written = 100;
305
306        renderer.reset();
307
308        assert_eq!(renderer.cursor_x, u16::MAX);
309        assert_eq!(renderer.cursor_y, u16::MAX);
310        assert_eq!(renderer.cells_written(), 0);
311    }
312
313    #[test]
314    fn test_renderer_flush_empty() {
315        let mut renderer = DiffRenderer::new();
316        let mut buffer = CellBuffer::new(10, 5);
317        let mut output = Vec::new();
318
319        let count = renderer.flush(&mut buffer, &mut output).unwrap();
320        assert_eq!(count, 0);
321    }
322
323    #[test]
324    fn test_renderer_flush_dirty_cells() {
325        let mut renderer = DiffRenderer::new();
326        let mut buffer = CellBuffer::new(10, 5);
327        buffer.update(5, 2, "X", Color::RED, Color::BLACK, Modifiers::NONE);
328        let mut output = Vec::new();
329
330        let count = renderer.flush(&mut buffer, &mut output).unwrap();
331        assert_eq!(count, 1);
332        assert!(output.len() > 0);
333    }
334
335    #[test]
336    fn test_renderer_flush_multiple_dirty() {
337        let mut renderer = DiffRenderer::new();
338        let mut buffer = CellBuffer::new(10, 5);
339        buffer.update(0, 0, "A", Color::WHITE, Color::BLACK, Modifiers::NONE);
340        buffer.update(5, 2, "B", Color::WHITE, Color::BLACK, Modifiers::NONE);
341        buffer.update(9, 4, "C", Color::WHITE, Color::BLACK, Modifiers::NONE);
342        let mut output = Vec::new();
343
344        let count = renderer.flush(&mut buffer, &mut output).unwrap();
345        assert_eq!(count, 3);
346        assert_eq!(renderer.cursor_moves(), 3);
347    }
348
349    #[test]
350    fn test_renderer_flush_adjacent_cells() {
351        let mut renderer = DiffRenderer::new();
352        let mut buffer = CellBuffer::new(10, 5);
353        // Adjacent cells should minimize cursor moves
354        buffer.update(0, 0, "A", Color::WHITE, Color::BLACK, Modifiers::NONE);
355        buffer.update(1, 0, "B", Color::WHITE, Color::BLACK, Modifiers::NONE);
356        buffer.update(2, 0, "C", Color::WHITE, Color::BLACK, Modifiers::NONE);
357        let mut output = Vec::new();
358
359        let count = renderer.flush(&mut buffer, &mut output).unwrap();
360        assert_eq!(count, 3);
361        // Should only need one cursor move (to start)
362        assert_eq!(renderer.cursor_moves(), 1);
363    }
364
365    #[test]
366    fn test_renderer_style_changes() {
367        let mut renderer = DiffRenderer::new();
368        let mut buffer = CellBuffer::new(10, 5);
369        buffer.update(0, 0, "A", Color::RED, Color::BLACK, Modifiers::NONE);
370        buffer.update(1, 0, "B", Color::BLUE, Color::BLACK, Modifiers::NONE);
371        let mut output = Vec::new();
372
373        renderer.flush(&mut buffer, &mut output).unwrap();
374        assert_eq!(renderer.style_changes(), 2);
375    }
376
377    #[test]
378    fn test_renderer_same_style_no_change() {
379        let mut renderer = DiffRenderer::new();
380        let mut buffer = CellBuffer::new(10, 5);
381        // Use non-default colors to force a style change on first cell
382        buffer.update(0, 0, "A", Color::RED, Color::BLUE, Modifiers::NONE);
383        buffer.update(1, 0, "B", Color::RED, Color::BLUE, Modifiers::NONE);
384        let mut output = Vec::new();
385
386        renderer.flush(&mut buffer, &mut output).unwrap();
387        // First cell triggers style change, second cell has same style = 1 total
388        assert_eq!(renderer.style_changes(), 1);
389    }
390
391    #[test]
392    fn test_renderer_with_modifiers() {
393        let mut renderer = DiffRenderer::new();
394        let mut buffer = CellBuffer::new(10, 5);
395        buffer.update(
396            0,
397            0,
398            "X",
399            Color::WHITE,
400            Color::BLACK,
401            Modifiers::BOLD | Modifiers::ITALIC,
402        );
403        let mut output = Vec::new();
404
405        let count = renderer.flush(&mut buffer, &mut output).unwrap();
406        assert_eq!(count, 1);
407        // Output should contain attribute sequences
408        assert!(output.len() > 5);
409    }
410
411    #[test]
412    fn test_renderer_all_modifiers() {
413        let mut renderer = DiffRenderer::new();
414        let mut buffer = CellBuffer::new(10, 5);
415        let all_mods = Modifiers::BOLD
416            | Modifiers::ITALIC
417            | Modifiers::UNDERLINE
418            | Modifiers::STRIKETHROUGH
419            | Modifiers::DIM
420            | Modifiers::BLINK
421            | Modifiers::REVERSE
422            | Modifiers::HIDDEN;
423        buffer.update(0, 0, "X", Color::WHITE, Color::BLACK, all_mods);
424        let mut output = Vec::new();
425
426        renderer.flush(&mut buffer, &mut output).unwrap();
427        assert!(output.len() > 10);
428    }
429
430    #[test]
431    fn test_renderer_render_full() {
432        let mut renderer = DiffRenderer::new();
433        let mut buffer = CellBuffer::new(10, 5);
434        buffer.clear_dirty();
435
436        let mut output = Vec::new();
437        let count = renderer.render_full(&mut buffer, &mut output).unwrap();
438
439        // All 50 cells should be rendered
440        assert_eq!(count, 50);
441    }
442
443    #[test]
444    fn test_renderer_skip_continuation() {
445        let mut renderer = DiffRenderer::new();
446        let mut buffer = CellBuffer::new(10, 5);
447
448        // Set a wide character
449        buffer.update(0, 0, "日", Color::WHITE, Color::BLACK, Modifiers::NONE);
450        // Mark continuation
451        if let Some(cell) = buffer.get_mut(1, 0) {
452            cell.make_continuation();
453        }
454        buffer.mark_dirty(1, 0);
455
456        let mut output = Vec::new();
457        let count = renderer.flush(&mut buffer, &mut output).unwrap();
458
459        // Only the main cell should be written
460        assert_eq!(count, 1);
461    }
462
463    #[test]
464    fn test_renderer_cursor_wrap() {
465        let mut renderer = DiffRenderer::new();
466        let mut buffer = CellBuffer::new(5, 2);
467        buffer.update(4, 0, "X", Color::WHITE, Color::BLACK, Modifiers::NONE);
468        let mut output = Vec::new();
469
470        renderer.flush(&mut buffer, &mut output).unwrap();
471        // After writing at x=4, cursor should wrap/be unknown
472        assert_eq!(renderer.cursor_x, u16::MAX);
473    }
474
475    #[test]
476    fn test_renderer_statistics() {
477        let mut renderer = DiffRenderer::new();
478        let mut buffer = CellBuffer::new(10, 6);
479        buffer.update(0, 0, "A", Color::RED, Color::BLACK, Modifiers::NONE);
480        buffer.update(5, 5, "B", Color::BLUE, Color::WHITE, Modifiers::BOLD);
481        let mut output = Vec::new();
482
483        renderer.flush(&mut buffer, &mut output).unwrap();
484
485        assert_eq!(renderer.cells_written(), 2);
486        assert!(renderer.cursor_moves() >= 2);
487        assert!(renderer.style_changes() >= 2);
488    }
489
490    #[test]
491    fn test_renderer_default() {
492        let renderer = DiffRenderer::default();
493        assert_eq!(renderer.cursor_x, u16::MAX);
494    }
495
496    #[test]
497    fn test_style_state_default() {
498        let state = StyleState::default();
499        assert_eq!(state.fg, Color::WHITE);
500        assert_eq!(state.bg, Color::BLACK);
501        assert!(state.modifiers.is_empty());
502    }
503
504    #[test]
505    fn test_style_state_equality() {
506        let s1 = StyleState::default();
507        let s2 = StyleState::default();
508        assert_eq!(s1, s2);
509
510        let s3 = StyleState {
511            fg: Color::RED,
512            ..Default::default()
513        };
514        assert_ne!(s1, s3);
515    }
516}