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
use crate::backend::{Backend, ClearType};
use crate::layout::Rect;
use crate::terminal::inline::compute_inline_size;
use crate::terminal::{Terminal, Viewport};
impl<B: Backend> Terminal<B> {
/// Updates the Terminal so that internal buffers match the requested area.
///
/// This updates the buffer size used for rendering and triggers a full clear so the next
/// [`Terminal::draw`] / [`Terminal::try_draw`] paints into a consistent area.
///
/// When the viewport is [`Viewport::Inline`], the `area` argument is treated as the new
/// terminal size and the viewport origin is recomputed relative to the current cursor position.
/// Ratatui attempts to keep the cursor at the same relative row within the viewport across
/// resizes.
///
/// See also: [`Terminal::autoresize`] (automatic resizing during [`Terminal::draw`] /
/// [`Terminal::try_draw`]).
///
/// For [`Viewport::Fixed`] and [`Viewport::Fullscreen`], `area` becomes the new viewport area.
/// For [`Viewport::Inline`], `area` is interpreted as the backend's new terminal size and the
/// viewport origin may move to preserve the cursor's relative row within the inline UI.
pub fn resize(&mut self, area: Rect) -> Result<(), B::Error> {
let (mut next_area, cursor_to_restore) = match self.viewport {
Viewport::Inline(height) => {
let offset_in_previous_viewport = self
.last_known_cursor_pos
.y
.saturating_sub(self.viewport_area.top());
let (next_area, cursor_position) = compute_inline_size(
&mut self.backend,
height,
area.as_size(),
offset_in_previous_viewport,
)?;
(next_area, Some(cursor_position))
}
Viewport::Fixed(_) | Viewport::Fullscreen => (area, None),
};
// clear screen on horizontal shrink to avoid line wrapping issues
if next_area.width < self.viewport_area.width {
next_area.y = 0;
self.backend.clear_region(ClearType::All)?;
}
self.set_viewport_area(next_area);
self.clear_viewport()?;
if let Some(cursor_position) = cursor_to_restore {
self.backend.set_cursor_position(cursor_position)?;
}
self.last_known_area = area;
Ok(())
}
/// Queries the backend for size and resizes if it doesn't match the previous size.
///
/// This is called automatically during [`Terminal::draw`] / [`Terminal::try_draw`] for
/// fullscreen and inline viewports. Fixed viewports are not automatically resized.
///
/// If the size changed, this calls [`Terminal::resize`] and therefore clears the affected
/// region before the next frame is rendered.
pub fn autoresize(&mut self) -> Result<(), B::Error> {
// fixed viewports do not get autoresized
if matches!(self.viewport, Viewport::Fullscreen | Viewport::Inline(_)) {
let area = self.size()?.into();
if area != self.last_known_area {
self.resize(area)?;
}
}
Ok(())
}
/// Resize internal buffers and update the current viewport area.
///
/// This is an internal helper used by [`Terminal::with_options`] and [`Terminal::resize`].
pub(crate) fn set_viewport_area(&mut self, area: Rect) {
self.buffers[self.current].resize(area);
self.buffers[1 - self.current].resize(area);
self.viewport_area = area;
}
}
#[cfg(test)]
mod tests {
use crate::backend::{Backend, TestBackend};
use crate::buffer::Buffer;
use crate::layout::{Position, Rect};
use crate::terminal::{Terminal, TerminalOptions, Viewport};
#[test]
fn resize_fullscreen_updates_viewport_and_buffer_areas() {
let backend = TestBackend::new(3, 2);
let mut terminal = Terminal::new(backend).unwrap();
terminal.backend_mut().resize(4, 3);
let new_area = Rect::new(0, 0, 4, 3);
terminal.resize(new_area).unwrap();
assert_eq!(terminal.viewport_area, new_area);
assert_eq!(terminal.last_known_area, new_area);
assert_eq!(terminal.buffers[terminal.current].area, new_area);
assert_eq!(terminal.buffers[1 - terminal.current].area, new_area);
}
#[test]
fn resize_fullscreen_triggers_clear_and_resets_back_buffer() {
// This test is specifically about the side effects of `resize`:
// - it calls `clear` to force a full redraw
// - it resets the "previous" buffer
let backend = TestBackend::new(3, 2);
let mut terminal = Terminal::new(backend).unwrap();
// Put visible content on the backend so we can tell whether a clear happened.
{
let frame = terminal.get_frame();
frame.buffer[(0, 0)].set_symbol("x");
}
terminal.flush().unwrap();
terminal.backend().assert_buffer_lines(["x ", " "]);
terminal.backend_mut().resize(4, 3);
let new_area = Rect::new(0, 0, 4, 3);
terminal.resize(new_area).unwrap();
terminal
.backend()
.assert_buffer_lines([" ", " ", " "]);
assert_eq!(
terminal.buffers[1 - terminal.current],
Buffer::empty(new_area)
);
}
#[test]
fn autoresize_fullscreen_uses_backend_size_when_changed() {
let backend = TestBackend::new(3, 2);
let mut terminal = Terminal::new(backend).unwrap();
{
let frame = terminal.get_frame();
frame.buffer[(0, 0)].set_symbol("x");
}
terminal.flush().unwrap();
terminal.backend_mut().resize(4, 3);
terminal.autoresize().unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 0, 4, 3));
assert_eq!(terminal.last_known_area, Rect::new(0, 0, 4, 3));
terminal
.backend()
.assert_buffer_lines([" ", " ", " "]);
}
#[test]
fn autoresize_fixed_does_not_change_viewport() {
let backend = TestBackend::with_lines(["xxx", "yyy"]);
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Fixed(Rect::new(1, 0, 2, 2)),
},
)
.unwrap();
terminal.autoresize().unwrap();
assert_eq!(terminal.viewport_area, Rect::new(1, 0, 2, 2));
assert_eq!(terminal.last_known_area, Rect::new(1, 0, 2, 2));
terminal.backend().assert_buffer_lines(["xxx", "yyy"]);
}
#[test]
fn resize_fixed_changes_viewport_area_and_buffer_sizes() {
let backend = TestBackend::new(5, 3);
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Fixed(Rect::new(1, 1, 2, 1)),
},
)
.unwrap();
terminal.resize(Rect::new(0, 0, 3, 2)).unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 0, 3, 2));
assert_eq!(terminal.last_known_area, Rect::new(0, 0, 3, 2));
assert_eq!(
terminal.buffers[terminal.current].area,
terminal.viewport_area
);
assert_eq!(
terminal.buffers[1 - terminal.current].area,
terminal.viewport_area
);
}
#[test]
fn resize_inline_recomputes_origin_using_previous_cursor_offset() {
let mut backend = TestBackend::new(10, 10);
backend
.set_cursor_position(Position { x: 0, y: 4 })
.unwrap();
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(4),
},
)
.unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 4, 10, 4));
// Characterization test:
// This test simulates a terminal resize (increasing the terminal height) while an inline
// viewport is active. The key behavior being exercised is that the viewport remains
// anchored to the backend cursor row and preserves the cursor's relative offset within the
// previous viewport.
//
// For inline viewports, `Terminal::resize(area)` interprets `area` as the *new terminal
// size*, then recomputes the viewport origin based on:
// - the backend cursor position at the time of the call
// - the cursor offset within the *previous* viewport (`last_known_cursor_pos -
// viewport_top`)
//
// This means `resize(Rect { .. })` can update `viewport_area.y` even when the passed-in
// `area.y` is 0, because `viewport_area` is anchored to the cursor row, not the terminal
// origin.
terminal.last_known_cursor_pos = Position { x: 0, y: 5 };
terminal
.backend_mut()
.set_cursor_position(Position { x: 0, y: 6 })
.unwrap();
terminal.backend_mut().resize(10, 12);
let new_terminal_area = Rect::new(0, 0, 10, 12);
terminal.resize(new_terminal_area).unwrap();
// Previous viewport top was y=4, and last_known_cursor_pos was y=5, so the cursor offset
// within the viewport is 1 row. At the time of resize the backend cursor is at y=6, so the
// new viewport top becomes 6 - 1 = 5.
assert_eq!(terminal.viewport_area, Rect::new(0, 5, 10, 4));
assert_eq!(terminal.last_known_area, new_terminal_area);
}
#[test]
fn resize_inline_clamps_height_to_terminal_height() {
// Characterization test:
// This test simulates a terminal resize that *reduces* the terminal height. Inline
// viewports clamp their height to the new terminal size so the viewport remains fully
// visible.
let mut backend = TestBackend::new(10, 10);
backend
.set_cursor_position(Position { x: 0, y: 0 })
.unwrap();
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(10),
},
)
.unwrap();
terminal.backend_mut().resize(10, 3);
terminal.resize(Rect::new(0, 0, 10, 3)).unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 0, 10, 3));
}
#[test]
fn resize_inline_preserves_backend_cursor_across_repeated_resizes() {
let mut backend = TestBackend::new(10, 10);
backend
.set_cursor_position(Position { x: 0, y: 4 })
.unwrap();
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(4),
},
)
.unwrap();
terminal.last_known_cursor_pos = Position { x: 0, y: 5 };
terminal
.backend_mut()
.set_cursor_position(Position { x: 0, y: 6 })
.unwrap();
terminal.resize(Rect::new(0, 0, 10, 12)).unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 5, 10, 4));
assert_eq!(
terminal.backend().cursor_position(),
Position { x: 0, y: 6 }
);
terminal.resize(Rect::new(0, 0, 10, 14)).unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 6, 10, 4));
assert_eq!(
terminal.backend().cursor_position(),
Position { x: 0, y: 6 }
);
}
// This tests for the case where the new width is smaller than the old
// width. The screen should be cleared completely to avoid rendering
// glitches caused by line wrap.
#[test]
fn resize_inline_clears_screen_on_horizontal_shrink() {
let mut backend = TestBackend::with_lines(["0000", "1111"]);
backend
.set_cursor_position(Position { x: 0, y: 0 })
.unwrap();
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(2),
},
)
.unwrap();
let old_area = terminal.backend().buffer().area;
let new_area = Rect {
width: old_area.width - 1,
..old_area
};
terminal.resize(new_area);
assert_eq!(terminal.viewport_area, new_area);
let all_clear = terminal
.current_buffer_mut()
.content()
.iter()
.all(|cell| cell == &crate::buffer::Cell::EMPTY);
assert!(all_clear, "not all buffer cells are empty");
}
}