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
#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub enum WrappingStrategy {
None,
AtCol(usize),
SpaceBeforeCol(usize),
AtColWithHypen(usize),
Cutoff(usize),
Ellipsis(usize),
}
impl Default for WrappingStrategy {
fn default() -> Self {
WrappingStrategy::None
}
}
impl WrappingStrategy {
pub fn wrap(&self, input: &str) -> Vec<String> {
match self {
WrappingStrategy::None => vec![input.to_string()],
WrappingStrategy::AtCol(col) => {
let mut output = vec![];
let mut text = input.to_string();
while text.chars().count() > *col {
output.push(text.chars().take(*col).collect());
text = text.chars().skip(*col).collect();
}
output.push(text);
output
}
WrappingStrategy::SpaceBeforeCol(col) => {
let mut output = vec![];
let mut text = input.to_string();
while text.chars().count() > *col {
let chars: Vec<char> = text.chars().collect();
let line: String = text.chars().take(*col).collect();
if line.ends_with(|c: char| c.is_whitespace())
|| text
.chars()
.nth(*col)
.map(|c| c.is_whitespace())
.unwrap_or(false)
{
output.push(text.chars().take(*col).collect());
text = text.chars().skip(*col).collect();
} else {
let mut whitespace = chars
.iter()
.take(*col)
.rposition(|c| c.is_whitespace())
.unwrap_or(0);
if whitespace == 0 {
whitespace = *col
}
output.push(text.chars().take(whitespace).collect());
text = text.chars().skip(whitespace).collect();
}
}
output.push(text);
output.iter().map(|s| s.trim().to_string()).collect()
}
WrappingStrategy::AtColWithHypen(col) => {
let mut output = vec![];
let mut text = input.to_string();
while text.chars().count() > *col {
let line: String = text.chars().take(*col).collect();
if line.chars().last().unwrap_or(' ').is_alphabetic()
&& text.chars().next().unwrap_or(' ').is_alphabetic()
{
output.push(format!("{}-", line));
} else {
output.push(line);
}
text = text.chars().skip(*col).collect();
}
output.push(text);
output
}
WrappingStrategy::Cutoff(col) => vec![input.chars().take(*col).collect()],
WrappingStrategy::Ellipsis(col) => {
if input.chars().count() >= *col {
vec![format!("{}…", input.chars().take(*col).collect::<String>())]
} else {
vec![input.to_string()]
}
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
use WrappingStrategy::*;
fn c(list: &[&str]) -> Vec<String> {
list.iter().map(|s| s.to_string()).collect()
}
#[test]
fn none() {
assert_eq!(None.wrap("this is a test"), c(&["this is a test"]));
}
#[test]
fn at_col() {
assert_eq!(
AtCol(4).wrap("some words, and some are longer"),
c(&["some", " wor", "ds, ", "and ", "some", " are", " lon", "ger"])
);
}
#[test]
fn at_col_hypen() {
assert_eq!(
AtCol(4).wrap("some words, and some are longer"),
c(&["some", " wor", "ds, ", "and ", "some", " are", " lon", "ger"])
);
}
#[test]
fn space_before_col() {
assert_eq!(
SpaceBeforeCol(4).wrap("some words, and some are longer"),
c(&["some", "wor", "ds,", "and", "some", "are", "lon", "ger"])
);
assert_eq!(
SpaceBeforeCol(8).wrap("some words, and some are longer"),
c(&["some", "words,", "and some", "are", "longer"])
);
}
#[test]
fn cutoff() {
assert_eq!(Cutoff(30).wrap("short test"), c(&["short test"]));
assert_eq!(Cutoff(10).wrap("longer test string"), c(&["longer tes"]));
}
#[test]
fn ellipsis() {
assert_eq!(Ellipsis(30).wrap("short test"), c(&["short test"]));
assert_eq!(Ellipsis(10).wrap("longer test string"), c(&["longer tes…"]));
}
}