src/message/printer.rs

207 lines · 5604 bytes

  1use std::borrow::Cow;
  2
  3use modalkit::tui::layout::Alignment;
  4use modalkit::tui::style::Style;
  5use modalkit::tui::text::{Span, Spans, Text};
  6use unicode_segmentation::UnicodeSegmentation;
  7use unicode_width::UnicodeWidthStr;
  8
  9use crate::util::{space_span, take_width};
 10
 11pub struct TextPrinter<'a> {
 12    text: Text<'a>,
 13    width: usize,
 14    base_style: Style,
 15    hide_reply: bool,
 16
 17    alignment: Alignment,
 18    curr_spans: Vec<Span<'a>>,
 19    curr_width: usize,
 20}
 21
 22impl<'a> TextPrinter<'a> {
 23    pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
 24        TextPrinter {
 25            text: Text::default(),
 26            width,
 27            base_style,
 28            hide_reply,
 29
 30            alignment: Alignment::Left,
 31            curr_spans: vec![],
 32            curr_width: 0,
 33        }
 34    }
 35
 36    pub fn align(mut self, alignment: Alignment) -> Self {
 37        self.alignment = alignment;
 38        self
 39    }
 40
 41    pub fn hide_reply(&self) -> bool {
 42        self.hide_reply
 43    }
 44
 45    pub fn width(&self) -> usize {
 46        self.width
 47    }
 48
 49    pub fn sub(&self, indent: usize) -> Self {
 50        TextPrinter {
 51            text: Text::default(),
 52            width: self.width.saturating_sub(indent),
 53            base_style: self.base_style,
 54            hide_reply: self.hide_reply,
 55
 56            alignment: self.alignment,
 57            curr_spans: vec![],
 58            curr_width: 0,
 59        }
 60    }
 61
 62    fn remaining(&self) -> usize {
 63        self.width - self.curr_width
 64    }
 65
 66    pub fn commit(&mut self) {
 67        if self.curr_width > 0 {
 68            self.push_break();
 69        }
 70    }
 71
 72    fn push(&mut self) {
 73        self.curr_width = 0;
 74        self.text.lines.push(Spans(std::mem::take(&mut self.curr_spans)));
 75    }
 76
 77    pub fn push_break(&mut self) {
 78        if self.curr_width == 0 && self.text.lines.is_empty() {
 79            // Disallow leading breaks.
 80            return;
 81        }
 82
 83        let remaining = self.remaining();
 84
 85        if remaining > 0 {
 86            match self.alignment {
 87                Alignment::Left => {
 88                    let tspan = space_span(remaining, self.base_style);
 89                    self.curr_spans.push(tspan);
 90                },
 91                Alignment::Center => {
 92                    let trailing = remaining / 2;
 93                    let leading = remaining - trailing;
 94
 95                    let tspan = space_span(trailing, self.base_style);
 96                    let lspan = space_span(leading, self.base_style);
 97
 98                    self.curr_spans.push(tspan);
 99                    self.curr_spans.insert(0, lspan);
100                },
101                Alignment::Right => {
102                    let lspan = space_span(remaining, self.base_style);
103                    self.curr_spans.insert(0, lspan);
104                },
105            }
106        }
107
108        self.push();
109    }
110
111    fn push_str_wrapped<T>(&mut self, s: T, style: Style)
112    where
113        T: Into<Cow<'a, str>>,
114    {
115        let style = self.base_style.patch(style);
116        let mut cow = s.into();
117
118        loop {
119            let sw = UnicodeWidthStr::width(cow.as_ref());
120
121            if self.curr_width + sw <= self.width {
122                // The text fits within the current line.
123                self.curr_spans.push(Span::styled(cow, style));
124                self.curr_width += sw;
125                break;
126            }
127
128            // Take a leading portion of the text that fits in the line.
129            let ((s0, w), s1) = take_width(cow, self.remaining());
130            cow = s1;
131
132            self.curr_spans.push(Span::styled(s0, style));
133            self.curr_width += w;
134
135            self.commit();
136        }
137
138        if self.curr_width == self.width {
139            // If the last bit fills the full line, start a new one.
140            self.push();
141        }
142    }
143
144    pub fn push_span_nobreak(&mut self, span: Span<'a>) {
145        let sw = UnicodeWidthStr::width(span.content.as_ref());
146
147        if self.curr_width + sw > self.width {
148            // Span doesn't fit on this line, so start a new one.
149            self.commit();
150        }
151
152        self.curr_spans.push(span);
153        self.curr_width += sw;
154    }
155
156    pub fn push_str(&mut self, s: &'a str, style: Style) {
157        let style = self.base_style.patch(style);
158
159        for word in UnicodeSegmentation::split_word_bounds(s) {
160            if self.width == 0 && word.chars().all(char::is_whitespace) {
161                // Drop leading whitespace.
162                continue;
163            }
164
165            let sw = UnicodeWidthStr::width(word);
166
167            if sw > self.width {
168                self.push_str_wrapped(word, style);
169                continue;
170            }
171
172            if self.curr_width + sw > self.width {
173                // Word doesn't fit on this line, so start a new one.
174                self.commit();
175
176                if word.chars().all(char::is_whitespace) {
177                    // Drop leading whitespace.
178                    continue;
179                }
180            }
181
182            let span = Span::styled(word, style);
183            self.curr_spans.push(span);
184            self.curr_width += sw;
185        }
186
187        if self.curr_width == self.width {
188            // If the last bit fills the full line, start a new one.
189            self.push();
190        }
191    }
192
193    pub fn push_line(&mut self, spans: Spans<'a>) {
194        self.commit();
195        self.text.lines.push(spans);
196    }
197
198    pub fn push_text(&mut self, text: Text<'a>) {
199        self.commit();
200        self.text.lines.extend(text.lines);
201    }
202
203    pub fn finish(mut self) -> Text<'a> {
204        self.commit();
205        self.text
206    }
207}