src/message/printer.rs

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