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
use {
crate::{
Alignment,
MadSkin,
Spacing,
Fitter,
},
minimad::{Composite, Compound},
unicode_width::UnicodeWidthStr,
};
#[derive(Debug, Clone)]
pub struct FmtComposite<'s> {
pub composite: Composite<'s>,
pub visible_length: usize, pub spacing: Option<Spacing>,
}
impl<'s> FmtComposite<'s> {
pub fn new() -> Self {
FmtComposite {
composite: Composite::new(),
visible_length: 0,
spacing: None,
}
}
pub fn from(composite: Composite<'s>, skin: &MadSkin) -> Self {
FmtComposite {
visible_length: skin.visible_composite_length(&composite),
composite,
spacing: None,
}
}
pub fn from_compound(compound: Compound<'s>) -> Self {
let mut fc = Self::new();
fc.add_compound(compound);
fc
}
#[inline(always)]
pub const fn completions(&self) -> (usize, usize) {
match &self.spacing {
Some(spacing) => spacing.completions_for(self.visible_length),
None => (0, 0),
}
}
#[inline(always)]
pub fn add_compound(&mut self, compound: Compound<'s>) {
self.visible_length += compound.src.width();
self.composite.compounds.push(compound);
}
pub fn recompute_width(&mut self, skin: &MadSkin) {
self.visible_length = skin.visible_composite_length(&self.composite);
}
pub fn fit_width(&mut self, width: usize, align: Alignment, skin: &MadSkin) {
Fitter::for_align(align).fit(self, width, skin);
}
pub fn extend_width(&mut self, width: usize, align: Alignment) {
if let Some(ref mut spacing) = self.spacing {
if spacing.width < width {
spacing.width = width;
}
spacing.align = align;
} else if self.visible_length < width {
self.spacing = Some(Spacing { width, align });
}
}
pub fn fill_width(&mut self, width: usize, align: Alignment, skin: &MadSkin) {
self.fit_width(width, align, skin);
self.extend_width(width, align);
}
}
impl Default for FmtComposite<'_> {
fn default() -> Self {
Self::new()
}
}