termtui 0.1.0

A framework for building beautiful, responsive terminal user interfaces with a DOM-style hierarchical approach
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! Optimized terminal renderer that applies cell updates efficiently.
//!
//! This module is responsible for translating cell updates into terminal
//! commands, minimizing the number of escape sequences and I/O operations
//! to achieve optimal performance and eliminate flicker.

use crate::buffer::{Cell, CellStyle, CellUpdate};
use crate::style::Color;
use crossterm::{
    ExecutableCommand, cursor,
    style::{Attribute, Print, ResetColor, SetAttribute, SetBackgroundColor, SetForegroundColor},
};
use std::io::{self, Write};

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// Optimized terminal renderer that tracks terminal state to minimize commands.
///
/// By tracking the current cursor position, colors, and attributes, we can
/// avoid sending redundant commands to the terminal, significantly improving
/// performance.
///
/// ```text
/// Terminal State Tracking:
/// ┌─────────────────────────────────────┐
/// │ TerminalRenderer                    │
/// │                                     │
/// │  current_pos: (10, 5)               │ ◄── Tracks cursor to avoid
/// │  current_fg: Some(Red)              │     redundant MoveTo commands
/// │  current_bg: Some(Blue)             │
/// │  current_style: Bold                │ ◄── Only sends style changes
/// │                                     │     when actually different
/// └─────────────────────────────────────┘
/// ```
pub struct TerminalRenderer {
    /// Output stream (usually stdout)
    stdout: io::Stdout,

    /// Current cursor position (x, y)
    current_pos: Option<(u16, u16)>,

    /// Current foreground color
    current_fg: Option<Color>,

    /// Current background color
    current_bg: Option<Color>,

    /// Current style attributes
    current_style: CellStyle,

    /// Whether synchronized output is supported
    supports_synchronized: bool,
}

/// A terminal command abstraction for batching operations.
#[derive(Debug)]
pub enum TerminalCommand {
    /// Move cursor to position
    MoveTo(u16, u16),

    /// Set foreground and background colors
    SetColors {
        fg: Option<Color>,
        bg: Option<Color>,
    },

    /// Print text at current position
    Print(String),

    /// Set style attributes
    SetStyle(CellStyle),

    /// Reset all styling
    Reset,
}

/// Batches cell updates into optimized terminal commands.
pub struct UpdateBatcher {
    /// The updates to process
    updates: Vec<CellUpdate>,
}

/// A run of consecutive cells with the same style.
struct Run {
    x: u16,
    y: u16,
    cells: Vec<Cell>,
    fg: Option<Color>,
    bg: Option<Color>,
    style: CellStyle,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl TerminalRenderer {
    /// Creates a new terminal renderer.
    pub fn new() -> Self {
        Self {
            stdout: io::stdout(),
            current_pos: None,
            current_fg: None,
            current_bg: None,
            current_style: CellStyle::default(),
            supports_synchronized: Self::detect_synchronized_output(),
        }
    }

    /// Detects if the terminal supports synchronized output mode.
    fn detect_synchronized_output() -> bool {
        // For now, we'll enable it for known terminals
        // In the future, we could do actual capability detection
        std::env::var("TERM_PROGRAM").is_ok_and(|term| {
            matches!(
                term.as_str(),
                "iTerm.app" | "kitty" | "alacritty" | "wezterm"
            )
        })
    }

    /// Applies a list of cell updates to the terminal.
    ///
    /// ```text
    /// Update Flow:
    /// ┌─────────────┐     ┌──────────────┐     ┌─────────────┐
    /// │ CellUpdates │ ──▶ │ UpdateBatcher│ ──▶ │  Terminal   │
    /// │ [(x,y,cell)]│     │  optimize()  │     │  Commands   │
    /// └─────────────┘     └──────────────┘     └─────────────┘
    ///                             │                     │
    ///                             ▼                     ▼
    ///                     ┌──────────────┐     ┌─────────────┐
    ///                     │ Sorted &     │     │ MoveTo(x,y) │
    ///                     │ Grouped Runs │     │ SetColors   │
    ///                     └──────────────┘     │ Print(text) │
    ///                                          └─────────────┘
    /// ```
    pub fn apply_updates(&mut self, updates: Vec<CellUpdate>) -> io::Result<()> {
        if updates.is_empty() {
            return Ok(());
        }

        // Use synchronized output if available
        if self.supports_synchronized {
            self.apply_updates_synchronized(updates)
        } else {
            self.apply_updates_direct(updates)
        }
    }

    /// Applies updates without terminal optimizations (for debugging).
    pub fn apply_updates_direct(&mut self, updates: Vec<CellUpdate>) -> io::Result<()> {
        // Process updates without optimization
        for update in updates {
            match update {
                CellUpdate::Single { x, y, cell } => {
                    self.stdout.execute(cursor::MoveTo(x, y))?;
                    self.apply_cell_style(&cell)?;
                    self.stdout.execute(Print(cell.char))?;
                }
                CellUpdate::Run { x, y, cells } => {
                    for (i, cell) in cells.iter().enumerate() {
                        self.stdout.execute(cursor::MoveTo(x + i as u16, y))?;
                        self.apply_cell_style(cell)?;
                        self.stdout.execute(Print(cell.char))?;
                    }
                }
            }
        }

        self.stdout.execute(ResetColor)?;
        self.stdout.execute(SetAttribute(Attribute::Reset))?;
        self.stdout.flush()?;
        Ok(())
    }

    /// Draws the entire buffer to terminal without optimization.
    pub fn draw_full_buffer(&mut self, buffer: &crate::buffer::ScreenBuffer) -> io::Result<()> {
        let (width, height) = buffer.dimensions();

        for y in 0..height {
            for x in 0..width {
                if let Some(cell) = buffer.get_cell(x, y) {
                    self.stdout.execute(cursor::MoveTo(x, y))?;
                    self.apply_cell_style(cell)?;
                    self.stdout.execute(Print(cell.char))?;
                }
            }
        }

        self.stdout.execute(ResetColor)?;
        self.stdout.execute(SetAttribute(Attribute::Reset))?;
        self.stdout.flush()?;
        Ok(())
    }

    /// Converts our Color enum to crossterm color.
    pub fn color_to_crossterm(&self, color: Color) -> crossterm::style::Color {
        match color {
            Color::Black => crossterm::style::Color::Black,
            Color::Red => crossterm::style::Color::DarkRed,
            Color::Green => crossterm::style::Color::DarkGreen,
            Color::Yellow => crossterm::style::Color::DarkYellow,
            Color::Blue => crossterm::style::Color::DarkBlue,
            Color::Magenta => crossterm::style::Color::DarkMagenta,
            Color::Cyan => crossterm::style::Color::DarkCyan,
            Color::White => crossterm::style::Color::Grey,
            Color::BrightBlack => crossterm::style::Color::DarkGrey,
            Color::BrightRed => crossterm::style::Color::Red,
            Color::BrightGreen => crossterm::style::Color::Green,
            Color::BrightYellow => crossterm::style::Color::Yellow,
            Color::BrightBlue => crossterm::style::Color::Blue,
            Color::BrightMagenta => crossterm::style::Color::Magenta,
            Color::BrightCyan => crossterm::style::Color::Cyan,
            Color::BrightWhite => crossterm::style::Color::White,
            Color::Rgb(r, g, b) => crossterm::style::Color::Rgb { r, g, b },
        }
    }

    /// Applies cell styling to terminal.
    fn apply_cell_style(&mut self, cell: &Cell) -> io::Result<()> {
        // Always reset attributes first to prevent bleeding from previous cells
        self.stdout.execute(SetAttribute(Attribute::Reset))?;

        // Apply colors
        if let Some(fg) = &cell.fg {
            self.stdout
                .execute(SetForegroundColor(self.color_to_crossterm(*fg)))?;
        }
        if let Some(bg) = &cell.bg {
            self.stdout
                .execute(SetBackgroundColor(self.color_to_crossterm(*bg)))?;
        }

        // Apply text styling attributes
        if cell.style.bold {
            self.stdout.execute(SetAttribute(Attribute::Bold))?;
        }
        if cell.style.italic {
            self.stdout.execute(SetAttribute(Attribute::Italic))?;
        }
        if cell.style.underline {
            self.stdout.execute(SetAttribute(Attribute::Underlined))?;
        }
        if cell.style.strikethrough {
            self.stdout.execute(SetAttribute(Attribute::CrossedOut))?;
        }
        Ok(())
    }

    /// Applies updates with synchronized output mode for atomic rendering.
    ///
    /// ```text
    /// Without Synchronization:         With Synchronization:
    /// ┌──────────────────────┐        ┌──────────────────────┐
    /// │ MoveTo ──────────────┼───┐    │ Begin Sync (?2026h)  │
    /// │ SetColor ────────────┼─┐ │    │ ┌──────────────────┐ │
    /// │ Print "Hello" ───────┼┐│ │    │ │ MoveTo           │ │
    /// │ MoveTo ──────────────┼┼┼─┤    │ │ SetColor         │ │◄─ All updates
    /// │ Print "World" ───────┼┼┼┼┤    │ │ Print "Hello"    │ │  buffered
    /// └──────────────────────┘││││    │ │ MoveTo           │ │
    ///   Visible tearing ──────┘│││    │ │ Print "World"    │ │
    ///   as each command ───────┘││    │ └──────────────────┘ │
    ///   executes ───────────────┘│    │ End Sync (?2026l)    │
    ///   immediately ─────────────┘    └──────────────────────┘
    ///                                   Atomic update - no tearing
    /// ```
    fn apply_updates_synchronized(&mut self, updates: Vec<CellUpdate>) -> io::Result<()> {
        // Begin synchronized update
        self.stdout.execute(Print("\x1b[?2026h"))?;

        let result = self.apply_updates_optimized(updates);

        // End synchronized update
        self.stdout.execute(Print("\x1b[?2026l"))?;
        self.stdout.flush()?;

        result
    }

    /// Applies updates with full terminal optimizations.
    fn apply_updates_optimized(&mut self, updates: Vec<CellUpdate>) -> io::Result<()> {
        // Convert updates to optimized commands
        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        // Apply each command
        for cmd in commands {
            self.apply_command(cmd)?;
        }

        self.stdout.flush()?;
        Ok(())
    }

    /// Applies a single terminal command.
    fn apply_command(&mut self, cmd: TerminalCommand) -> io::Result<()> {
        match cmd {
            TerminalCommand::MoveTo(x, y) => {
                if self.current_pos != Some((x, y)) {
                    self.stdout.execute(cursor::MoveTo(x, y))?;
                    self.current_pos = Some((x, y));
                }
            }
            TerminalCommand::SetColors { fg, bg } => {
                self.set_colors(fg, bg)?;
            }
            TerminalCommand::Print(text) => {
                self.stdout.execute(Print(&text))?;
                // Update cursor position
                if let Some((x, y)) = self.current_pos {
                    self.current_pos = Some((x + text.len() as u16, y));
                }
            }
            TerminalCommand::SetStyle(style) => {
                self.set_style(style)?;
            }
            TerminalCommand::Reset => {
                self.stdout.execute(ResetColor)?;
                self.stdout.execute(SetAttribute(Attribute::Reset))?;
                self.current_fg = None;
                self.current_bg = None;
                self.current_style = CellStyle::default();
            }
        }
        Ok(())
    }

    /// Sets colors only if they've changed.
    fn set_colors(&mut self, fg: Option<Color>, bg: Option<Color>) -> io::Result<()> {
        // Handle foreground color
        if fg != self.current_fg {
            match fg {
                Some(color) => {
                    self.stdout
                        .execute(SetForegroundColor(to_crossterm_color(color)))?;
                }
                None => {
                    // Reset to default foreground (usually white/gray)
                    // We use the terminal's default foreground explicitly
                    self.stdout
                        .execute(SetForegroundColor(crossterm::style::Color::Reset))?;
                }
            }
            self.current_fg = fg;
        }

        // Handle background color
        if bg != self.current_bg {
            match bg {
                Some(color) => {
                    self.stdout
                        .execute(SetBackgroundColor(to_crossterm_color(color)))?;
                }
                None => {
                    // Reset to default background (usually black/transparent)
                    // We use the terminal's default background explicitly
                    self.stdout
                        .execute(SetBackgroundColor(crossterm::style::Color::Reset))?;
                }
            }
            self.current_bg = bg;
        }

        Ok(())
    }

    /// Sets style attributes only if they've changed.
    fn set_style(&mut self, style: CellStyle) -> io::Result<()> {
        if style != self.current_style {
            // Always reset attributes when changing style to ensure clean state
            self.stdout.execute(SetAttribute(Attribute::Reset))?;

            // Apply new attributes if any are needed
            if style.bold {
                self.stdout.execute(SetAttribute(Attribute::Bold))?;
            }
            if style.italic {
                self.stdout.execute(SetAttribute(Attribute::Italic))?;
            }
            if style.underline {
                self.stdout.execute(SetAttribute(Attribute::Underlined))?;
            }
            if style.strikethrough {
                self.stdout.execute(SetAttribute(Attribute::CrossedOut))?;
            }

            self.current_style = style;
        }
        Ok(())
    }

    /// Resets the renderer state.
    pub fn reset(&mut self) -> io::Result<()> {
        self.apply_command(TerminalCommand::Reset)
    }
}

impl UpdateBatcher {
    /// Creates a new update batcher.
    pub fn new(updates: Vec<CellUpdate>) -> Self {
        Self { updates }
    }

    /// Optimizes updates into efficient terminal commands.
    ///
    /// This performs several optimizations:
    /// 1. Sorts updates by position for better cursor movement
    /// 2. Groups consecutive cells with same style into runs
    /// 3. Minimizes style changes
    ///
    /// ```text
    /// Input Updates:              Optimization Process:           Output Commands:
    /// ┌─────────────┐            ┌─────────────────┐            ┌──────────────┐
    /// │ (5,2) 'C'   │            │ Sort by y,x:    │            │ MoveTo(0,0)  │
    /// │ (0,0) 'A'   │ ─────────▶ │ (0,0) → (1,0)   │ ─────────▶ │ SetColors    │
    /// │ (1,0) 'B'   │   Sort &   │ (3,1) → (5,2)   │  Generate  │ Print("AB")  │
    /// │ (3,1) 'D'   │   Group    │                 │  Commands  │ MoveTo(3,1)  │
    /// └─────────────┘            │ Group into runs │            │ Print("D")   │
    ///                            └─────────────────┘            └──────────────┘
    /// ```
    pub fn optimize(mut self) -> Vec<TerminalCommand> {
        let mut commands = Vec::new();

        // Sort updates by position (top-to-bottom, left-to-right)
        self.updates.sort_by_key(|update| match update {
            CellUpdate::Single { x, y, .. } => (*y, *x),
            CellUpdate::Run { x, y, .. } => (*y, *x),
        });

        // Group updates into runs where possible
        let runs = self.group_into_runs();

        // Convert runs to commands
        for run in runs {
            commands.extend(run.into_commands());
        }

        commands
    }

    /// Groups updates into runs of consecutive cells with same style.
    ///
    /// ```text
    /// Single Updates:                     Grouped Runs:
    /// ┌────────────────┐                 ┌─────────────────────┐
    /// │ (0,0) 'H' Red  │                 │ Run @ (0,0):        │
    /// │ (1,0) 'e' Red  │ ──────────▶     │   "Hello" (Red)     │
    /// │ (2,0) 'l' Red  │  Group same     ├─────────────────────┤
    /// │ (3,0) 'l' Red  │  style cells    │ Run @ (0,1):        │
    /// │ (4,0) 'o' Red  │                 │   "World" (Blue)    │
    /// │ (0,1) 'W' Blue │                 └─────────────────────┘
    /// │ (1,1) 'o' Blue │
    /// │ (2,1) 'r' Blue │     ↑ Consecutive cells with same color/style
    /// │ (3,1) 'l' Blue │     │ are merged into single Print command
    /// │ (4,1) 'd' Blue │     │
    /// └────────────────┘     └── Reduces terminal commands significantly
    /// ```
    fn group_into_runs(self) -> Vec<Run> {
        let mut runs = Vec::new();
        let mut current_run: Option<Run> = None;

        for update in self.updates {
            match update {
                CellUpdate::Single { x, y, cell } => {
                    // Check if we can add to current run
                    if let Some(ref mut run) = current_run
                        && run.can_append(x, y, &cell)
                    {
                        run.cells.push(cell);
                        continue;
                    }

                    // Start new run
                    if let Some(run) = current_run.take() {
                        runs.push(run);
                    }
                    current_run = Some(Run::new(x, y, cell));
                }
                CellUpdate::Run { x, y, cells } => {
                    // Flush current run
                    if let Some(run) = current_run.take() {
                        runs.push(run);
                    }
                    // Add pre-made run
                    runs.push(Run {
                        x,
                        y,
                        cells,
                        fg: None,
                        bg: None,
                        style: CellStyle::default(),
                    });
                }
            }
        }

        // Don't forget the last run
        if let Some(run) = current_run {
            runs.push(run);
        }

        runs
    }
}

impl Run {
    /// Creates a new run starting with a single cell.
    fn new(x: u16, y: u16, cell: Cell) -> Self {
        let fg = cell.fg;
        let bg = cell.bg;
        let style = cell.style.clone();
        Self {
            x,
            y,
            cells: vec![cell],
            fg,
            bg,
            style,
        }
    }

    /// Checks if a cell can be appended to this run.
    ///
    /// ```text
    /// Current Run:                   New Cell:              Can Append?
    /// ┌──────────────────┐          ┌─────────────┐
    /// │ @ (5,10)         │          │ @ (8,10)    │        ✓ YES - Consecutive
    /// │ "ABC" (len=3)    │    vs    │ 'D' Red/Blu │          position & same
    /// │ Red fg, Blue bg  │          │             │          style
    /// └──────────────────┘          └─────────────┘
    ///        ↓                             ↓
    ///   Next expected: (8,10)         Actual: (8,10) ✓
    ///
    /// ┌──────────────────┐          ┌─────────────┐
    /// │ @ (5,10)         │          │ @ (9,10)    │        ✗ NO - Not consecutive
    /// │ "ABC" (len=3)    │    vs    │ 'E' Red/Blu │          (gap in position)
    /// │ Red fg, Blue bg  │          │             │
    /// └──────────────────┘          └─────────────┘
    ///        ↓                             ↓
    ///   Next expected: (8,10)         Actual: (9,10) ✗
    /// ```
    fn can_append(&self, x: u16, y: u16, cell: &Cell) -> bool {
        // Must be on same line and consecutive
        if y != self.y || x != self.x + self.cells.len() as u16 {
            return false;
        }
        // Must have same style
        cell.fg == self.fg && cell.bg == self.bg && cell.style == self.style
    }

    /// Converts this run to terminal commands.
    fn into_commands(self) -> Vec<TerminalCommand> {
        let has_styles = self.style != CellStyle::default();

        let mut commands = vec![
            TerminalCommand::MoveTo(self.x, self.y),
            TerminalCommand::SetColors {
                fg: self.fg,
                bg: self.bg,
            },
            TerminalCommand::SetStyle(self.style),
        ];

        // Build string from cells
        let text: String = self.cells.iter().map(|c| c.char).collect();
        commands.push(TerminalCommand::Print(text));

        // Reset styles after printing if any non-default styles were applied
        if has_styles {
            commands.push(TerminalCommand::Reset);
        }

        commands
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

/// Converts our Color enum to crossterm's Color type.
fn to_crossterm_color(color: Color) -> crossterm::style::Color {
    match color {
        Color::Black => crossterm::style::Color::Black,
        Color::Red => crossterm::style::Color::DarkRed,
        Color::Green => crossterm::style::Color::DarkGreen,
        Color::Yellow => crossterm::style::Color::DarkYellow,
        Color::Blue => crossterm::style::Color::DarkBlue,
        Color::Magenta => crossterm::style::Color::DarkMagenta,
        Color::Cyan => crossterm::style::Color::DarkCyan,
        Color::White => crossterm::style::Color::Grey,
        Color::BrightBlack => crossterm::style::Color::DarkGrey,
        Color::BrightRed => crossterm::style::Color::Red,
        Color::BrightGreen => crossterm::style::Color::Green,
        Color::BrightYellow => crossterm::style::Color::Yellow,
        Color::BrightBlue => crossterm::style::Color::Blue,
        Color::BrightMagenta => crossterm::style::Color::Magenta,
        Color::BrightCyan => crossterm::style::Color::Cyan,
        Color::BrightWhite => crossterm::style::Color::White,
        Color::Rgb(r, g, b) => crossterm::style::Color::Rgb { r, g, b },
    }
}

//--------------------------------------------------------------------------------------------------
// Trait Implementations
//--------------------------------------------------------------------------------------------------

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

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::buffer::{Cell, CellStyle, CellUpdate};
    use crate::style::Color;

    #[test]
    fn test_update_batcher_single_cell() {
        let updates = vec![CellUpdate::Single {
            x: 5,
            y: 10,
            cell: Cell {
                char: 'A',
                fg: Some(Color::Red),
                bg: Some(Color::Blue),
                style: CellStyle::default(),
            },
        }];

        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        assert_eq!(commands.len(), 4);
        assert!(matches!(commands[0], TerminalCommand::MoveTo(5, 10)));
        assert!(matches!(
            commands[1],
            TerminalCommand::SetColors {
                fg: Some(Color::Red),
                bg: Some(Color::Blue)
            }
        ));
        assert!(matches!(commands[2], TerminalCommand::SetStyle(_)));
        assert!(matches!(commands[3], TerminalCommand::Print(ref s) if s == "A"));
    }

    #[test]
    fn test_update_batcher_consecutive_cells_same_style() {
        let style = CellStyle::default();
        let updates = vec![
            CellUpdate::Single {
                x: 0,
                y: 0,
                cell: Cell {
                    char: 'H',
                    fg: Some(Color::Green),
                    bg: None,
                    style: style.clone(),
                },
            },
            CellUpdate::Single {
                x: 1,
                y: 0,
                cell: Cell {
                    char: 'e',
                    fg: Some(Color::Green),
                    bg: None,
                    style: style.clone(),
                },
            },
            CellUpdate::Single {
                x: 2,
                y: 0,
                cell: Cell {
                    char: 'l',
                    fg: Some(Color::Green),
                    bg: None,
                    style: style.clone(),
                },
            },
            CellUpdate::Single {
                x: 3,
                y: 0,
                cell: Cell {
                    char: 'l',
                    fg: Some(Color::Green),
                    bg: None,
                    style: style.clone(),
                },
            },
            CellUpdate::Single {
                x: 4,
                y: 0,
                cell: Cell {
                    char: 'o',
                    fg: Some(Color::Green),
                    bg: None,
                    style,
                },
            },
        ];

        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        // Should be optimized into a single run
        assert_eq!(commands.len(), 4);
        assert!(matches!(commands[0], TerminalCommand::MoveTo(0, 0)));
        assert!(matches!(commands[3], TerminalCommand::Print(ref s) if s == "Hello"));
    }

    #[test]
    fn test_update_batcher_different_styles() {
        let updates = vec![
            CellUpdate::Single {
                x: 0,
                y: 0,
                cell: Cell {
                    char: 'A',
                    fg: Some(Color::Red),
                    bg: None,
                    style: CellStyle::default(),
                },
            },
            CellUpdate::Single {
                x: 1,
                y: 0,
                cell: Cell {
                    char: 'B',
                    fg: Some(Color::Blue),
                    bg: None,
                    style: CellStyle::default(),
                },
            },
        ];

        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        // Should create two separate runs due to different colors
        // Each run needs: MoveTo, SetColors, SetStyle, Print = 4 commands
        // Total: 8 commands (4 for 'A' with Red, 4 for 'B' with Blue)
        assert_eq!(commands.len(), 8);

        // First run for 'A' with Red color
        assert!(matches!(commands[0], TerminalCommand::MoveTo(0, 0)));
        assert!(matches!(
            commands[1],
            TerminalCommand::SetColors {
                fg: Some(Color::Red),
                bg: None
            }
        ));
        assert!(matches!(commands[2], TerminalCommand::SetStyle(_)));
        assert!(matches!(commands[3], TerminalCommand::Print(ref s) if s == "A"));

        // Second run for 'B' with Blue color
        assert!(matches!(commands[4], TerminalCommand::MoveTo(1, 0)));
        assert!(matches!(
            commands[5],
            TerminalCommand::SetColors {
                fg: Some(Color::Blue),
                bg: None
            }
        ));
        assert!(matches!(commands[6], TerminalCommand::SetStyle(_)));
        assert!(matches!(commands[7], TerminalCommand::Print(ref s) if s == "B"));
    }

    #[test]
    fn test_update_batcher_sorting() {
        let updates = vec![
            CellUpdate::Single {
                x: 5,
                y: 2,
                cell: Cell::new('C'),
            },
            CellUpdate::Single {
                x: 0,
                y: 0,
                cell: Cell::new('A'),
            },
            CellUpdate::Single {
                x: 3,
                y: 1,
                cell: Cell::new('B'),
            },
        ];

        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        // First command should be MoveTo(0, 0) due to sorting
        assert!(matches!(commands[0], TerminalCommand::MoveTo(0, 0)));
    }

    #[test]
    fn test_update_batcher_run_type() {
        let cells = vec![
            Cell::new('T'),
            Cell::new('e'),
            Cell::new('s'),
            Cell::new('t'),
        ];

        let updates = vec![CellUpdate::Run { x: 10, y: 5, cells }];

        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        assert!(matches!(commands[0], TerminalCommand::MoveTo(10, 5)));
        assert!(matches!(commands[3], TerminalCommand::Print(ref s) if s == "Test"));
    }

    #[test]
    fn test_run_can_append() {
        let cell1 = Cell {
            char: 'A',
            fg: Some(Color::Red),
            bg: Some(Color::Blue),
            style: CellStyle::default(),
        };

        let run = Run::new(5, 10, cell1.clone());

        // Same style, consecutive position - should append
        let cell2 = Cell {
            char: 'B',
            fg: Some(Color::Red),
            bg: Some(Color::Blue),
            style: CellStyle::default(),
        };
        assert!(run.can_append(6, 10, &cell2));

        // Different line - should not append
        assert!(!run.can_append(6, 11, &cell2));

        // Non-consecutive position - should not append
        assert!(!run.can_append(8, 10, &cell2));

        // Different color - should not append
        let cell3 = Cell {
            char: 'C',
            fg: Some(Color::Green),
            bg: Some(Color::Blue),
            style: CellStyle::default(),
        };
        assert!(!run.can_append(6, 10, &cell3));
    }

    #[test]
    fn test_run_with_bold_style() {
        let style = CellStyle {
            bold: true,
            ..Default::default()
        };

        let updates = vec![CellUpdate::Single {
            x: 0,
            y: 0,
            cell: Cell {
                char: 'B',
                fg: None,
                bg: None,
                style,
            },
        }];

        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();

        // Should include SetStyle command with bold
        let has_bold_style = commands
            .iter()
            .any(|cmd| matches!(cmd, TerminalCommand::SetStyle(style) if style.bold));
        assert!(has_bold_style);
    }

    #[test]
    fn test_terminal_command_types() {
        // Test MoveTo
        let move_cmd = TerminalCommand::MoveTo(10, 20);
        assert!(matches!(move_cmd, TerminalCommand::MoveTo(10, 20)));

        // Test SetColors
        let color_cmd = TerminalCommand::SetColors {
            fg: Some(Color::Red),
            bg: Some(Color::Blue),
        };
        assert!(matches!(
            color_cmd,
            TerminalCommand::SetColors {
                fg: Some(Color::Red),
                bg: Some(Color::Blue)
            }
        ));

        // Test Print
        let print_cmd = TerminalCommand::Print("Hello".to_string());
        assert!(matches!(print_cmd, TerminalCommand::Print(ref s) if s == "Hello"));

        // Test Reset
        let reset_cmd = TerminalCommand::Reset;
        assert!(matches!(reset_cmd, TerminalCommand::Reset));
    }

    #[test]
    fn test_to_crossterm_color() {
        assert_eq!(
            to_crossterm_color(Color::Red),
            crossterm::style::Color::DarkRed
        );
        assert_eq!(
            to_crossterm_color(Color::BrightRed),
            crossterm::style::Color::Red
        );
        assert_eq!(
            to_crossterm_color(Color::Rgb(100, 150, 200)),
            crossterm::style::Color::Rgb {
                r: 100,
                g: 150,
                b: 200
            }
        );
    }

    #[test]
    fn test_empty_updates() {
        let updates = vec![];
        let batcher = UpdateBatcher::new(updates);
        let commands = batcher.optimize();
        assert!(commands.is_empty());
    }

    #[test]
    fn test_multiple_runs_different_lines() {
        let updates = vec![
            CellUpdate::Single {
                x: 0,
                y: 0,
                cell: Cell::new('A'),
            },
            CellUpdate::Single {
                x: 1,
                y: 0,
                cell: Cell::new('B'),
            },
            CellUpdate::Single {
                x: 0,
                y: 1,
                cell: Cell::new('C'),
            },
            CellUpdate::Single {
                x: 1,
                y: 1,
                cell: Cell::new('D'),
            },
        ];

        let batcher = UpdateBatcher::new(updates);
        let runs = batcher.group_into_runs();

        // Should create two runs (one per line)
        assert_eq!(runs.len(), 2);
        assert_eq!(runs[0].cells.len(), 2); // "AB"
        assert_eq!(runs[1].cells.len(), 2); // "CD"
    }
}