btui/
ft.rs

1/// module containing enums to represent terminal effects
2pub mod effects {
3    /// enum to represent any color
4    pub enum Color {
5        Black,
6        Red,
7        Green,
8        Yellow,
9        Blue,
10        Magenta,
11        Cyan,
12        White,
13        /// set color to an rgb color
14        RGB(u8, u8, u8),
15    }
16
17    /// enum to represent special printings other than colors
18    pub enum Special {
19        Bold,
20        Underline,
21        Stroke,
22        Italic,
23        /// reset all graphical effects
24        Reset,
25    }
26}
27
28/// module for giving printable escape sequences
29pub mod print {
30    use crate::effects::Color;
31    use crate::effects::Special;
32
33    /// change foreground color
34    ///
35    /// # Arguments
36    /// *`col`: color to use
37    ///
38    /// # Returns
39    /// the color code to be printed to the terminal
40    ///
41    /// # Example
42    /// ```rust
43    /// use btui::print::fg;
44    /// use btui::effects::Color;
45    /// println!("{}test", fg(Color::Red));
46    /// ```
47    pub fn fg(col: Color) -> String {
48        match col {
49            Color::Black => String::from("\x1b[30m"),
50            Color::Red => String::from("\x1b[31m"),
51            Color::Green => String::from("\x1b[32m"),
52            Color::Yellow => String::from("\x1b[33m"),
53            Color::Blue => String::from("\x1b[34m"),
54            Color::Magenta => String::from("\x1b[35m"),
55            Color::Cyan => String::from("\x1b[36m"),
56            Color::White => String::from("\x1b[37m"),
57            Color::RGB(r, g, b) => format!("\x1b[38;2;{};{};{}m", r, g, b),
58        }
59    }
60
61    /// change background color
62    ///
63    /// # Arguments
64    /// *`col`: color to use
65    ///
66    /// # Returns
67    /// the color code to be printed to the terminal
68    ///
69    /// # Example
70    /// ```rust
71    /// use btui::print::bg;
72    /// use btui::effects::Color;
73    /// println!("{}test", bg(Color::Red));
74    /// ```
75    pub fn bg(col: Color) -> String {
76        match col {
77            Color::Black => String::from("\x1b[40m"),
78            Color::Red => String::from("\x1b[41m"),
79            Color::Green => String::from("\x1b[42m"),
80            Color::Yellow => String::from("\x1b[43m"),
81            Color::Blue => String::from("\x1b[44m"),
82            Color::Magenta => String::from("\x1b[45m"),
83            Color::Cyan => String::from("\x1b[46m"),
84            Color::White => String::from("\x1b[47m"),
85            Color::RGB(r, g, b) => format!("\x1b[48;2;{};{};{}m", r, g, b),
86        }
87    }
88
89    /// apply a special effect
90    ///
91    /// # Arguments
92    /// *`special`: effect to apply
93    ///
94    /// # Returns
95    /// a string containing the escape sequence to use
96    ///
97    /// # Example
98    /// ```rust
99    /// use btui::effects::Special;
100    /// use btui::print::sp;
101    /// println!("{}test{}", sp(Special::Underline), sp(Special::Reset));
102    /// ```
103    pub fn sp(special: Special) -> String {
104        match special {
105            Special::Bold => String::from("\x1b[1m"),
106            Special::Underline => String::from("\x1b[4m"),
107            Special::Stroke => String::from("\x1b[9m"),
108            Special::Italic => String::from("\x1b[3m"),
109            Special::Reset => String::from("\x1b[0m"),
110        }
111    }
112}