ratatui/terminal/viewport.rs
1use std::fmt;
2
3use crate::layout::Rect;
4
5/// Represents the viewport of the terminal. The viewport is the area of the terminal that is
6/// currently visible to the user. It can be either fullscreen, inline or fixed.
7///
8/// When the viewport is fullscreen, the whole terminal is used to draw the application.
9///
10/// When the viewport is inline, it is drawn inline with the rest of the terminal. The height of
11/// the viewport is fixed, but the width is the same as the terminal width.
12///
13/// When the viewport is fixed, it is drawn in a fixed area of the terminal. The area is specified
14/// by a [`Rect`].
15///
16/// See [`Terminal::with_options`] for more information.
17///
18/// [`Terminal::with_options`]: crate::Terminal::with_options
19#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
20pub enum Viewport {
21 /// The viewport is fullscreen
22 #[default]
23 Fullscreen,
24 /// The viewport is inline with the rest of the terminal.
25 ///
26 /// The viewport's height is fixed and specified in number of lines. The width is the same as
27 /// the terminal's width. The viewport is drawn below the cursor position.
28 Inline(u16),
29 /// The viewport is drawn in a fixed area of the terminal. The area is specified by a [`Rect`].
30 Fixed(Rect),
31}
32
33impl fmt::Display for Viewport {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 match self {
36 Self::Fullscreen => write!(f, "Fullscreen"),
37 Self::Inline(height) => write!(f, "Inline({height})"),
38 Self::Fixed(area) => write!(f, "Fixed({area})"),
39 }
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn viewport_to_string() {
49 assert_eq!(Viewport::Fullscreen.to_string(), "Fullscreen");
50 assert_eq!(Viewport::Inline(5).to_string(), "Inline(5)");
51 assert_eq!(
52 Viewport::Fixed(Rect::new(0, 0, 5, 5)).to_string(),
53 "Fixed(5x5+0+0)"
54 );
55 }
56}