src/message/printer.rs
157 lines · 4081 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_width::UnicodeWidthStr;
7
8use crate::util::{space_span, take_width};
9
10pub struct TextPrinter<'a> {
11 text: Text<'a>,
12 width: usize,
13 base_style: Style,
14 hide_reply: bool,
15
16 alignment: Alignment,
17 curr_spans: Vec<Span<'a>>,
18 curr_width: usize,
19}
20
21impl<'a> TextPrinter<'a> {
22 pub fn new(width: usize, base_style: Style, hide_reply: bool) -> Self {
23 TextPrinter {
24 text: Text::default(),
25 width,
26 base_style,
27 hide_reply,
28
29 alignment: Alignment::Left,
30 curr_spans: vec![],
31 curr_width: 0,
32 }
33 }
34
35 pub fn align(mut self, alignment: Alignment) -> Self {
36 self.alignment = alignment;
37 self
38 }
39
40 pub fn hide_reply(&self) -> bool {
41 self.hide_reply
42 }
43
44 pub fn width(&self) -> usize {
45 self.width
46 }
47
48 pub fn sub(&self, indent: usize) -> Self {
49 TextPrinter {
50 text: Text::default(),
51 width: self.width.saturating_sub(indent),
52 base_style: self.base_style,
53 hide_reply: self.hide_reply,
54
55 alignment: self.alignment,
56 curr_spans: vec![],
57 curr_width: 0,
58 }
59 }
60
61 fn remaining(&self) -> usize {
62 self.width - self.curr_width
63 }
64
65 pub fn commit(&mut self) {
66 if self.curr_width > 0 {
67 self.push_break();
68 }
69 }
70
71 fn push(&mut self) {
72 self.curr_width = 0;
73 self.text.lines.push(Spans(std::mem::take(&mut self.curr_spans)));
74 }
75
76 pub fn push_break(&mut self) {
77 if self.curr_width == 0 && self.text.lines.is_empty() {
78 // Disallow leading breaks.
79 return;
80 }
81
82 let remaining = self.remaining();
83
84 if remaining > 0 {
85 match self.alignment {
86 Alignment::Left => {
87 let tspan = space_span(remaining, self.base_style);
88 self.curr_spans.push(tspan);
89 },
90 Alignment::Center => {
91 let trailing = remaining / 2;
92 let leading = remaining - trailing;
93
94 let tspan = space_span(trailing, self.base_style);
95 let lspan = space_span(leading, self.base_style);
96
97 self.curr_spans.push(tspan);
98 self.curr_spans.insert(0, lspan);
99 },
100 Alignment::Right => {
101 let lspan = space_span(remaining, self.base_style);
102 self.curr_spans.insert(0, lspan);
103 },
104 }
105 }
106
107 self.push();
108 }
109
110 pub fn push_str<T>(&mut self, s: T, style: Style)
111 where
112 T: Into<Cow<'a, str>>,
113 {
114 let style = self.base_style.patch(style);
115 let mut cow = s.into();
116
117 loop {
118 let sw = UnicodeWidthStr::width(cow.as_ref());
119
120 if self.curr_width + sw <= self.width {
121 // The text fits within the current line.
122 self.curr_spans.push(Span::styled(cow, style));
123 self.curr_width += sw;
124 break;
125 }
126
127 // Take a leading portion of the text that fits in the line.
128 let ((s0, w), s1) = take_width(cow, self.remaining());
129 cow = s1;
130
131 self.curr_spans.push(Span::styled(s0, style));
132 self.curr_width += w;
133
134 self.commit();
135 }
136
137 if self.curr_width == self.width {
138 // If the last bit fills the full line, start a new one.
139 self.push();
140 }
141 }
142
143 pub fn push_line(&mut self, spans: Spans<'a>) {
144 self.commit();
145 self.text.lines.push(spans);
146 }
147
148 pub fn push_text(&mut self, text: Text<'a>) {
149 self.commit();
150 self.text.lines.extend(text.lines);
151 }
152
153 pub fn finish(mut self) -> Text<'a> {
154 self.commit();
155 self.text
156 }
157}