src/message/printer.rs
212 lines · 5753 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 mut word in UnicodeSegmentation::split_word_bounds(s) {
160 if let "\n" | "\r\n" = word {
161 // Render embedded newlines as spaces.
162 word = " ";
163 }
164
165 if self.curr_width == 0 && word.chars().all(char::is_whitespace) {
166 // Drop leading whitespace.
167 continue;
168 }
169
170 let sw = UnicodeWidthStr::width(word);
171
172 if sw > self.width {
173 self.push_str_wrapped(word, style);
174 continue;
175 }
176
177 if self.curr_width + sw > self.width {
178 // Word doesn't fit on this line, so start a new one.
179 self.commit();
180
181 if word.chars().all(char::is_whitespace) {
182 // Drop leading whitespace.
183 continue;
184 }
185 }
186
187 let span = Span::styled(word, style);
188 self.curr_spans.push(span);
189 self.curr_width += sw;
190 }
191
192 if self.curr_width == self.width {
193 // If the last bit fills the full line, start a new one.
194 self.push();
195 }
196 }
197
198 pub fn push_line(&mut self, spans: Spans<'a>) {
199 self.commit();
200 self.text.lines.push(spans);
201 }
202
203 pub fn push_text(&mut self, text: Text<'a>) {
204 self.commit();
205 self.text.lines.extend(text.lines);
206 }
207
208 pub fn finish(mut self) -> Text<'a> {
209 self.commit();
210 self.text
211 }
212}