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
use std::sync::LazyLock;
use crate::grapheme::StyledGraphemes;
pub static EMPTY_PANE: LazyLock<Pane> = LazyLock::new(|| Pane::new(vec![], 0));
#[derive(Clone)]
pub struct Pane {
/// The layout of graphemes within the pane.
/// This vector stores the styled graphemes that make up the content of the pane.
layout: Vec<StyledGraphemes>,
/// The offset from the top of the pane, used when extracting graphemes to display.
/// This value determines the starting point for grapheme extraction, allowing for scrolling behavior.
offset: usize,
}
impl Pane {
/// Constructs a new `Pane` with the specified layout, offset, and optional fixed height.
/// - `layout`: A vector of `StyledGraphemes` representing the content of the pane.
/// - `offset`: The initial offset from the top of the pane.
pub fn new(layout: Vec<StyledGraphemes>, offset: usize) -> Self {
Pane { layout, offset }
}
pub fn visible_row_count(&self) -> usize {
self.layout.len()
}
/// Checks if the pane is empty.
pub fn is_empty(&self) -> bool {
self.layout.is_empty()
}
pub fn extract(&self, viewport_height: usize) -> Vec<StyledGraphemes> {
let lines = self.layout.len().min(viewport_height);
let mut start = self.offset;
let end = self.offset + lines;
if end > self.layout.len() {
start = self.layout.len().saturating_sub(lines);
}
self.layout
.iter()
.enumerate()
.filter(|(i, _)| start <= *i && *i < end)
.map(|(_, row)| row.clone())
.collect::<Vec<_>>()
}
}
#[cfg(test)]
mod test {
use super::*;
mod visible_row_count {
use super::*;
#[test]
fn test() {
let pane = Pane::new(vec![], 0);
assert_eq!(0, pane.visible_row_count())
}
}
mod is_empty {
use super::*;
#[test]
fn test() {
assert_eq!(
true,
Pane {
layout: StyledGraphemes::from("").matrixify(10, 10, 0).0,
offset: 0,
}
.is_empty()
);
}
}
mod extract {
use super::*;
#[test]
fn test_with_less_extraction_size_than_layout() {
let expect = vec![
StyledGraphemes::from("aa"),
StyledGraphemes::from("bb"),
StyledGraphemes::from("cc"),
];
assert_eq!(
expect,
Pane {
layout: vec![
StyledGraphemes::from("aa"),
StyledGraphemes::from("bb"),
StyledGraphemes::from("cc"),
StyledGraphemes::from("dd"),
StyledGraphemes::from("ee"),
],
offset: 0,
}
.extract(3)
);
}
#[test]
fn test_with_much_extraction_size_than_layout() {
let expect = vec![
StyledGraphemes::from("aa"),
StyledGraphemes::from("bb"),
StyledGraphemes::from("cc"),
StyledGraphemes::from("dd"),
StyledGraphemes::from("ee"),
];
assert_eq!(
expect,
Pane {
layout: vec![
StyledGraphemes::from("aa"),
StyledGraphemes::from("bb"),
StyledGraphemes::from("cc"),
StyledGraphemes::from("dd"),
StyledGraphemes::from("ee"),
],
offset: 0,
}
.extract(10)
);
}
#[test]
fn test_with_within_extraction_size_and_offset_non_zero() {
let expect = vec![StyledGraphemes::from("cc"), StyledGraphemes::from("dd")];
assert_eq!(
expect,
Pane {
layout: vec![
StyledGraphemes::from("aa"),
StyledGraphemes::from("bb"),
StyledGraphemes::from("cc"),
StyledGraphemes::from("dd"),
StyledGraphemes::from("ee"),
],
offset: 2, // indicate `cc`
}
.extract(2)
);
}
#[test]
fn test_with_beyond_extraction_size_and_offset_non_zero() {
let expect = vec![
StyledGraphemes::from("cc"),
StyledGraphemes::from("dd"),
StyledGraphemes::from("ee"),
];
assert_eq!(
expect,
Pane {
layout: vec![
StyledGraphemes::from("aa"),
StyledGraphemes::from("bb"),
StyledGraphemes::from("cc"),
StyledGraphemes::from("dd"),
StyledGraphemes::from("ee"),
],
offset: 3, // indicate `dd`
}
.extract(3)
);
}
}
}