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
//! Terminal backend core implementation using crossterm
use crossterm::{
cursor::{Hide, MoveTo, Show},
event::{DisableMouseCapture, EnableMouseCapture},
execute, queue,
style::{Attribute, ResetColor, SetAttribute},
terminal::{
self, disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen,
LeaveAlternateScreen,
},
};
use std::io::Write;
use super::super::{diff, Buffer};
use crate::layout::Rect;
use crate::Result;
use super::types::Terminal;
impl<W: Write> Terminal<W> {
/// Create a new terminal with the given writer
pub fn new(writer: W) -> Result<Self> {
let (width, height) = terminal::size()?;
Ok(Self {
writer,
current: Buffer::new(width, height),
raw_mode: false,
mouse_capture: false,
})
}
/// Initialize the terminal for TUI mode with mouse capture
pub fn init(&mut self) -> Result<()> {
self.init_with_mouse(true)
}
/// Initialize the terminal for TUI mode with optional mouse capture
///
/// When `mouse_capture` is false, text selection in terminal works normally.
/// Use this for keyboard-only applications.
pub fn init_with_mouse(&mut self, mouse_capture: bool) -> Result<()> {
enable_raw_mode()?;
self.raw_mode = true;
self.mouse_capture = mouse_capture;
if mouse_capture {
execute!(
self.writer,
EnterAlternateScreen,
EnableMouseCapture,
Hide,
Clear(ClearType::All)
)?;
} else {
execute!(
self.writer,
EnterAlternateScreen,
Hide,
Clear(ClearType::All)
)?;
}
Ok(())
}
/// Restore the terminal to normal mode
pub fn restore(&mut self) -> Result<()> {
if self.raw_mode {
if self.mouse_capture {
execute!(
self.writer,
DisableMouseCapture,
ResetColor,
Show,
LeaveAlternateScreen
)?;
} else {
execute!(self.writer, ResetColor, Show, LeaveAlternateScreen)?;
}
disable_raw_mode()?;
self.raw_mode = false;
}
Ok(())
}
/// Get terminal size
pub fn size(&self) -> (u16, u16) {
(self.current.width(), self.current.height())
}
/// Resize the terminal buffer
pub fn resize(&mut self, width: u16, height: u16) {
self.current.resize(width, height);
}
/// Render a buffer to the terminal using diff-based updates
///
/// This performs a full-screen diff. For optimized rendering with dirty regions,
/// use [`render_dirty`](Self::render_dirty) instead.
pub fn render(&mut self, buffer: &Buffer) -> Result<()> {
let changes = diff::diff(&self.current, buffer, &[]);
self.draw_changes(changes, buffer)
}
/// Render a buffer with dirty-rect tracking for optimized updates
///
/// Only cells within the specified dirty regions will be compared and updated.
/// This significantly reduces CPU usage for mostly-static UIs where only
/// specific regions change each frame.
///
/// # Arguments
///
/// * `buffer` - The new buffer state to render
/// * `dirty_rects` - Regions that may have changed since last render
///
/// # Example
///
/// ```ignore
/// use revue::layout::Rect;
///
/// // Only diff the area where a widget was updated
/// let dirty = [Rect::new(10, 5, 20, 3)];
/// terminal.render_dirty(&buffer, &dirty)?;
/// ```
pub fn render_dirty(&mut self, buffer: &Buffer, dirty_rects: &[Rect]) -> Result<()> {
let changes = diff::diff(&self.current, buffer, dirty_rects);
self.draw_changes(changes, buffer)
}
/// Force a full redraw
pub fn force_redraw(&mut self, buffer: &Buffer) -> Result<()> {
queue!(self.writer, Clear(ClearType::All))?;
let mut state = super::types::RenderState::default();
for (x, y, cell) in buffer.iter_cells() {
if !cell.is_continuation() {
let hyperlink_url = cell.hyperlink_id.and_then(|id| buffer.get_hyperlink(id));
let escape_sequence = cell.sequence_id.and_then(|id| buffer.get_sequence(id));
self.draw_cell_stateful(x, y, cell, hyperlink_url, escape_sequence, &mut state)?;
}
// Update current buffer (Cell is Copy, no allocation)
self.current.set(x, y, *cell);
}
// Close any open hyperlink at end of frame
if state.hyperlink_id.is_some() {
self.write_hyperlink_end()?;
}
// Reset state at end of frame
if state.fg.is_some() || state.bg.is_some() || !state.modifier.is_empty() {
queue!(self.writer, SetAttribute(Attribute::Reset))?;
}
self.writer.flush()?;
Ok(())
}
/// Clear the screen
pub fn clear(&mut self) -> Result<()> {
execute!(self.writer, Clear(ClearType::All))?;
self.current.clear();
Ok(())
}
/// Show the cursor
pub fn show_cursor(&mut self) -> Result<()> {
execute!(self.writer, Show)?;
Ok(())
}
/// Hide the cursor
pub fn hide_cursor(&mut self) -> Result<()> {
execute!(self.writer, Hide)?;
Ok(())
}
/// Move cursor to position
pub fn set_cursor(&mut self, x: u16, y: u16) -> Result<()> {
execute!(self.writer, MoveTo(x, y))?;
Ok(())
}
}
impl<W: Write> Drop for Terminal<W> {
fn drop(&mut self) {
let _ = self.restore();
}
}