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
use crate::backend::Backend;
use crate::buffer::Buffer;
use crate::layout::Position;
use crate::terminal::inline::compute_inline_size;
use crate::terminal::{Terminal, TerminalOptions, Viewport};
impl<B: Backend> Terminal<B> {
/// Creates a new [`Terminal`] with the given [`Backend`] with a full screen viewport.
///
/// This is a convenience for [`Terminal::with_options`] with [`Viewport::Fullscreen`].
/// Ratatui initializes two empty buffers sized to the backend's current screen area and treats
/// future backend size changes as redraw-triggering resizes during render passes.
///
/// After creating a terminal, call [`Terminal::draw`] (or [`Terminal::try_draw`]) in a loop to
/// render your UI.
///
/// Note that unlike [`ratatui::init`], this does not install a panic hook, so it is
/// recommended to do that manually when using this function, otherwise any panic messages will
/// be printed to the alternate screen and the terminal may be left in an unusable state.
///
/// See [how to set up panic hooks](https://ratatui.rs/recipes/apps/panic-hooks/) and
/// [`better-panic` example](https://ratatui.rs/recipes/apps/better-panic/) for more
/// information.
///
/// # Example
///
/// ```rust,no_run
/// # #![allow(unexpected_cfgs)]
/// # #[cfg(feature = "crossterm")]
/// # {
/// use std::io::stdout;
///
/// use ratatui::Terminal;
/// use ratatui::backend::CrosstermBackend;
///
/// let backend = CrosstermBackend::new(stdout());
/// let _terminal = Terminal::new(backend)?;
///
/// // Optionally set up a panic hook to restore the terminal on panic.
/// let old_hook = std::panic::take_hook();
/// std::panic::set_hook(Box::new(move |info| {
/// ratatui::restore();
/// old_hook(info);
/// }));
/// # }
/// # #[cfg(not(feature = "crossterm"))]
/// # {
/// # use ratatui_core::{backend::TestBackend, terminal::Terminal};
/// # let backend = TestBackend::new(10, 10);
/// # let _terminal = Terminal::new(backend)?;
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// [`ratatui::init`]: https://docs.rs/ratatui/latest/ratatui/fn.init.html
pub fn new(backend: B) -> Result<Self, B::Error> {
Self::with_options(
backend,
TerminalOptions {
viewport: Viewport::Fullscreen,
},
)
}
/// Creates a new [`Terminal`] with the given [`Backend`] and [`TerminalOptions`].
///
/// The viewport determines what area is exposed to widgets via [`Frame::area`] and how Ratatui
/// keeps its internal buffers synchronized with the backend. See [`Viewport`] for an overview
/// of the available modes.
///
/// For viewport behavior after initialization, see [`Terminal::resize`] and
/// [`Terminal::autoresize`].
///
/// [`Frame::area`]: crate::terminal::Frame::area
/// [`Terminal::autoresize`]: crate::terminal::Terminal::autoresize
/// [`Terminal::resize`]: crate::terminal::Terminal::resize
///
/// After creating a terminal, call [`Terminal::draw`] (or [`Terminal::try_draw`]) in a loop to
/// render your UI.
///
/// Resize behavior depends on the selected viewport:
///
/// - [`Viewport::Fullscreen`] and [`Viewport::Inline`] are automatically resized during
/// [`Terminal::draw`] / [`Terminal::try_draw`] (via [`Terminal::autoresize`]).
/// - [`Viewport::Fixed`] is not automatically resized; call [`Terminal::resize`] if the region
/// should change.
///
/// # Example
///
/// ```rust,no_run
/// # #![allow(unexpected_cfgs)]
/// # #[cfg(feature = "crossterm")]
/// # {
/// use std::io::stdout;
///
/// use ratatui::backend::CrosstermBackend;
/// use ratatui::layout::Rect;
/// use ratatui::{Terminal, TerminalOptions, Viewport};
///
/// let backend = CrosstermBackend::new(stdout());
/// let viewport = Viewport::Fixed(Rect::new(0, 0, 10, 10));
/// let _terminal = Terminal::with_options(backend, TerminalOptions { viewport })?;
/// # }
/// # #[cfg(not(feature = "crossterm"))]
/// # {
/// # use ratatui_core::{
/// # backend::TestBackend,
/// # layout::Rect,
/// # terminal::{Terminal, TerminalOptions, Viewport},
/// # };
/// # let backend = TestBackend::new(10, 10);
/// # let viewport = Viewport::Fixed(Rect::new(0, 0, 10, 10));
/// # let _terminal = Terminal::with_options(backend, TerminalOptions { viewport })?;
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// When the viewport is [`Viewport::Inline`], Ratatui anchors the viewport to the current
/// cursor row at initialization time (always starting at column 0). Ratatui may append lines
/// and thereby scroll the terminal to make enough room for the requested height so the
/// viewport stays fully visible.
pub fn with_options(mut backend: B, options: TerminalOptions) -> Result<Self, B::Error> {
let area = match options.viewport {
Viewport::Fullscreen | Viewport::Inline(_) => backend.size()?.into(),
Viewport::Fixed(area) => area,
};
let (viewport_area, cursor_pos) = match options.viewport {
Viewport::Fullscreen => (area, Position::ORIGIN),
Viewport::Inline(height) => {
compute_inline_size(&mut backend, height, area.as_size(), 0)?
}
Viewport::Fixed(area) => (area, area.as_position()),
};
Ok(Self {
backend,
buffers: [Buffer::empty(viewport_area), Buffer::empty(viewport_area)],
current: 0,
hidden_cursor: false,
viewport: options.viewport,
viewport_area,
last_known_area: area,
last_known_cursor_pos: cursor_pos,
frame_count: 0,
})
}
}
#[cfg(test)]
mod tests {
use crate::backend::{Backend, TestBackend};
use crate::layout::{Position, Rect};
use crate::terminal::{Terminal, TerminalOptions, Viewport};
#[test]
fn new_fullscreen_initializes_state() {
let backend = TestBackend::new(10, 5);
let terminal = Terminal::new(backend).unwrap();
assert_eq!(terminal.viewport, Viewport::Fullscreen);
assert_eq!(terminal.viewport_area, Rect::new(0, 0, 10, 5));
assert_eq!(terminal.last_known_area, Rect::new(0, 0, 10, 5));
assert_eq!(terminal.last_known_cursor_pos, Position::ORIGIN);
assert_eq!(terminal.current, 0);
assert!(!terminal.hidden_cursor);
assert_eq!(terminal.frame_count, 0);
assert_eq!(terminal.buffers[0].area, terminal.viewport_area);
assert_eq!(terminal.buffers[1].area, terminal.viewport_area);
}
#[test]
fn with_options_fixed_uses_fixed_area() {
let backend = TestBackend::new(10, 10);
let viewport = Viewport::Fixed(Rect::new(2, 3, 5, 4));
let terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: viewport.clone(),
},
)
.unwrap();
assert_eq!(terminal.viewport, viewport);
assert_eq!(terminal.viewport_area, Rect::new(2, 3, 5, 4));
assert_eq!(terminal.last_known_area, Rect::new(2, 3, 5, 4));
assert_eq!(terminal.last_known_cursor_pos, Position { x: 2, y: 3 });
assert_eq!(terminal.buffers[0].area, terminal.viewport_area);
assert_eq!(terminal.buffers[1].area, terminal.viewport_area);
}
#[test]
fn with_options_inline_anchors_to_cursor_when_space_available() {
let mut backend = TestBackend::new(10, 10);
backend
.set_cursor_position(Position { x: 0, y: 3 })
.unwrap();
let terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(4),
},
)
.unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 3, 10, 4));
assert_eq!(terminal.last_known_cursor_pos, Position { x: 0, y: 3 });
}
#[test]
fn with_options_inline_shifts_up_when_near_bottom() {
let mut backend = TestBackend::new(10, 10);
backend
.set_cursor_position(Position { x: 0, y: 8 })
.unwrap();
let terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(4),
},
)
.unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 6, 10, 4));
assert_eq!(terminal.last_known_cursor_pos, Position { x: 0, y: 8 });
}
#[test]
fn with_options_inline_clamps_height_to_terminal() {
let mut backend = TestBackend::new(10, 3);
backend
.set_cursor_position(Position { x: 0, y: 0 })
.unwrap();
let terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(10),
},
)
.unwrap();
assert_eq!(terminal.viewport_area, Rect::new(0, 0, 10, 3));
}
}