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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::line::LineId;
use crate::position::{AbsolutePosition, RelativePosition};
use crate::segment::SegmentId;
#[derive(Debug, Clone)]
pub struct InterfaceLayout {
terminal_size: (u16, u16),
lines: Vec<LineLayout>,
}
impl InterfaceLayout {
pub(crate) fn new(lines: Vec<LineLayout>, terminal_size: (u16, u16)) -> Self {
Self {
lines,
terminal_size,
}
}
pub fn terminal_size(&self) -> (u16, u16) {
self.terminal_size
}
pub fn lines(&self) -> &Vec<LineLayout> {
&self.lines
}
pub fn get_line(&self, line_id: LineId) -> Option<&LineLayout> {
self.lines
.iter()
.find(|layout| layout.line_id == Some(line_id))
}
pub fn rendered_line_count(&self) -> u16 {
self.lines
.iter()
.map(|line| line.wrapped_line_count())
.sum::<u16>()
}
pub fn end_position(&self) -> Option<AbsolutePosition> {
Some(
self.lines()
.iter()
.filter(|line| !line.segments().is_empty())
.last()?
.end_position()?,
)
}
pub fn get_absolute_position(&self, relative: RelativePosition) -> Option<AbsolutePosition> {
let index = relative.position() as usize;
let mut matched_segment = false;
for line_layout in self.lines.iter().rev() {
let mut matching_line = false;
if let Some(line_id) = line_layout.line_id {
if line_id == relative.line_id() {
matching_line = true;
}
}
for segment_layout in line_layout.segments.iter().rev() {
let mut matching_segment = false;
if let Some(segment_id) = segment_layout.segment_id {
if matching_line && segment_id == relative.segment_id() {
matched_segment = true;
matching_segment = true;
}
}
if matched_segment && !segment_layout.parts.is_empty() {
if matching_segment {
return segment_layout.get_absolute_position(index);
} else if index == 0 {
return segment_layout.end_position();
} else {
return None;
}
}
}
}
None
}
}
#[derive(Debug, Clone)]
pub struct LineLayout {
line_id: Option<LineId>,
segments: Vec<SegmentLayout>,
}
impl LineLayout {
pub(crate) fn new(line_id: LineId, segments: Vec<SegmentLayout>) -> Self {
Self {
line_id: Some(line_id),
segments,
}
}
pub fn line_id(&self) -> Option<LineId> {
self.line_id
}
pub fn segments(&self) -> &Vec<SegmentLayout> {
&self.segments
}
pub fn get_segment(&self, segment_id: SegmentId) -> Option<&SegmentLayout> {
self.segments
.iter()
.find(|segment| segment.segment_id == Some(segment_id))
}
pub fn get_segment_before(&self, segment_id: SegmentId) -> Option<&SegmentLayout> {
let mut previous_segment = None;
for segment in &self.segments {
if segment.segment_id == Some(segment_id) {
return previous_segment;
}
previous_segment = Some(segment);
}
None
}
pub fn wrapped_line_count(&self) -> u16 {
if self.segments.is_empty() {
0
} else {
self.segments
.iter()
.map(|segment| segment.wrapped_line_count())
.sum::<u16>()
+ 1
}
}
pub fn end_position(&self) -> Option<AbsolutePosition> {
for segment in self.segments.iter().rev() {
if !segment.parts().is_empty() {
return segment.end_position();
}
}
None
}
}
impl Default for LineLayout {
fn default() -> Self {
Self {
line_id: None,
segments: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct SegmentLayout {
segment_id: Option<SegmentId>,
parts: Vec<PartLayout>,
}
impl SegmentLayout {
pub(crate) fn new(segment_id: SegmentId, parts: Vec<PartLayout>) -> Self {
Self {
segment_id: Some(segment_id),
parts,
}
}
pub(crate) fn default() -> Self {
Self {
segment_id: None,
parts: Vec::new(),
}
}
pub fn segment_id(&self) -> Option<SegmentId> {
self.segment_id
}
pub fn parts(&self) -> &Vec<PartLayout> {
&self.parts
}
pub fn wrapped_line_count(&self) -> u16 {
if self.parts.is_empty() {
0
} else {
self.parts.len() as u16 - 1
}
}
pub fn end_position(&self) -> Option<AbsolutePosition> {
Some(self.parts.last()?.end_position())
}
pub fn get_absolute_position(&self, grapheme_index: usize) -> Option<AbsolutePosition> {
for part_index in 0..self.parts.len() {
let part_layout = &self.parts[part_index];
if part_layout.end > grapheme_index
|| part_layout.end == grapheme_index && part_index + 1 == self.parts.len()
{
return part_layout.get_position(grapheme_index);
}
}
None
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PartLayout {
position: AbsolutePosition,
start: usize,
end: usize,
widths: Vec<usize>,
}
impl PartLayout {
pub(crate) fn new(
position: AbsolutePosition,
start: usize,
end: usize,
widths: Vec<usize>,
) -> Self {
Self {
position,
start,
end,
widths,
}
}
pub fn position(&self) -> AbsolutePosition {
self.position
}
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
pub fn widths(&self) -> &Vec<usize> {
&self.widths
}
pub fn end_position(&self) -> AbsolutePosition {
self.get_position(self.end).unwrap()
}
pub fn length(&self) -> usize {
self.end - self.start
}
pub fn get_position(&self, index: usize) -> Option<AbsolutePosition> {
if index >= self.start && index <= self.end {
let preceding_width = self.widths.iter().take(index - self.start).sum::<usize>() as i16;
let position = self.position.add_columns(preceding_width);
Some(position)
} else {
None
}
}
}