1use crate::style::Style;
7use std::borrow::Cow;
8use unicode_segmentation::UnicodeSegmentation;
9use unicode_width::UnicodeWidthStr;
10
11#[derive(Debug, Clone, PartialEq)]
13pub struct Span {
14 pub text: Cow<'static, str>,
16 pub style: Style,
18 pub link: Option<String>,
20}
21
22impl Span {
23 pub fn raw<S: Into<Cow<'static, str>>>(text: S) -> Self {
25 Span {
26 text: text.into(),
27 style: Style::new(),
28 link: None,
29 }
30 }
31
32 pub fn styled<S: Into<Cow<'static, str>>>(text: S, style: Style) -> Self {
34 Span {
35 text: text.into(),
36 style,
37 link: None,
38 }
39 }
40
41 pub fn linked<S: Into<Cow<'static, str>>>(text: S, style: Style, url: String) -> Self {
43 Span {
44 text: text.into(),
45 style,
46 link: Some(url),
47 }
48 }
49
50 pub fn width(&self) -> usize {
52 UnicodeWidthStr::width(self.text.as_ref())
53 }
54
55 pub fn is_empty(&self) -> bool {
57 self.text.is_empty()
58 }
59}
60
61impl<S: Into<Cow<'static, str>>> From<S> for Span {
62 fn from(text: S) -> Self {
63 Span::raw(text)
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
69pub enum Alignment {
70 #[default]
72 Left,
73 Center,
75 Right,
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
81pub enum Overflow {
82 #[default]
84 Wrap,
85 Ellipsis,
87 Truncate,
89 Visible,
91}
92
93#[derive(Debug, Clone, Default)]
95pub struct Text {
96 pub spans: Vec<Span>,
98 pub alignment: Alignment,
100 pub overflow: Overflow,
102 pub style: Style,
104}
105
106impl Text {
107 pub fn new() -> Self {
109 Text::default()
110 }
111
112 pub fn plain<S: Into<Cow<'static, str>>>(text: S) -> Self {
114 Text {
115 spans: vec![Span::raw(text)],
116 ..Default::default()
117 }
118 }
119
120 pub fn styled<S: Into<Cow<'static, str>>>(text: S, style: Style) -> Self {
122 Text {
123 spans: vec![Span::styled(text, style)],
124 style,
125 ..Default::default()
126 }
127 }
128
129 pub fn from_spans<I: IntoIterator<Item = Span>>(spans: I) -> Self {
131 Text {
132 spans: spans.into_iter().collect(),
133 ..Default::default()
134 }
135 }
136
137 pub fn push_span(&mut self, span: Span) {
139 self.spans.push(span);
140 }
141
142 pub fn push<S: Into<Cow<'static, str>>>(&mut self, text: S) {
144 self.spans.push(Span::raw(text));
145 }
146
147 pub fn push_styled<S: Into<Cow<'static, str>>>(&mut self, text: S, style: Style) {
149 self.spans.push(Span::styled(text, style));
150 }
151
152 pub fn alignment(mut self, alignment: Alignment) -> Self {
154 self.alignment = alignment;
155 self
156 }
157
158 pub fn overflow(mut self, overflow: Overflow) -> Self {
160 self.overflow = overflow;
161 self
162 }
163
164 pub fn style(mut self, style: Style) -> Self {
166 self.style = style;
167 self
168 }
169
170 pub fn width(&self) -> usize {
172 self.spans.iter().map(|s| s.width()).sum()
173 }
174
175 pub fn plain_text(&self) -> String {
177 self.spans.iter().map(|s| s.text.as_ref()).collect()
178 }
179
180 pub fn is_empty(&self) -> bool {
182 self.spans.is_empty() || self.spans.iter().all(|s| s.is_empty())
183 }
184
185 pub fn wrap(&self, width: usize) -> Vec<Vec<Span>> {
187 if width == 0 {
188 return vec![];
189 }
190
191 match self.overflow {
192 Overflow::Visible => vec![self.spans.clone()],
193 Overflow::Truncate | Overflow::Ellipsis => {
194 vec![self.truncate_spans(width, self.overflow == Overflow::Ellipsis)]
195 }
196 Overflow::Wrap => self.wrap_spans(width),
197 }
198 }
199
200 fn truncate_spans(&self, width: usize, ellipsis: bool) -> Vec<Span> {
201 let mut result = Vec::new();
202 let mut remaining_width = if ellipsis {
203 width.saturating_sub(1)
204 } else {
205 width
206 };
207
208 for span in &self.spans {
209 if remaining_width == 0 {
210 break;
211 }
212
213 let span_width = span.width();
214 if span_width <= remaining_width {
215 result.push(span.clone());
216 remaining_width -= span_width;
217 } else {
218 let truncated = truncate_str(&span.text, remaining_width);
220 result.push(Span::styled(truncated.to_string(), span.style));
221 remaining_width = 0;
222 }
223 }
224
225 if ellipsis && self.width() > width {
226 result.push(Span::raw("…"));
227 }
228
229 result
230 }
231
232 fn wrap_spans(&self, max_width: usize) -> Vec<Vec<Span>> {
233 let mut lines: Vec<Vec<Span>> = Vec::new();
234 let mut current_line: Vec<Span> = Vec::new();
235 let mut current_width = 0;
236
237 for span in &self.spans {
238 let words = split_into_words(&span.text);
239
240 for (word, trailing_space) in words {
241 let word_width = UnicodeWidthStr::width(word);
242 let space_width = if trailing_space { 1 } else { 0 };
243 let total_width = word_width + space_width;
244
245 if current_width + word_width <= max_width {
247 let text = if trailing_space {
248 format!("{word} ")
249 } else {
250 word.to_string()
251 };
252 current_line.push(Span::styled(text, span.style));
253 current_width += total_width;
254 } else if word_width > max_width {
255 if !current_line.is_empty() {
257 lines.push(std::mem::take(&mut current_line));
258 current_width = 0;
259 }
260
261 let broken = break_word(word, max_width);
263 for (i, part) in broken.iter().enumerate() {
264 if i > 0 {
265 lines.push(std::mem::take(&mut current_line));
266 }
267 current_line.push(Span::styled(part.to_string(), span.style));
268 current_width = UnicodeWidthStr::width(part.as_str());
269 }
270
271 if trailing_space && current_width < max_width {
272 current_line.push(Span::styled(" ", span.style));
273 current_width += 1;
274 }
275 } else {
276 if !current_line.is_empty() {
278 lines.push(std::mem::take(&mut current_line));
279 }
280 let text = if trailing_space {
281 format!("{word} ")
282 } else {
283 word.to_string()
284 };
285 current_line.push(Span::styled(text, span.style));
286 current_width = total_width;
287 }
288 }
289 }
290
291 if !current_line.is_empty() {
292 lines.push(current_line);
293 }
294
295 if lines.is_empty() {
296 lines.push(Vec::new());
297 }
298
299 lines
300 }
301
302 pub fn align_line(&self, line: Vec<Span>, width: usize) -> Vec<Span> {
304 let line_width: usize = line.iter().map(|s| s.width()).sum();
305
306 if line_width >= width {
307 return line;
308 }
309
310 let padding = width - line_width;
311
312 match self.alignment {
313 Alignment::Left => {
314 line
317 }
318 Alignment::Right => {
319 let mut result = vec![Span::raw(" ".repeat(padding))];
320 result.extend(line);
321 result
322 }
323 Alignment::Center => {
324 let left_pad = padding / 2;
325 let right_pad = padding - left_pad;
326 let mut result = vec![Span::raw(" ".repeat(left_pad))];
327 result.extend(line);
328 result.push(Span::raw(" ".repeat(right_pad)));
329 result
330 }
331 }
332 }
333}
334
335impl<S: Into<Cow<'static, str>>> From<S> for Text {
336 fn from(text: S) -> Self {
337 Text::plain(text)
338 }
339}
340
341fn truncate_str(s: &str, max_width: usize) -> &str {
343 let mut width = 0;
344 let mut end = 0;
345
346 for grapheme in s.graphemes(true) {
347 let grapheme_width = UnicodeWidthStr::width(grapheme);
348 if width + grapheme_width > max_width {
349 break;
350 }
351 width += grapheme_width;
352 end += grapheme.len();
353 }
354
355 &s[..end]
356}
357
358fn split_into_words(s: &str) -> Vec<(&str, bool)> {
360 let mut words = Vec::new();
361 let mut word_start = None;
362 let mut leading_spaces = 0;
363
364 for (i, c) in s.char_indices() {
366 if c.is_whitespace() {
367 leading_spaces = i + c.len_utf8();
368 } else {
369 break;
370 }
371 }
372
373 let chars_to_process = if leading_spaces > 0 {
376 &s[leading_spaces..]
377 } else {
378 s
379 };
380
381 for (i, c) in chars_to_process.char_indices() {
382 if c.is_whitespace() {
383 if let Some(start) = word_start {
384 let word = &chars_to_process[start..i];
385 let final_word = if start == 0 && leading_spaces > 0 {
387 word
389 } else {
390 word
391 };
392 words.push((final_word, true));
393 word_start = None;
394 }
395 } else if word_start.is_none() {
396 word_start = Some(i);
397 }
398 }
399
400 if let Some(start) = word_start {
401 words.push((&chars_to_process[start..], false));
402 }
403
404 if leading_spaces > 0 && !words.is_empty() {
407 let mut result = vec![(&s[..leading_spaces], false)];
413 result.extend(words);
414 return result;
415 } else if leading_spaces > 0 && words.is_empty() {
416 return vec![(s, false)];
418 }
419
420 words
421}
422
423fn break_word(word: &str, max_width: usize) -> Vec<String> {
425 let mut parts = Vec::new();
426 let mut current = String::new();
427 let mut current_width = 0;
428
429 for grapheme in word.graphemes(true) {
430 let grapheme_width = UnicodeWidthStr::width(grapheme);
431
432 if current_width + grapheme_width > max_width && !current.is_empty() {
433 parts.push(std::mem::take(&mut current));
434 current_width = 0;
435 }
436
437 current.push_str(grapheme);
438 current_width += grapheme_width;
439 }
440
441 if !current.is_empty() {
442 parts.push(current);
443 }
444
445 parts
446}
447
448#[cfg(test)]
449mod tests {
450 use super::*;
451
452 #[test]
453 fn test_span_width() {
454 assert_eq!(Span::raw("hello").width(), 5);
455 assert_eq!(Span::raw("你好").width(), 4); assert_eq!(Span::raw("").width(), 0);
457 }
458
459 #[test]
460 fn test_text_plain() {
461 let text = Text::plain("Hello, World!");
462 assert_eq!(text.plain_text(), "Hello, World!");
463 assert_eq!(text.width(), 13);
464 }
465
466 #[test]
467 fn test_text_wrap_simple() {
468 let text = Text::plain("hello world");
469 let lines = text.wrap(6);
470 assert_eq!(lines.len(), 2);
471 assert_eq!(lines[0][0].text, "hello ");
472 assert_eq!(lines[1][0].text, "world");
473 }
474
475 #[test]
476 fn test_text_wrap_long_word() {
477 let text = Text::plain("supercalifragilistic");
478 let lines = text.wrap(10);
479 assert!(lines.len() > 1);
480 }
481
482 #[test]
483 fn test_truncate_ellipsis() {
484 let text = Text::plain("Hello, World!").overflow(Overflow::Ellipsis);
485 let lines = text.wrap(8);
486 let plain: String = lines[0].iter().map(|s| s.text.as_ref()).collect();
487 assert!(plain.ends_with('…'));
488 assert!(UnicodeWidthStr::width(plain.as_str()) <= 8);
490 }
491
492 #[test]
493 fn test_alignment_left() {
494 let text = Text::plain("hi").alignment(Alignment::Left);
495 let lines = text.wrap(10);
496 let aligned = text.align_line(lines[0].clone(), 10);
497 let plain: String = aligned.iter().map(|s| s.text.as_ref()).collect();
498 assert_eq!(plain, "hi");
500 }
501
502 #[test]
503 fn test_alignment_right() {
504 let text = Text::plain("hi").alignment(Alignment::Right);
505 let lines = text.wrap(10);
506 let aligned = text.align_line(lines[0].clone(), 10);
507 let plain: String = aligned.iter().map(|s| s.text.as_ref()).collect();
508 assert_eq!(plain, " hi");
509 }
510
511 #[test]
512 fn test_alignment_center() {
513 let text = Text::plain("hi").alignment(Alignment::Center);
514 let lines = text.wrap(10);
515 let aligned = text.align_line(lines[0].clone(), 10);
516 let plain: String = aligned.iter().map(|s| s.text.as_ref()).collect();
517 assert_eq!(plain, " hi ");
518 }
519}