style_term/
styles.rs

1use std::fmt::{Display, Formatter};
2use std::num::ParseIntError;
3use std::str::FromStr;
4
5/// Use Styles instead
6pub(crate) static RESET: Style = Style(0);
7
8/// Default Style Types
9pub enum Styles {
10    Reset,
11    Bold,
12    Italic,
13    Underline,
14}
15
16impl From<Styles> for Style {
17    fn from(style: Styles) -> Self {
18        match style {
19            Styles::Reset => {
20                Style(0)
21            }
22            Styles::Bold => {
23                Style(1)
24            }
25            Styles::Italic => { Style(3) }
26            Styles::Underline => {
27                Style(4)
28            }
29        }
30    }
31}
32
33/// Refers to to a ANSI Style code
34/// Rendered VIA `\x1b[{u8}m`
35#[derive(Debug, Clone)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37pub struct Style(u8);
38
39impl Display for Style {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        write!(f, "\x1b[{}m", self.0)
42    }
43}
44
45impl From<u8> for Style {
46    fn from(v: u8) -> Self {
47        Style(v)
48    }
49}
50
51impl FromStr for Style {
52    type Err = ParseIntError;
53
54    fn from_str(s: &str) -> Result<Self, Self::Err> {
55        let result = u8::from_str(s)?;
56        Ok(Style(result))
57    }
58}