1use {
2 crate::*,
3 minimad::{
4 Composite,
5 Compound,
6 },
7 unicode_width::UnicodeWidthStr,
8};
9
10#[derive(Debug, Clone)]
13pub struct FmtComposite<'s> {
14 pub kind: CompositeKind,
15
16 pub compounds: Vec<Compound<'s>>,
17
18 pub visible_length: usize,
20
21 pub spacing: Option<Spacing>,
22}
23
24impl<'s> FmtComposite<'s> {
25 pub fn new() -> Self {
26 FmtComposite {
27 kind: CompositeKind::Paragraph,
28 compounds: Vec::new(),
29 visible_length: 0,
30 spacing: None,
31 }
32 }
33 pub fn from(composite: Composite<'s>, skin: &MadSkin) -> Self {
34 let kind: CompositeKind = composite.style.into();
35 FmtComposite {
36 visible_length: skin.visible_composite_length(kind, &composite.compounds),
37 kind,
38 compounds: composite.compounds,
39 spacing: None,
40 }
41 }
42 pub fn from_compound(compound: Compound<'s>) -> Self {
43 let mut fc = Self::new();
44 fc.add_compound(compound);
45 fc
46 }
47 pub const fn completions(&self) -> (usize, usize) {
50 match &self.spacing {
51 Some(spacing) => spacing.completions_for(self.visible_length),
52 None => (0, 0),
53 }
54 }
55 pub fn add_compound(&mut self, compound: Compound<'s>) {
57 self.visible_length += compound.src.width();
58 self.compounds.push(compound);
59 }
60 pub fn recompute_width(&mut self, skin: &MadSkin) {
66 self.visible_length = skin.visible_composite_length(self.kind, &self.compounds);
67 }
68 pub fn fit_width(&mut self, width: usize, align: Alignment, skin: &MadSkin) {
77 Fitter::for_align(align).fit(self, width, skin);
78 }
79 pub fn extend_width(&mut self, width: usize, align: Alignment) {
82 if let Some(ref mut spacing) = self.spacing {
83 if spacing.width < width {
84 spacing.width = width;
85 }
86 spacing.align = align;
87 } else if self.visible_length < width {
88 self.spacing = Some(Spacing { width, align });
89 }
90 }
91 pub fn fill_width(&mut self, width: usize, align: Alignment, skin: &MadSkin) {
96 self.fit_width(width, align, skin);
97 self.extend_width(width, align);
98 }
99}
100
101impl Default for FmtComposite<'_> {
102 fn default() -> Self {
103 Self::new()
104 }
105}