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
308
309
310
311
use crate::config::CONFIG;
use crate::ui::article::links::{Link, LinkHandler};
use crate::ui::article::view::{Element, Line, RenderedElement};
use cursive::theme::Style;
pub struct LinesWrapper {
width: usize,
pub lines_wrapped: bool,
pub headers: Vec<String>,
pub header_coords: Vec<usize>,
pub lines: Vec<Line>,
}
impl LinesWrapper {
pub fn new(width: usize, headers: Vec<String>) -> Self {
LinesWrapper {
width,
lines_wrapped: false,
headers,
header_coords: Vec::new(),
lines: Vec::new(),
}
}
pub fn calculate_lines(
mut self,
content: &[RenderedElement],
link_handler: &mut LinkHandler,
) -> Self {
// the width of the line that is currently calculated
let mut line_width: usize = 0;
let mut lines: Vec<Line> = Vec::new();
let mut current_line: Vec<Element> = Vec::new();
let mut lines_wrapped = false;
// this is to prevent the program to add more headers of the same name
self.header_coords.clear();
// go through every rendered element
for (idx, element) in content.iter().enumerate() {
log::debug!("Rendering now the element no: {}", idx);
let element_width = element.text.chars().count();
let link_index = element.link_destination.as_ref().map(|destination| {
link_handler.push(Link {
position: (line_width, lines.len()).into(),
width: element_width,
destination: destination.to_string(),
})
});
log::trace!(
"The element has the link_index: {:?}, and a width of: {}",
link_index,
element_width
);
if element.newline && element_width.eq(&0) {
log::debug!("Created a new line");
lines.push(std::mem::take(&mut current_line));
line_width = 0;
continue;
}
let is_header = self.headers.contains(&element.text);
// does the element fit in the current line?
if (line_width + element_width) < self.width {
// if it goes into a new line, add it to a new one
if element.newline {
log::debug!("Adding the element to a new line");
// fill the current line with empty spaces
current_line.push(Element {
text: " ".repeat(self.width - line_width).to_string(),
style: Style::from(CONFIG.theme.text),
width: self.width - line_width,
link_index: None,
});
// add the current line to the finished ones and add the new element to another
// line
line_width = self.add_element_to_new_line(
&mut lines,
&mut current_line,
element,
link_index,
);
if is_header {
self.headers.remove(0);
self.header_coords.push(lines.len());
}
// this element is finished, continue with the next one
continue;
}
// if the element goes to the current line, add it to the line
log::debug!("Adding the element to the current line");
current_line.push(self.create_element_from_rendered_element(element, link_index));
if is_header {
self.headers.remove(0);
self.header_coords.push(lines.len())
}
// don't forget to increase the line width and continue with the next element
line_width += element_width;
log::trace!("New line width: {}", line_width);
continue;
} else {
// hmm, the element doesn't fit into the current line
// however, does the element go into a new line? if so, check if it fits into a
// single line and split it if not
log::debug!("The element no: {} doesn't fit into the current line", idx);
if element.newline {
if element_width < self.width {
log::debug!("Adding the element to a new line");
// fill the current line with empty spaces
current_line.push(Element {
text: " ".repeat(self.width - line_width).to_string(),
style: Style::from(CONFIG.theme.text),
width: self.width - line_width,
link_index: None,
});
// add the current line to the finished ones and add the new element to another
// line
line_width = self.add_element_to_new_line(
&mut lines,
&mut current_line,
element,
link_index,
);
if is_header {
self.headers.remove(0);
self.header_coords.push(lines.len())
}
// this element is finished, continue with the next one
continue;
}
// so.. the element goes into a new line, but it doesn't fit into a single one
// well, then split it and add it to a new line
log::debug!("The element no: {} must be splitted", idx);
let mut new_lines = self.split_element(element, link_index, 0, self.width);
log::trace!("splitted lines: \n{:?}", new_lines);
line_width = 0;
lines_wrapped = true;
lines.push(std::mem::take(&mut current_line));
lines.append(&mut new_lines);
log::debug!("Added the current line and the new lines to the finished lines");
if is_header {
self.headers.remove(0);
self.header_coords.push(lines.len())
}
continue;
}
// split the element
log::debug!("The element will now be splitted");
lines_wrapped = true;
let mut new_lines = self.split_element(element, link_index, line_width, self.width);
log::trace!("splitted lines: \n{:?}", new_lines);
// the first line of these new lines is merged with the current line
current_line.append(&mut new_lines.remove(0));
log::debug!("merged the first of these new lines with the current one");
log::trace!("new_lines: \n{:?}", new_lines);
log::trace!("new_lines after pop(): \n{:?}", new_lines.pop());
lines.push(std::mem::replace(
&mut current_line,
new_lines.pop().unwrap_or_default(),
));
log::debug!("added the other new lines to the finished ones");
// all the other lines will be added to the finished lines
lines.append(&mut new_lines);
line_width = current_line.iter().map(|element| element.width).sum();
log::debug!("new line width: {}", line_width);
if is_header {
self.headers.remove(0);
self.header_coords.push(lines.len())
}
}
continue;
}
self.lines_wrapped = lines_wrapped;
// add the remaining line to the finished ones, because no elements are left
lines.push(current_line);
self.lines = lines;
self
}
fn create_element_from_rendered_element(
&self,
element: &RenderedElement,
link_index: Option<usize>,
) -> Element {
Element {
text: element.text.to_string(),
style: element.style,
width: element.text.chars().count(),
link_index,
}
}
fn split_element(
&self,
element: &RenderedElement,
link_index: Option<usize>,
line_width: usize,
max_width: usize,
) -> Vec<Line> {
let mut lines: Vec<Line> = Vec::new();
let mut current_line: Line = Vec::new();
let mut current_line_width = line_width;
// first, split the element into chunks
for (idx, chunk) in element.text.split(' ').enumerate().map(|(idx, chunk)| {
if idx == 0 {
(idx, chunk.to_string())
} else {
(idx, format!(" {}", chunk))
}
}) {
log::debug!("the chunk no: {} will now be added", idx);
let chunk_width = chunk.chars().count();
log::trace!("chunk width: {}", chunk_width);
// does the cunk fit into the current line? then add it and continue with the next one
if (current_line_width + chunk_width) < max_width {
current_line.push(Element {
text: chunk,
style: element.style,
width: chunk_width,
link_index,
});
log::debug!("Added the chunk to the current line");
current_line_width += chunk_width;
continue;
}
// if not, add it to a new line
let element_from_chunk = RenderedElement {
text: chunk,
style: element.style,
newline: element.newline,
link_destination: element.link_destination.clone(),
};
current_line_width = self.add_element_to_new_line(
&mut lines,
&mut current_line,
&element_from_chunk,
link_index,
);
log::debug!("Added the chunk no: {} to a new line", idx);
}
// add the remaining line to the finished ones, because no chunks are left
lines.push(current_line);
lines
}
fn add_element_to_new_line(
&self,
lines: &mut Vec<Line>,
current_line: &mut Line,
element: &RenderedElement,
link_index: Option<usize>,
) -> usize {
log::trace!("current_line: \n{:?}", current_line);
let element_width: usize;
lines.push(std::mem::replace(
current_line,
vec![{
let trimmed_element = RenderedElement {
text: element.text.trim_start().to_string(),
style: element.style,
newline: element.newline,
link_destination: element.link_destination.clone(),
};
let element =
self.create_element_from_rendered_element(&trimmed_element, link_index);
element_width = element.width;
element
}],
));
log::trace!("new current line: \n{:?}", current_line);
element_width
}
}