Skip to main content

view/
render_context.rs

1const MINIMUM_WINDOW_HEIGHT: usize = 5; // title + pad top + line + pad bottom + help
2const MINIMUM_COMPACT_WINDOW_WIDTH: usize = 20; // ">s ccc mmmmmmmmmmmmm".len()
3const MINIMUM_FULL_WINDOW_WIDTH: usize = 34; // " > squash cccccccc mmmmmmmmmmmmm %".len()
4
5/// Represents data associated with rendering content.
6#[derive(Debug, Copy, Clone)]
7pub struct RenderContext {
8	height: usize,
9	width: usize,
10}
11
12impl RenderContext {
13	/// Create a new instance with a width and height.
14	#[must_use]
15	#[inline]
16	pub const fn new(width: usize, height: usize) -> Self {
17		Self { height, width }
18	}
19
20	/// Update the recorded width and height.
21	#[inline]
22	pub fn update(&mut self, width: u16, height: u16) {
23		self.width = width as usize;
24		self.height = height as usize;
25	}
26
27	/// Get the width of the terminal window.
28	#[must_use]
29	#[inline]
30	pub const fn width(&self) -> usize {
31		self.width
32	}
33
34	/// Get the height of the terminal window.
35	#[must_use]
36	#[inline]
37	pub const fn height(&self) -> usize {
38		self.height
39	}
40
41	/// Is the terminal window width at least the minimal supported width.
42	#[must_use]
43	#[inline]
44	pub const fn is_minimum_view_width(&self) -> bool {
45		self.width > MINIMUM_COMPACT_WINDOW_WIDTH
46	}
47
48	/// Is the terminal window height at least the minimal supported height.
49	#[must_use]
50	#[inline]
51	pub const fn is_minimum_view_height(&self) -> bool {
52		self.height > MINIMUM_WINDOW_HEIGHT
53	}
54
55	/// Is the terminal window large enough to render lines using their full width.
56	#[must_use]
57	#[inline]
58	pub const fn is_full_width(&self) -> bool {
59		self.width >= MINIMUM_FULL_WINDOW_WIDTH
60	}
61
62	/// Is the terminal window too small to render content.
63	#[must_use]
64	#[inline]
65	pub const fn is_window_too_small(&self) -> bool {
66		!self.is_minimum_view_width() || !self.is_minimum_view_height()
67	}
68}
69
70#[cfg(test)]
71mod tests {
72	use super::*;
73
74	#[test]
75	fn update() {
76		let mut context = RenderContext { width: 10, height: 20 };
77		context.update(100, 200);
78		assert_eq!(context.width(), 100);
79		assert_eq!(context.height(), 200);
80	}
81
82	#[test]
83	fn is_window_too_small_width_too_small() {
84		let context = RenderContext {
85			width: MINIMUM_COMPACT_WINDOW_WIDTH,
86			height: MINIMUM_WINDOW_HEIGHT + 1,
87		};
88		assert!(context.is_window_too_small());
89	}
90
91	#[test]
92	fn is_window_too_small_height_too_small() {
93		let context = RenderContext {
94			width: MINIMUM_COMPACT_WINDOW_WIDTH + 1,
95			height: MINIMUM_WINDOW_HEIGHT,
96		};
97		assert!(context.is_window_too_small());
98	}
99
100	#[test]
101	fn is_window_too_small_height_and_width_too_small() {
102		let context = RenderContext {
103			width: MINIMUM_COMPACT_WINDOW_WIDTH,
104			height: MINIMUM_WINDOW_HEIGHT,
105		};
106		assert!(context.is_window_too_small());
107	}
108
109	#[test]
110	fn is_window_too_small_width_and_height_large() {
111		let context = RenderContext {
112			width: MINIMUM_COMPACT_WINDOW_WIDTH + 1,
113			height: MINIMUM_WINDOW_HEIGHT + 1,
114		};
115		assert!(!context.is_window_too_small());
116	}
117}