1use std::borrow::Cow;
2#[cfg(feature = "color")]
3use colored::Color;
4
5#[derive(Default, Copy, Clone)]
7pub enum TextAlign {
8 #[default]
10 Left,
11 Right,
13 Center,
15}
16
17#[derive(Default, Clone)]
19pub struct Text<'a> {
20 pub content: Cow<'a, str>,
22 pub style: TextStyle,
24}
25
26#[derive(Clone)]
28pub struct TextStyle {
29 pub align: TextAlign,
31 #[cfg(feature = "color")]
33 pub color: Color,
34}
35
36impl Default for TextStyle {
37 fn default() -> Self {
38 Self {
39 align: Default::default(),
40 #[cfg(feature = "color")]
41 color: Color::BrightWhite,
42 }
43 }
44}
45
46impl<'a> Text<'a> {
47 pub fn align(mut self, align: TextAlign) -> Self {
49 self.style.align = align;
50 self
51 }
52
53 #[cfg(feature = "color")]
55 pub fn color(mut self, color: Color) -> Self {
56 self.style.color = color;
57 self
58 }
59}
60
61impl From<String> for Text<'_> {
62 fn from(value: String) -> Self {
63 Text {
64 content: Cow::Owned(value),
65 ..Default::default()
66 }
67 }
68}
69
70impl<'a> From<&'a str> for Text<'a> {
71 fn from(value: &'a str) -> Self {
72 Text {
73 content: Cow::Borrowed(value),
74 ..Default::default()
75 }
76 }
77}
78
79impl<'a> From<&'a String> for Text<'a> {
80 fn from(value: &'a String) -> Self {
81 Text {
82 content: Cow::Borrowed(value),
83 ..Default::default()
84 }
85 }
86}
87
88impl<'a> From<Cow<'a, str>> for Text<'a> {
89 fn from(value: Cow<'a, str>) -> Self {
90 Text {
91 content: value,
92 ..Default::default()
93 }
94 }
95}