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
use minimad::{
Compound,
};
use crate::{
Alignment,
FmtComposite,
MadSkin,
};
pub static ELLIPSIS: &str = "…";
pub struct Fitter {
pub min_kept_after_ellision: usize,
pub allow_ellision: bool,
pub prefer_mid_token_ellision: bool,
pub allow_removals: bool,
}
impl Default for Fitter {
fn default() -> Self {
Self {
min_kept_after_ellision: 4,
allow_ellision: true,
prefer_mid_token_ellision: true,
allow_removals: true,
}
}
}
impl Fitter {
pub fn fit(
&self,
fc: &mut FmtComposite<'_>,
max_width: usize,
align: Alignment,
skin: &MadSkin
) {
if fc.visible_length <= max_width {
if let Some(ref mut spacing) = fc.spacing {
if spacing.width > max_width {
spacing.width = max_width;
}
}
return;
}
let mut to_remove = fc.visible_length - max_width;
if self.allow_ellision {
while to_remove > 0 {
let mut compound_width = self.min_kept_after_ellision + 2;
let mut longest: Option<usize> = None;
for (idx, compound) in fc.composite.compounds.iter().enumerate() {
let len = compound.char_length();
if len >= compound_width {
longest = Some(idx);
compound_width = len;
}
}
if let Some(idx) = longest {
let gain = to_remove.min(compound_width - self.min_kept_after_ellision - 1);
let left = compound_width - gain - 1;
let mut left_tail = left / 2;
let mut left_head = left - left_tail;
if self.prefer_mid_token_ellision && gain < compound_width + 1 {
let mut token_start: Option<usize> = None;
let mut biggest_token_width = gain + 1;
let mut biggest_token_start: Option<usize> = None;
for (char_idx, char) in fc.composite.compounds[idx].as_str().chars().enumerate() {
if char.is_whitespace() {
if let Some(ts) = token_start {
let token_width = char_idx - ts;
if token_width > biggest_token_width {
biggest_token_start = Some(ts);
biggest_token_width = token_width;
}
token_start = None;
}
} else {
if token_start.is_none() {
token_start = Some(char_idx);
}
}
}
if let Some(token_start) = token_start {
let token_width = compound_width - 1 - token_start;
if token_width > biggest_token_width {
biggest_token_start = Some(token_start);
biggest_token_width = token_width;
}
}
if let Some(biggest_token_start) = biggest_token_start {
let r = biggest_token_width - gain - 1;
left_head = biggest_token_start + r / 2 + 1;
left_tail = left - left_head;
}
}
let head = fc.composite.compounds[idx].sub_chars(0, left_head);
let tail = fc.composite.compounds[idx].tail_chars(compound_width - left_tail);
fc.composite.compounds[idx]= head;
fc.composite.compounds.insert(idx+1, Compound::raw_str(ELLIPSIS));
fc.composite.compounds.insert(idx+2, tail);
to_remove -= gain;
} else {
break;
}
}
}
if self.allow_removals && to_remove > 0 {
fc.composite.remove_chars(to_remove, align);
}
fc.recompute_width(&skin);
if let Some(ref mut spacing) = fc.spacing {
spacing.width = fc.visible_length;
}
}
}
#[cfg(test)]
mod fit_tests {
use minimad::{
Alignment,
Composite,
};
use crate::{
Fitter,
FmtComposite,
};
fn check_fit(src: &str, target_width: usize) {
let skin = crate::get_default_skin();
let mut fc = FmtComposite::from(Composite::from_inline(src), &skin);
let fitter = Fitter::default();
fitter.fit(&mut fc, target_width, Alignment::Right, &skin);
assert_eq!(fc.visible_length, target_width);
}
#[test]
fn test_fit() {
let sentence = "This beautiful sentence has **short** and **much much much longer** parts.";
check_fit(sentence, 60);
check_fit(sentence, 40);
let five_issues = "一曰道,二曰天,三曰地,四曰將,五曰法。";
check_fit(five_issues, 15);
check_fit(five_issues, 8);
let status = "ab *cd* `12345 123456789`";
check_fit(status, 17);
check_fit(status, 2);
}
}