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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use {
crate::*,
minimad::*,
unicode_width::{
UnicodeWidthChar,
UnicodeWidthStr,
},
};
pub static ELLIPSIS: &str = "…";
#[derive(Debug, Clone, Copy)]
pub struct Fitter {
mid_token_ellision: bool,
mid_compound_ellision: bool,
align: Alignment,
}
impl Default for Fitter {
fn default() -> Self {
Self {
mid_token_ellision: true,
mid_compound_ellision: true,
align: Alignment::Unspecified,
}
}
}
#[derive(Debug, Clone, Copy)]
struct CharInfo {
byte_idx: usize,
char: char,
width: usize,
}
fn str_char_infos(s: &str) -> Vec<CharInfo> {
s.char_indices()
.map(|(byte_idx, char)| CharInfo {
byte_idx,
char,
width: char.width().unwrap_or(0),
})
.collect()
}
#[derive(Debug, Clone)]
struct Zone {
compound_idx: usize,
byte_start_idx: usize,
byte_end_idx: usize,
char_infos: Vec<CharInfo>,
removable_width: usize,
}
impl Zone {
fn token(composite: &Composite, min_removable_width: usize) -> Vec<Zone> {
let mut zones = Vec::new();
for (compound_idx, compound) in composite.compounds.iter().enumerate() {
let s = compound.src;
if s.len() < min_removable_width + 2 {
continue;
}
let mut byte_start: Option<usize> = None;
for (byte_idx, char) in s.char_indices() {
if char.is_whitespace() {
if let Some(byte_start_idx) = byte_start {
if byte_idx - byte_start_idx >= min_removable_width + 2 {
let zs = &s[byte_start_idx..byte_idx];
let removable_width = zs.width();
if removable_width >= min_removable_width {
let char_infos = str_char_infos(zs);
zones.push(Zone {
compound_idx,
byte_start_idx,
byte_end_idx: byte_idx,
char_infos,
removable_width,
});
}
}
byte_start = None;
}
} else if byte_start.is_none() {
byte_start = Some(byte_idx);
}
}
if let Some(byte_start_idx) = byte_start {
let byte_end_idx = s.len();
if byte_end_idx - byte_start_idx >= min_removable_width + 2 {
let zs = &s[byte_start_idx..];
let removable_width = zs.width();
if removable_width >= min_removable_width {
let char_infos = str_char_infos(zs);
zones.push(Zone {
compound_idx,
byte_start_idx,
byte_end_idx,
char_infos,
removable_width,
});
}
}
}
}
zones
}
fn biggest_token(composite: &Composite, min_removable_width: usize) -> Option<Zone> {
Zone::token(composite, min_removable_width)
.drain(..)
.max_by_key(|z| z.removable_width)
}
fn compounds(composite: &Composite, min_removable_width: usize) -> Vec<Zone> {
composite.compounds.iter()
.enumerate()
.filter_map(|(compound_idx, compound)| {
let char_infos = str_char_infos(compound.src);
if char_infos.len() < 2 + min_removable_width {
return None;
}
let removable = &compound.src[char_infos[1].byte_idx..char_infos[char_infos.len()-1].byte_idx];
let removable_width = removable.width();
if removable_width < min_removable_width {
None
} else {
Some(Zone {
compound_idx,
byte_start_idx: 0,
byte_end_idx: compound.src.len(),
char_infos,
removable_width,
})
}
})
.collect()
}
fn biggest_compound(composite: &Composite, min_removable_width: usize) -> Option<Zone> {
Zone::compounds(composite, min_removable_width)
.drain(..)
.max_by_key(|z| z.removable_width)
}
fn cut(&self, composite: &mut Composite, to_remove: usize) -> usize {
if self.removable_width < 2 {
return 0;
}
let compound = &composite.compounds[self.compound_idx];
let len = self.char_infos.len();
let mut start_char_idx = len / 2;
let mut end_char_idx = start_char_idx;
let mut removed_width = 0;
loop {
if (end_char_idx-start_char_idx)%2 == 0 {
if end_char_idx + 1 >= len {
break;
}
end_char_idx += 1;
} else {
if start_char_idx <= 1 {
break;
}
start_char_idx -= 1;
}
let start_byte_idx = self.byte_start_idx + self.char_infos[start_char_idx].byte_idx;
let end_byte_idx = self.byte_start_idx + self.char_infos[end_char_idx].byte_idx;
removed_width = (&compound.src[start_byte_idx..end_byte_idx]).width();
if removed_width >= to_remove {
break;
}
}
let start_byte_idx = self.byte_start_idx + self.char_infos[start_char_idx].byte_idx;
let end_byte_idx = self.byte_start_idx + self.char_infos[end_char_idx].byte_idx;
let head = compound.sub(0, start_byte_idx);
let tail = compound.tail(end_byte_idx);
composite.compounds[self.compound_idx] = head;
composite.compounds.insert(self.compound_idx+1, Compound::raw_str(ELLIPSIS));
composite.compounds.insert(self.compound_idx+2, tail);
removed_width - 1
}
}
impl Fitter {
pub fn for_align(align: Alignment) -> Self {
let internal_ellision = align == Alignment::Unspecified;
Self {
mid_token_ellision: internal_ellision,
mid_compound_ellision: internal_ellision,
align,
}
}
pub fn fit<'s>(
self,
fc: &mut FmtComposite<'s>,
max_width: usize,
skin: &MadSkin
) {
if fc.visible_length <= max_width {
return;
} else if max_width == 0 {
fc.composite.compounds.clear();
fc.visible_length = 0;
return;
} else if max_width == 1 {
fc.composite.compounds.clear();
fc.composite.compounds.push(Compound::raw_str(ELLIPSIS));
fc.visible_length = 1;
return;
}
let mut excess = fc.visible_length - max_width;
if self.mid_token_ellision {
while excess > 0 {
let mut gain = 0;
if let Some(zone) = Zone::biggest_token(&fc.composite, 3) {
gain = zone.cut(&mut fc.composite, excess + 1);
}
if gain == 0 {
break;
}
excess -= gain.min(excess);
}
}
if self.mid_compound_ellision {
while excess > 0 {
let mut gain = 0;
if let Some(zone) = Zone::biggest_compound(&fc.composite, 2) {
gain = zone.cut(&mut fc.composite, excess + 1);
}
if gain == 0 {
break;
}
excess -= gain.min(excess);
}
}
if excess == 0 {
fc.recompute_width(skin);
return;
}
let compounds = &mut fc.composite.compounds;
let (mut excess_left, mut excess_right) = match self.align {
Alignment::Right => (excess + 1, 0),
Alignment::Left | Alignment:: Unspecified => (0, excess + 1),
Alignment::Center => {
let left = excess / 2;
let right = excess - left;
if left > 0 {
(left + 1, right + 1)
} else {
(0, right + 1)
}
},
};
if excess_left > 0 {
while excess_left > 0 && !compounds.is_empty() {
let compound = &mut compounds[0];
let char_infos = str_char_infos(compound.src);
let mut last_removed_char_idx = 0;
let mut removed_width = 0;
loop {
removed_width += char_infos[last_removed_char_idx].width;
if removed_width >= excess_left || last_removed_char_idx + 1 == char_infos.len() {
break;
}
last_removed_char_idx += 1;
}
if last_removed_char_idx + 1 == char_infos.len() {
compounds.remove(0);
excess_left -= removed_width.min(excess_left);
} else {
compound.src = &compound.src[char_infos[last_removed_char_idx+1].byte_idx..];
excess_left = 0;
}
}
compounds.insert(0, Compound::raw_str(ELLIPSIS));
}
if excess_right > 0 {
while excess_right > 0 && !compounds.is_empty() {
let last_idx = compounds.len()-1;
let compound = &mut compounds[last_idx];
let char_infos = str_char_infos(compound.src);
let mut removed_width = 0;
let mut end_byte_idx = compound.src.len();
for ci in char_infos.iter().rev() {
end_byte_idx = ci.byte_idx;
removed_width += ci.width;
if removed_width >= excess_right {
break;
}
}
if end_byte_idx == 0 {
compounds.pop();
excess_right -= removed_width.min(excess_right);
} else {
compound.src = &compound.src[..end_byte_idx];
excess_right = 0;
}
}
compounds.push(Compound::raw_str(ELLIPSIS));
}
fc.recompute_width(skin);
}
}
#[cfg(test)]
mod fit_tests {
use minimad::{
Alignment,
Composite,
};
use crate::{
Fitter,
FmtComposite,
};
fn check_fit_align(src: &str, target_width: usize, align: Alignment) {
dbg!((target_width, align));
let skin = crate::get_default_skin();
let mut fc = FmtComposite::from(Composite::from_inline(src), &skin);
let fitter = Fitter::for_align(align);
fitter.fit(&mut fc, target_width, &skin);
dbg!(&fc);
assert!(fc.visible_length <= target_width);
}
fn check_fit(src: &str, target_width: usize) {
check_fit_align(src, target_width, Alignment::Right);
check_fit_align(src, target_width, Alignment::Left);
check_fit_align(src, target_width, Alignment::Center);
check_fit_align(src, target_width, Alignment::Unspecified);
}
#[test]
fn test_fit() {
let sentence = "This sentence has **short** and **much longer** parts, and some Korean: *一曰道,二曰天*.";
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);
}
}