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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::config::CONFIG;
use crate::ui::article::links::LinkHandler;
use crate::wiki::article::{Element, ElementType};
use cursive::theme::Style;
use std::collections::HashMap;
use std::mem;
use std::rc::Rc;
/// An element only containing the neccessary information for rendering (and an id so that it can
/// be referenced to an article element
#[derive(Debug)]
pub struct RenderedElement {
/// The id of the ArticleElement this element belongs to
pub id: usize,
/// The content of the element
pub content: String,
/// The style of the element
pub style: Style,
/// The width of the element. Measured by the amount of characters in the content
pub width: usize,
}
impl RenderedElement {
/// Appends a string to the content of the element
pub fn push_str(&mut self, string: &str) {
self.width += string.chars().count();
self.content.push_str(string);
}
/// Appends a character to the content of the element
pub fn push(&mut self, char: char) {
self.width += 1;
self.content.push(char);
}
}
pub type Line = Vec<RenderedElement>;
/// Generates lines of elements in constrained width
pub struct LinesWrapper {
/// The line that is currently being rendered
current_line: Line,
/// The width of the current line
current_width: usize,
/// The maximal width a line can have
width: usize,
/// The length of the longest rendered line
pub max_width: usize,
/// Are any lines wrapped?
pub is_wrapped: bool,
/// A referece to the article elements
elements: Rc<Vec<Element>>,
/// The rendered lines
pub rendered_lines: Vec<Line>,
/// The link handler, it is only created and used when enabled in the config
pub link_handler: Option<LinkHandler>,
/// The y coordinates of the headers, it is only created and used when enabled in the config
pub header_y: Option<HashMap<usize, usize>>,
}
impl LinesWrapper {
/// Creates a new LinesWrapper with a content and constraint
pub fn new(width: usize, elements: Rc<Vec<Element>>) -> Self {
LinesWrapper {
current_line: Line::new(),
current_width: 0,
width,
max_width: 0,
is_wrapped: false,
elements,
rendered_lines: Vec::new(),
link_handler: {
if CONFIG.features.links {
Some(LinkHandler::new())
} else {
None
}
},
header_y: {
if CONFIG.features.toc {
Some(HashMap::new())
} else {
None
}
},
}
}
/// Wraps the lines and returns the required width. This method is way cheaper than wrap_lines
/// because it only calculates the required width and nothing else
pub fn required_width(mut self) -> usize {
debug!("calculating the required with");
// go through every elment
for element in self.elements.iter() {
// does this element go onto a new line?
if element.kind() == ElementType::Newline {
// "add" the element onto a new line
self.current_line = Line::new();
self.current_width = element.width();
// store the width of the element if it is the biggest one yet
if element.width() > self.max_width {
self.max_width = element.width();
continue;
}
}
// does it fit into the current line?
if element.width() + self.current_width < self.width {
// yay, it fits
// add its width to the current line
self.current_width += element.width();
// store the width of the element if it is the biggest one yet
if element.width() > self.max_width {
self.max_width = element.width();
continue;
}
}
// if it doesn't fit, return 0
debug!("the lines are wrapped, returning 0");
return 0;
}
debug!("finished with a required size of '{}'", self.max_width);
self.max_width
}
/// Starts the wrapping process
#[must_use]
pub fn wrap_lines(mut self) -> Self {
debug!("wrapping the lines");
// go through every element
for element in self.elements.clone().iter() {
// is this a link?
let is_link = element.kind() == ElementType::Link;
// is this a toc header?
let is_header = element.kind() == ElementType::Header;
// does this element go onto a new line?
if element.kind() == ElementType::Newline {
// fill the current line and make the next one blank
self.fill_line();
self.newline();
continue;
}
// what we do here is fairly simple:
// First, we split the content into words and then we merge these words together until the
// line is full. Then we create a new one and do the same thing over and over again until
// we run out of words.
let mut merged_element = RenderedElement {
id: element.id(),
style: element.style(),
content: String::new(),
width: 0,
};
// now our lines are wrapped
self.is_wrapped = true;
// if the element does not have a leading special character and we are not at the beginning
// of a line, add a leading whitespace
if !element.content().starts_with([',', '.', ';', ':']) && !self.current_line.is_empty()
{
self.push_whitespace();
}
for span in element.content().split_whitespace() {
// does the span fit onto the current line?
if span.chars().count() + merged_element.width + self.current_width < self.width {
// only add a leading whitespace if the merged element is not empty
if !merged_element.content.is_empty() {
merged_element.push(' ');
}
// then add it to the merged element
merged_element.push_str(span);
continue;
}
// now we have to do the following things:
// - add the merged element to the current line
// - fill the current line and replace it with a new one
// - add the span to a new merged element
self.current_width += merged_element.width;
self.current_line.push(merged_element);
// if its a link, add it
if is_link {
self.register_link(element.id())
}
// if its a toc header, register it
if is_header {
self.register_header(element.id(), self.rendered_lines.len());
}
self.fill_line();
self.newline();
merged_element = RenderedElement {
id: element.id(),
style: element.style(),
content: String::new(),
width: 0,
};
// does the span fit onto the current line?
if span.chars().count() + merged_element.width + self.current_width < self.width {
// only add a leading whitespace if the merged element is not empty
if !merged_element.content.is_empty() {
merged_element.push(' ');
}
// then add it to the merged element
merged_element.push_str(span);
continue;
}
}
// if there are still some spans in the merged_element, add it to the current line and
// register a link if it is one
if !merged_element.content.is_empty() {
self.current_width += merged_element.width;
self.current_line.push(merged_element);
if is_link {
self.register_link(element.id());
}
if is_header {
self.register_header(element.id(), self.rendered_lines.len());
}
}
}
if let Some(ref header_y) = self.header_y {
debug!("'{}' headers registered", header_y.len());
}
if let Some(ref link_handler) = self.link_handler {
debug!("'{}' links found", link_handler.registered_links());
}
debug!("wrapped '{}' lines", self.rendered_lines.len());
self
}
// Registers a new header. If the headers is already registered, it won't be registered again
fn register_header(&mut self, id: usize, y_pos: usize) {
if let Some(ref mut header_y) = self.header_y {
if header_y.contains_key(&id) {
return;
}
header_y.insert(id, y_pos);
}
}
/// Registers a new link with the given id
fn register_link(&mut self, id: usize) {
if let Some(ref mut link_handler) = self.link_handler {
link_handler.push_link(
id,
self.current_line.len().saturating_sub(1),
self.rendered_lines.len().saturating_sub(1),
);
}
}
/// Adds an element to the current line and if needed, registers a link to it
fn push_element(&mut self, element: RenderedElement) {
self.current_width += element.width;
self.current_line.push(element);
}
/// Adds a whitespacde to the current line
fn push_whitespace(&mut self) {
// check if we can add a whitespace
if self.current_width == self.width {
return;
}
// create a rendered element with the id -1 and push it to the current line
self.push_element(RenderedElement {
id: usize::MAX,
content: " ".to_string(),
style: Style::from(CONFIG.theme.text),
width: 1,
});
}
/// Adds the current line to the rendered lines and replaces it with a new, empty one
fn newline(&mut self) {
// add the current line to the rendered lines
self.rendered_lines.push(mem::take(&mut self.current_line));
// and reset the current line afterwards
self.current_width = 0;
}
/// Fills the remaining space of the line with spaces
fn fill_line(&mut self) {
// if our current line is wider than allowed, we really messed up
assert!(self.current_width <= self.width);
// change the max width, if neccessary
if self.current_width > self.max_width {
self.max_width = self.current_width;
}
// just create an empty element that filles the whole line
let remaining_width = self.width - self.current_width;
self.create_rendered_element(
&usize::MAX,
&Style::none(),
&" ".repeat(remaining_width),
&remaining_width,
);
}
/// Creates a rendered element and adds it to the current line
fn create_rendered_element(&mut self, id: &usize, style: &Style, content: &str, width: &usize) {
// we can just clone the whole thing and call the push_element function
self.push_element(RenderedElement {
id: *id,
style: *style,
content: {
// if the line is empty, remove leading whitespace
if self.current_line.is_empty() {
content.trim_start().to_string()
} else {
content.to_string()
}
},
width: *width,
});
}
}