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
use With;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use utils::prefix;
pub struct LinesIterator<'a> {
content: &'a str,
offset: usize,
width: usize,
show_spaces: bool,
}
impl<'a> LinesIterator<'a> {
pub fn new(content: &'a str, width: usize) -> Self {
LinesIterator {
content: content,
width: width,
offset: 0,
show_spaces: false,
}
}
pub fn show_spaces(mut self) -> Self {
self.show_spaces = true;
self
}
}
#[derive(Debug, Clone, Copy)]
pub struct Row {
pub start: usize,
pub end: usize,
pub width: usize,
}
impl Row {
pub fn shift(&mut self, offset: usize) {
self.start += offset;
self.end += offset;
}
pub fn shifted(self, offset: usize) -> Self {
self.with(|s| s.shift(offset))
}
pub fn rev_shift(&mut self, offset: usize) {
self.start -= offset;
self.end -= offset;
}
}
impl<'a> Iterator for LinesIterator<'a> {
type Item = Row;
fn next(&mut self) -> Option<Row> {
if self.offset >= self.content.len() {
return None;
}
let start = self.offset;
let content = &self.content[start..];
let next = content.find('\n').unwrap_or_else(|| content.len());
let content = &content[..next];
let allowed_width = if self.show_spaces {
self.width.saturating_sub(1)
} else {
self.width
};
let line_width = content.width();
if line_width <= allowed_width {
self.offset += next + 1;
return Some(Row {
start: start,
end: start + next,
width: line_width,
});
}
let prefix_length =
match prefix(content.split(' '), allowed_width, " ").length {
0 => prefix(content.graphemes(true), self.width, "").length,
other => {
self.offset += 1;
other
}
};
if prefix_length == 0 {
return None;
}
self.offset += prefix_length;
Some(Row {
start: start,
end: start + prefix_length,
width: self.width,
})
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_layout() {}
}