ratatui-toolkit 0.2.6

DEPRECATED: this crate was renamed to `ratkit`. Please migrate to `ratkit`.
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
//! TermTui - Terminal emulator using mprocs architecture
//!
//! This module provides a terminal emulator with:
//! - termwiz for VT100 escape sequence parsing
//! - VecDeque-based infinite scrollback
//! - Copy mode with frozen screen snapshots
//! - Mouse and keyboard text selection
//!
//! Architecture (matching mprocs):
//! ```text
//! bytes → termwiz Parser → actions → Screen.handle_action() → Grid (VecDeque<Row>)
//! ```

mod attrs;
mod cell;
mod copy_mode;
mod grid;
mod keybindings;
mod parser;
mod row;
mod screen;
mod size;
mod widget;

pub use attrs::{Attrs, Color};
pub use cell::Cell;
pub use copy_mode::{CopyMode, CopyMoveDir, CopyPos};
pub use grid::{Grid, Pos};
pub use keybindings::TermTuiKeyBindings;
pub use parser::Parser;
pub use row::Row;
pub use screen::Screen;
pub use size::Size;
pub use widget::TermTuiWidget;

use anyhow::Result;
use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};
use ratatui::layout::Rect;
use ratatui::style::{Color as RatatuiColor, Style};
use ratatui::widgets::{Block, BorderType, Borders};
use ratatui::Frame;
use std::io::{Read, Write};
use std::sync::{Arc, Mutex};

/// TermTui - Terminal widget with mprocs-style architecture
///
/// Features:
/// - termwiz-based VT100 parsing
/// - VecDeque scrollback buffer
/// - Copy mode with frozen screen snapshots
/// - Mouse and keyboard selection
pub struct TermTui {
    /// Terminal parser and screen state
    parser: Arc<Mutex<Parser>>,

    /// Title for the terminal
    pub title: String,

    /// Whether the terminal has focus
    pub focused: bool,

    /// Copy mode state
    pub copy_mode: CopyMode,

    /// Process management
    _master: Option<Arc<Mutex<Box<dyn MasterPty + Send>>>>,
    _child: Option<Box<dyn Child + Send + Sync>>,
    writer: Option<Arc<Mutex<Box<dyn Write + Send>>>>,

    /// Styling
    pub border_style: Style,
    pub focused_border_style: Style,

    /// Customizable keybindings
    pub keybindings: TermTuiKeyBindings,
}

impl TermTui {
    /// Create a new terminal
    pub fn new(title: impl Into<String>) -> Self {
        let parser = Parser::new(24, 80, 10000);

        Self {
            parser: Arc::new(Mutex::new(parser)),
            title: title.into(),
            focused: false,
            copy_mode: CopyMode::None,
            _master: None,
            _child: None,
            writer: None,
            border_style: Style::default().fg(RatatuiColor::White),
            focused_border_style: Style::default().fg(RatatuiColor::Cyan),
            keybindings: TermTuiKeyBindings::default(),
        }
    }

    /// Set custom keybindings (builder pattern)
    pub fn with_keybindings(mut self, keybindings: TermTuiKeyBindings) -> Self {
        self.keybindings = keybindings;
        self
    }

    /// Spawn a terminal with a command
    pub fn spawn_with_command(
        title: impl Into<String>,
        command: &str,
        args: &[&str],
    ) -> Result<Self> {
        let rows = 24;
        let cols = 80;

        let pty_system = native_pty_system();
        let pty_size = PtySize {
            rows,
            cols,
            pixel_width: 0,
            pixel_height: 0,
        };

        let pair = pty_system.openpty(pty_size)?;

        let mut cmd = CommandBuilder::new(command);
        for arg in args {
            cmd.arg(arg);
        }

        // Set terminal type
        cmd.env("TERM", "xterm-256color");

        let current_dir = std::env::current_dir()?;
        cmd.cwd(current_dir);

        let child = pair.slave.spawn_command(cmd)?;

        // Set non-blocking mode
        #[cfg(unix)]
        {
            if let Some(fd) = pair.master.as_raw_fd() {
                unsafe {
                    let flags = libc::fcntl(fd, libc::F_GETFL, 0);
                    libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
                }
            }
        }

        let reader = pair.master.try_clone_reader()?;
        let writer = pair.master.take_writer()?;

        let writer = Arc::new(Mutex::new(writer));

        let parser = Parser::new(rows as usize, cols as usize, 10000);
        let parser = Arc::new(Mutex::new(parser));
        let parser_clone = Arc::clone(&parser);

        // Spawn read thread (using std::thread for sync compatibility)
        std::thread::spawn(move || {
            let mut buf = [0u8; 8192];
            let mut reader = reader;
            loop {
                match reader.read(&mut buf) {
                    Ok(0) => break,
                    Ok(n) => {
                        if let Ok(mut parser) = parser_clone.lock() {
                            parser.process(&buf[..n]);
                        }
                        std::thread::sleep(std::time::Duration::from_millis(10));
                    }
                    Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                        std::thread::sleep(std::time::Duration::from_millis(10));
                    }
                    Err(_) => break,
                }
            }
        });

        Ok(Self {
            parser,
            title: title.into(),
            focused: false,
            copy_mode: CopyMode::None,
            _master: None,
            _child: Some(child),
            writer: Some(writer),
            border_style: Style::default().fg(RatatuiColor::White),
            focused_border_style: Style::default().fg(RatatuiColor::Cyan),
            keybindings: TermTuiKeyBindings::default(),
        })
    }

    /// Handle keyboard input
    pub fn handle_key(&mut self, key: crossterm::event::KeyEvent) -> bool {
        use crossterm::event::KeyEventKind;

        // Only handle Press events
        if key.kind != KeyEventKind::Press {
            return false;
        }

        // Handle copy mode keys
        if self.copy_mode.is_active() {
            return self.handle_copy_mode_key(key);
        }

        // Copy selection (configurable, default: Ctrl+Shift+C)
        if TermTuiKeyBindings::key_matches(&key, &self.keybindings.copy_selection) {
            if let Some(text) = self.copy_mode.get_selected_text() {
                if let Ok(mut clipboard) = arboard::Clipboard::new() {
                    let _ = clipboard.set_text(text);
                }
                return true;
            }
        }

        // Enter copy mode (configurable, default: Ctrl+X)
        if TermTuiKeyBindings::key_matches(&key, &self.keybindings.enter_copy_mode) {
            self.enter_copy_mode();
            return true;
        }

        // Convert key to terminal input and send
        let text = self.key_to_terminal_input(key);
        if !text.is_empty() {
            self.send_input(&text);
            true
        } else {
            false
        }
    }

    /// Handle keyboard in copy mode
    fn handle_copy_mode_key(&mut self, key: crossterm::event::KeyEvent) -> bool {
        let kb = &self.keybindings;

        // Exit copy mode
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_exit)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_exit_alt)
        {
            self.copy_mode = CopyMode::None;
            return true;
        }

        // Navigation - Up
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_move_up)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_move_up_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::Up);
            return true;
        }

        // Navigation - Down
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_move_down)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_move_down_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::Down);
            return true;
        }

        // Navigation - Left
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_move_left)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_move_left_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::Left);
            return true;
        }

        // Navigation - Right
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_move_right)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_move_right_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::Right);
            return true;
        }

        // Line start
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_line_start)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_line_start_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::LineStart);
            return true;
        }

        // Line end
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_line_end)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_line_end_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::LineEnd);
            return true;
        }

        // Page up
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_page_up)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_page_up_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::PageUp);
            return true;
        }

        // Page down
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_page_down)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_page_down_alt)
        {
            self.copy_mode.move_dir(CopyMoveDir::PageDown);
            return true;
        }

        // Top
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_top) {
            self.copy_mode.move_dir(CopyMoveDir::Top);
            return true;
        }

        // Bottom
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_bottom) {
            self.copy_mode.move_dir(CopyMoveDir::Bottom);
            return true;
        }

        // Word left
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_word_left) {
            self.copy_mode.move_dir(CopyMoveDir::WordLeft);
            return true;
        }

        // Word right
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_word_right) {
            self.copy_mode.move_dir(CopyMoveDir::WordRight);
            return true;
        }

        // Start/toggle selection
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_start_selection)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_start_selection_alt)
        {
            self.copy_mode.set_anchor();
            return true;
        }

        // Copy and exit
        if TermTuiKeyBindings::key_matches(&key, &kb.copy_and_exit)
            || TermTuiKeyBindings::key_matches(&key, &kb.copy_and_exit_alt)
        {
            if let Some(text) = self.copy_mode.get_selected_text() {
                if let Ok(mut clipboard) = arboard::Clipboard::new() {
                    let _ = clipboard.set_text(text);
                }
            }
            self.copy_mode = CopyMode::None;
            return true;
        }

        false
    }

    /// Convert key event to terminal input sequence
    fn key_to_terminal_input(&self, key: crossterm::event::KeyEvent) -> String {
        use crossterm::event::{KeyCode, KeyModifiers};

        match key.code {
            KeyCode::Char(c) => {
                if key.modifiers.contains(KeyModifiers::CONTROL) {
                    match c.to_ascii_lowercase() {
                        'a'..='z' => {
                            let code = (c.to_ascii_lowercase() as u8 - b'a' + 1) as char;
                            code.to_string()
                        }
                        '@' => "\x00".to_string(),
                        '[' => "\x1b".to_string(),
                        '\\' => "\x1c".to_string(),
                        ']' => "\x1d".to_string(),
                        '^' => "\x1e".to_string(),
                        '_' => "\x1f".to_string(),
                        _ => c.to_string(),
                    }
                } else if key.modifiers.contains(KeyModifiers::ALT) {
                    format!("\x1b{}", c)
                } else {
                    c.to_string()
                }
            }
            KeyCode::Enter => "\r".to_string(),
            KeyCode::Backspace => "\x7f".to_string(),
            KeyCode::Tab => "\t".to_string(),
            KeyCode::Esc => "\x1b".to_string(),
            KeyCode::Up => "\x1b[A".to_string(),
            KeyCode::Down => "\x1b[B".to_string(),
            KeyCode::Right => "\x1b[C".to_string(),
            KeyCode::Left => "\x1b[D".to_string(),
            KeyCode::Home => "\x1b[H".to_string(),
            KeyCode::End => "\x1b[F".to_string(),
            KeyCode::PageUp => "\x1b[5~".to_string(),
            KeyCode::PageDown => "\x1b[6~".to_string(),
            KeyCode::Delete => "\x1b[3~".to_string(),
            _ => String::new(),
        }
    }

    /// Enter copy mode with frozen screen
    pub fn enter_copy_mode(&mut self) {
        let parser = self.parser.lock().unwrap();
        let screen = parser.screen().clone();
        let size = screen.size();

        // Start at bottom-right of visible area
        let start = CopyPos::new(size.cols as i32 - 1, size.rows as i32 - 1);
        self.copy_mode = CopyMode::enter(screen, start);
    }

    /// Handle mouse events
    ///
    /// This method handles all mouse interactions:
    /// - Mouse drag: automatically enters copy mode and starts selection (like mprocs)
    /// - Mouse wheel: scrolls the terminal content
    /// - In copy mode: mouse click moves cursor, mouse drag selects
    ///
    /// # Arguments
    /// * `event` - The mouse event from crossterm
    /// * `area` - The area where the terminal is rendered (for coordinate translation)
    ///
    /// # Returns
    /// `true` if the event was handled, `false` otherwise
    pub fn handle_mouse(&mut self, event: crossterm::event::MouseEvent, area: Rect) -> bool {
        use crossterm::event::{MouseButton, MouseEventKind};

        // Translate coordinates relative to the content area (inside borders)
        let content_x = event.column.saturating_sub(area.x + 1) as i32;
        let content_y = event.row.saturating_sub(area.y + 1) as i32;

        match event.kind {
            MouseEventKind::Down(MouseButton::Left) => {
                if self.copy_mode.is_active() {
                    // In copy mode, click moves cursor
                    if let CopyMode::Active { cursor, .. } = &mut self.copy_mode {
                        cursor.x = content_x;
                        cursor.y = content_y;
                    }
                } else {
                    // Not in copy mode - enter copy mode and position cursor
                    let parser = self.parser.lock().unwrap();
                    let screen = parser.screen().clone();
                    drop(parser);

                    let start = CopyPos::new(content_x, content_y);
                    self.copy_mode = CopyMode::enter(screen, start);
                }
                true
            }
            MouseEventKind::Drag(MouseButton::Left) => {
                if !self.copy_mode.is_active() {
                    // Auto-enter copy mode on drag (like mprocs)
                    let parser = self.parser.lock().unwrap();
                    let screen = parser.screen().clone();
                    drop(parser);

                    let start = CopyPos::new(content_x, content_y);
                    self.copy_mode = CopyMode::enter(screen, start);
                    // Set anchor immediately for selection
                    self.copy_mode.set_anchor();
                } else {
                    // Already in copy mode - set anchor if not set, then update cursor
                    self.copy_mode.set_end();

                    // Move cursor to new position
                    if let CopyMode::Active { cursor, .. } = &mut self.copy_mode {
                        cursor.x = content_x;
                        cursor.y = content_y;
                    }
                }
                true
            }
            MouseEventKind::Up(MouseButton::Left) => {
                // Keep selection active on mouse up
                true
            }
            MouseEventKind::ScrollUp => {
                self.scroll_up(3);
                true
            }
            MouseEventKind::ScrollDown => {
                self.scroll_down(3);
                true
            }
            _ => false,
        }
    }

    /// Handle mouse down (start selection)
    ///
    /// Note: Consider using `handle_mouse` instead for comprehensive mouse handling.
    #[deprecated(
        since = "0.2.0",
        note = "Use handle_mouse instead for comprehensive mouse handling"
    )]
    pub fn handle_mouse_down(&mut self, x: u16, y: u16) {
        let content_x = x.saturating_sub(1) as i32;
        let content_y = y.saturating_sub(1) as i32;

        let parser = self.parser.lock().unwrap();
        let screen = parser.screen().clone();

        let start = CopyPos::new(content_x, content_y);
        self.copy_mode = CopyMode::enter(screen, start);
    }

    /// Handle mouse drag (update selection)
    ///
    /// Note: Consider using `handle_mouse` instead for comprehensive mouse handling.
    #[deprecated(
        since = "0.2.0",
        note = "Use handle_mouse instead for comprehensive mouse handling"
    )]
    pub fn handle_mouse_drag(&mut self, x: u16, y: u16) {
        if self.copy_mode.is_active() {
            let content_x = x.saturating_sub(1) as i32;
            let content_y = y.saturating_sub(1) as i32;

            // First set anchor if not set
            self.copy_mode.set_end();

            // Move cursor to new position
            if let CopyMode::Active { cursor, .. } = &mut self.copy_mode {
                cursor.x = content_x;
                cursor.y = content_y;
            }
        }
    }

    /// Handle mouse up
    ///
    /// Note: Consider using `handle_mouse` instead for comprehensive mouse handling.
    #[deprecated(
        since = "0.2.0",
        note = "Use handle_mouse instead for comprehensive mouse handling"
    )]
    pub fn handle_mouse_up(&mut self) {
        // Keep selection active
    }

    /// Scroll up
    pub fn scroll_up(&mut self, lines: usize) {
        let mut parser = self.parser.lock().unwrap();
        parser.screen_mut().scroll_screen_up(lines);
    }

    /// Scroll down
    pub fn scroll_down(&mut self, lines: usize) {
        let mut parser = self.parser.lock().unwrap();
        parser.screen_mut().scroll_screen_down(lines);
    }

    /// Clear selection
    pub fn clear_selection(&mut self) {
        self.copy_mode = CopyMode::None;
    }

    /// Check if has selection
    pub fn has_selection(&self) -> bool {
        self.copy_mode.is_active()
    }

    /// Get selected text
    pub fn get_selected_text(&self) -> Option<String> {
        self.copy_mode.get_selected_text()
    }

    /// Send input to the terminal
    pub fn send_input(&self, text: &str) {
        if let Some(ref writer) = self.writer {
            let mut writer = writer.lock().unwrap();
            let _ = writer.write_all(text.as_bytes());
            let _ = writer.flush();
        }
    }

    /// Resize the terminal
    pub fn resize(&mut self, rows: u16, cols: u16) {
        let mut parser = self.parser.lock().unwrap();
        parser.resize(rows as usize, cols as usize);
    }

    /// Render terminal content (without borders)
    pub fn render_content(&mut self, frame: &mut Frame, area: Rect) {
        let parser = self.parser.lock().unwrap();
        let screen = if let Some(frozen) = self.copy_mode.frozen_screen() {
            frozen
        } else {
            parser.screen()
        };

        let widget = TermTuiWidget::new(screen)
            .scroll_offset(screen.scrollback())
            .copy_mode(&self.copy_mode);

        frame.render_widget(widget, area);
    }

    /// Render terminal with borders
    pub fn render(&mut self, frame: &mut Frame, area: Rect) {
        use ratatui::layout::{Constraint, Direction, Layout};
        use ratatui::text::{Line, Span};

        let border_style = if self.focused {
            self.focused_border_style
        } else {
            self.border_style
        };

        // Split area for content and hotkey footer
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Min(3), Constraint::Length(1)])
            .split(area);

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(border_style)
            .title(self.title.as_str());

        let inner = block.inner(chunks[0]);
        frame.render_widget(block, chunks[0]);
        self.render_content(frame, inner);

        // Render hotkey footer based on mode (showing configured keybindings)
        let kb = &self.keybindings;
        let hotkeys = if self.copy_mode.is_active() {
            // Build move keys display string
            let move_keys = format!(
                "{}/{}",
                TermTuiKeyBindings::key_to_display_string(&kb.copy_move_up),
                TermTuiKeyBindings::key_to_display_string(&kb.copy_move_down)
            );
            let select_key = TermTuiKeyBindings::key_to_display_string(&kb.copy_start_selection);
            let copy_key = TermTuiKeyBindings::key_to_display_string(&kb.copy_and_exit);
            let word_keys = format!(
                "{}/{}",
                TermTuiKeyBindings::key_to_display_string(&kb.copy_word_right),
                TermTuiKeyBindings::key_to_display_string(&kb.copy_word_left)
            );
            let line_keys = format!(
                "{}/{}",
                TermTuiKeyBindings::key_to_display_string(&kb.copy_line_start),
                TermTuiKeyBindings::key_to_display_string(&kb.copy_line_end)
            );
            let top_bot_keys = format!(
                "{}/{}",
                TermTuiKeyBindings::key_to_display_string(&kb.copy_top),
                TermTuiKeyBindings::key_to_display_string(&kb.copy_bottom)
            );
            let exit_key = TermTuiKeyBindings::key_to_display_string(&kb.copy_exit);

            Line::from(vec![
                Span::styled(
                    " COPY ",
                    Style::default()
                        .fg(RatatuiColor::Black)
                        .bg(RatatuiColor::Yellow),
                ),
                Span::raw(" "),
                Span::styled(move_keys, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" move "),
                Span::styled(select_key, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" select "),
                Span::styled(copy_key, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" copy "),
                Span::styled(word_keys, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" word "),
                Span::styled(line_keys, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" line "),
                Span::styled(top_bot_keys, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" top/bot "),
                Span::styled(exit_key, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" exit"),
            ])
        } else {
            let enter_copy_key = TermTuiKeyBindings::key_to_display_string(&kb.enter_copy_mode);
            let copy_selection_key = TermTuiKeyBindings::key_to_display_string(&kb.copy_selection);

            Line::from(vec![
                Span::styled(enter_copy_key, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" copy mode "),
                Span::styled(copy_selection_key, Style::default().fg(RatatuiColor::Cyan)),
                Span::raw(" copy "),
                Span::styled("scroll", Style::default().fg(RatatuiColor::DarkGray)),
                Span::raw(" mouse wheel"),
            ])
        };

        frame.render_widget(hotkeys, chunks[1]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    #[test]
    fn test_termtui_creation() {
        let term = TermTui::new("Test Terminal");
        assert_eq!(term.title, "Test Terminal");
        assert!(!term.focused);
        assert!(!term.copy_mode.is_active());
    }

    #[test]
    fn test_termtui_focus() {
        let mut term = TermTui::new("Test");

        term.focused = true;
        assert!(term.focused);

        term.focused = false;
        assert!(!term.focused);
    }

    #[test]
    fn test_termtui_copy_mode_enter() {
        let mut term = TermTui::new("Test");

        assert!(!term.copy_mode.is_active());

        let key = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
        term.handle_key(key);

        assert!(term.copy_mode.is_active());
    }

    #[test]
    fn test_termtui_copy_mode_exit() {
        let mut term = TermTui::new("Test");

        // Enter copy mode
        let enter_key = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
        term.handle_key(enter_key);
        assert!(term.copy_mode.is_active());

        // Exit with Esc
        let esc_key = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
        term.handle_key(esc_key);
        assert!(!term.copy_mode.is_active());
    }

    #[test]
    fn test_termtui_key_conversion() {
        let term = TermTui::new("Test");

        // Regular character
        let key = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE);
        assert_eq!(term.key_to_terminal_input(key), "a");

        // Enter
        let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
        assert_eq!(term.key_to_terminal_input(key), "\r");

        // Ctrl+C
        let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        assert_eq!(term.key_to_terminal_input(key), "\x03");

        // Arrow up
        let key = KeyEvent::new(KeyCode::Up, KeyModifiers::NONE);
        assert_eq!(term.key_to_terminal_input(key), "\x1b[A");
    }

    #[test]
    fn test_termtui_selection() {
        use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};

        let mut term = TermTui::new("Test");
        let area = ratatui::layout::Rect::new(0, 0, 80, 24);

        // Start selection via mouse down
        let mouse_event = MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 5,
            row: 5,
            modifiers: KeyModifiers::NONE,
        };
        term.handle_mouse(mouse_event, area);
        assert!(term.has_selection());

        // Clear selection
        term.clear_selection();
        assert!(!term.has_selection());
    }

    #[test]
    fn test_termtui_keybindings() {
        // Test default keybindings
        let kb = TermTuiKeyBindings::default();
        assert_eq!(kb.enter_copy_mode.code, KeyCode::Char('x'));
        assert!(kb.enter_copy_mode.modifiers.contains(KeyModifiers::CONTROL));

        // Test key_to_display_string
        let display = TermTuiKeyBindings::key_to_display_string(&kb.enter_copy_mode);
        assert_eq!(display, "^X");

        // Test key_matches
        let key = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
        assert!(TermTuiKeyBindings::key_matches(&key, &kb.enter_copy_mode));

        let wrong_key = KeyEvent::new(KeyCode::Char('y'), KeyModifiers::CONTROL);
        assert!(!TermTuiKeyBindings::key_matches(
            &wrong_key,
            &kb.enter_copy_mode
        ));
    }

    #[test]
    fn test_termtui_with_keybindings() {
        let custom_kb = TermTuiKeyBindings {
            enter_copy_mode: KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
            ..Default::default()
        };

        let term = TermTui::new("Test").with_keybindings(custom_kb);
        assert_eq!(term.keybindings.enter_copy_mode.code, KeyCode::Char('c'));
    }

    #[test]
    fn test_mouse_scroll() {
        use crossterm::event::{MouseEvent, MouseEventKind};

        let mut term = TermTui::new("Test");
        let area = ratatui::layout::Rect::new(0, 0, 80, 24);

        // Test scroll up
        let scroll_up = MouseEvent {
            kind: MouseEventKind::ScrollUp,
            column: 10,
            row: 10,
            modifiers: KeyModifiers::NONE,
        };
        assert!(term.handle_mouse(scroll_up, area));

        // Test scroll down
        let scroll_down = MouseEvent {
            kind: MouseEventKind::ScrollDown,
            column: 10,
            row: 10,
            modifiers: KeyModifiers::NONE,
        };
        assert!(term.handle_mouse(scroll_down, area));
    }
}