terminal_banner/
text.rs

1use std::borrow::Cow;
2#[cfg(feature = "color")]
3use colored::Color;
4
5/// Variants for text alignment.
6#[derive(Default, Copy, Clone)]
7pub enum TextAlign {
8    /// Text align left
9    #[default]
10    Left,
11    /// Text align right
12    Right,
13    /// Text align center
14    Center,
15}
16
17/// Text content.
18#[derive(Default, Clone)]
19pub struct Text<'a> {
20    /// Content for the text.
21    pub content: Cow<'a, str>,
22    /// Styling information.
23    pub style: TextStyle,
24}
25
26/// Text style.
27#[derive(Clone)]
28pub struct TextStyle {
29    /// Alignment.
30    pub align: TextAlign,
31    /// Color information.
32    #[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    /// Set the text alignment.
48    pub fn align(mut self, align: TextAlign) -> Self {
49        self.style.align = align;
50        self
51    }
52    
53    /// Set the text color.
54    #[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}