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
// Copyright (c) 2023-2025 R3BL LLC. Licensed under Apache License, Version 2.0.
use crossterm::{cursor::{MoveToNextLine, MoveToPreviousLine},
terminal::{Clear, ClearType}};
use crate::{queue_commands,
throws,
ChUnit,
OutputDevice,
ResizeHint,
Size,
DEVELOPMENT_MODE};
pub trait CalculateResizeHint {
fn set_size(&mut self, new_size: Size);
fn get_resize_hint(&self) -> Option<ResizeHint>;
fn set_resize_hint(&mut self, new_size: Size);
fn clear_resize_hint(&mut self);
}
pub trait FunctionComponent<S: CalculateResizeHint> {
fn get_output_device(&mut self) -> OutputDevice;
fn calculate_header_viewport_height(&self, state: &mut S) -> ChUnit;
fn calculate_items_viewport_height(&self, state: &mut S) -> ChUnit;
/// # Errors
///
/// Returns an error if the rendering operation fails.
fn render(&mut self, state: &mut S) -> miette::Result<()>;
/// # Errors
///
/// Returns an error if the viewport allocation fails.
fn allocate_viewport_height_space(&mut self, state: &mut S) -> miette::Result<()> {
throws!({
let viewport_height =
/* not including the header */ self.calculate_items_viewport_height(state) +
/* for header row(s) */ self.calculate_header_viewport_height(state);
// Allocate space. This is required so that the commands to move the cursor up
// and down shown below will work.
for _ in 0..*viewport_height {
println!();
}
// Move the cursor back up.
queue_commands! {
self.get_output_device(),
MoveToPreviousLine(*viewport_height),
};
});
}
/// # Errors
///
/// Returns an error if clearing the viewport fails.
fn clear_viewport_for_resize(&mut self, state: &mut S) -> miette::Result<()> {
throws!({
DEVELOPMENT_MODE.then(|| {
// % is Display, ? is Debug.
tracing::debug!(
message = "🥑🥑🥑 clear viewport for resize",
resize_hint = ?state.get_resize_hint()
);
});
let viewport_height = match state.get_resize_hint() {
// Resize happened.
Some(
ResizeHint::GotBigger | ResizeHint::NoChange | ResizeHint::GotSmaller,
) => {
/* not including the header */
self.calculate_items_viewport_height(state) +
/* for header row(s) */
self.calculate_header_viewport_height(state)
}
// Nothing to do, since resize didn't happen.
None => return Ok(()),
};
// Clear the viewport.
for _ in 0..*viewport_height {
queue_commands! {
self.get_output_device(),
Clear(ClearType::FromCursorDown),
MoveToNextLine(1),
};
}
// Move the cursor back up.
queue_commands! {
self.get_output_device(),
MoveToPreviousLine(*viewport_height),
};
// Clear resize hint.
state.clear_resize_hint();
});
}
/// # Errors
///
/// Returns an error if clearing the viewport fails.
fn clear_viewport(&mut self, state: &mut S) -> miette::Result<()> {
throws!({
let viewport_height =
/* not including the header */ self.calculate_items_viewport_height(state) +
/* for header row(s) */ self.calculate_header_viewport_height(state);
// Clear the viewport.
for _ in 0..*viewport_height {
queue_commands! {
self.get_output_device(),
Clear(ClearType::CurrentLine),
MoveToNextLine(1),
};
}
// Move the cursor back up.
queue_commands! {
self.get_output_device(),
MoveToPreviousLine(*viewport_height),
};
});
}
}