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
use crate::highlighter::Highlighter;
use crate::tokens::{TokOpt, Token};
#[macro_export]
macro_rules! glen {
($e:expr) => {
$e.chars().collect::<Vec<_>>().len()
};
}
#[macro_export]
macro_rules! gidx {
($e:expr, $i:expr) => {
$e.chars().nth($i).unwrap().len_utf8()
};
}
#[must_use]
pub fn trim(input: &[Token], start: usize) -> Vec<Token> {
let mut opt = Highlighter::from_stream(input);
let mut total_width = 0;
for i in &opt {
let (TokOpt::Some(txt, _) | TokOpt::None(txt)) = i;
total_width += txt.len();
}
let width = total_width.saturating_sub(start);
while total_width != width {
if let Some(token) = opt.get_mut(0) {
token.nibble();
total_width -= 1;
if token.is_empty() {
opt.remove(0);
}
} else {
break;
}
}
Highlighter::from_opt(&opt)
}