coloroid 0.7.0

A simple library for coloring your terminal
Documentation
pub mod colors {
    use core::fmt;

    pub struct ColoredString<'a> {
        text_to_color: &'a str,
        colors: Vec<String>,

        // Not related to user text...
        all_colors: Vec<&'a str>,
        all_background_colors: Vec<&'a str>,
    }

    impl<'a> ColoredString<'a> {
        pub fn new(text_to_color: &'a str) -> ColoredString<'a> {
            ColoredString {
                text_to_color,
                colors: vec![],
                all_colors: vec![
                    "0;30", "0;31", "0;32", "0;33", "0;34", "0;35", "0;36", "0;37",
                ],
                all_background_colors: vec![
                    "0;40", "0;41", "0;42", "0;43", "0;44", "0;45", "0;46", "0;47",
                ],
            }
        }

        /// Sets the color of the **text** based off of a index. This index refers to a color
        /// in a vector of colors, either custom or default. If you set not custom colors
        /// the list of colors is: black (0), red (1), green (2), yellow (3), blue (4),
        /// purple (5), cyan (6), white (7).

        pub fn text_from_index(&mut self, index: usize) -> &mut Self {
            match self.all_colors.iter().nth(index) {
                Some(color_code) => {
                    self.colors.push(color_code.to_string());
                }
                None => {
                    self.colors.push("0;37".to_string());
                }
            }

            self
        }

        /// Sets the color of the **background** based off of a index. This index refers to a color
        /// in a vector of colors, either custom or default. If you set not custom colors
        /// the list of colors is: black (0), red (1), green (2), yellow (3), blue (4),
        /// purple (5), cyan (6), white (7).

        pub fn bg_from_index(&mut self, index: usize) -> &mut Self {
            match self.all_background_colors.iter().nth(index) {
                Some(color_code) => {
                    self.colors.push(color_code.to_string());
                }
                None => {
                    self.colors.push("0;37".to_string());
                }
            }

            self
        }

        /// Sets the list of custom **text** colors. If you set custom colors, you must call
        /// `text_from_index()` **after** you use this method to use custom colors, otherwise
        /// you will use the preset colors (see `text_from_index` for the default colors).
        pub fn set_custom_text(&mut self, colors: Vec<&'static str>) -> &mut Self {
            self.all_colors = colors;
            self
        }

        /// Sets the list of custom **background** colors. If you set custom colors, you must call
        /// `bg_from_index()` **after** you use this method to use custom colors, otherwise
        /// you will use the preset colors (see `bg_from_index` for the default colors).
        pub fn set_custom_bg(&mut self, colors: Vec<&'static str>) -> &mut Self {
            self.all_background_colors = colors;
            self
        }

        /* Below is normal colors, not underlines or backgrounds */

        /// Turns the text black
        pub fn black(&mut self) -> &mut Self {
            self.colors.push(String::from("0;30"));

            self
        }

        /// Turns the text red
        pub fn red(&mut self) -> &mut Self {
            self.colors.push(String::from("0;31"));

            self
        }

        /// Turns the text green
        pub fn green(&mut self) -> &mut Self {
            self.colors.push(String::from("0;32"));

            self
        }

        /// Turns the text yellow
        pub fn yellow(&mut self) -> &mut Self {
            self.colors.push(String::from("0;33"));

            self
        }

        /// Turns the text blue
        pub fn blue(&mut self) -> &mut Self {
            self.colors.push(String::from("0;34"));

            self
        }

        /// Turns the text purple
        pub fn purple(&mut self) -> &mut Self {
            self.colors.push(String::from("0;35"));

            self
        }

        /// Turns the text cyan
        pub fn cyan(&mut self) -> &mut Self {
            self.colors.push(String::from("0;36"));

            self
        }

        /// Turns the text white
        pub fn white(&mut self) -> &mut Self {
            self.colors.push(String::from("0;37"));

            self
        }

        /* Below is all background related colors */

        /// Turns the background black
        pub fn bg_black(&mut self) -> &mut Self {
            self.colors.push(String::from("40"));
            self
        }
        /// Turns the background red
        pub fn bg_red(&mut self) -> &mut Self {
            self.colors.push(String::from("41"));

            self
        }
        /// Turns the background green
        pub fn bg_green(&mut self) -> &mut Self {
            self.colors.push(String::from("42"));

            self
        }
        /// Turns the background yellow
        pub fn bg_yellow(&mut self) -> &mut Self {
            self.colors.push(String::from("43"));

            self
        }
        /// Turns the background blue
        pub fn bg_blue(&mut self) -> &mut Self {
            self.colors.push(String::from("44"));

            self
        }
        /// Turns the background purple
        pub fn bg_purple(&mut self) -> &mut Self {
            self.colors.push(String::from("45"));

            self
        }
        /// Turns the background cyan
        pub fn bg_cyan(&mut self) -> &mut Self {
            self.colors.push(String::from("46"));

            self
        }
        /// Turns the background white
        pub fn bg_white(&mut self) -> &mut Self {
            self.colors.push(String::from("47"));

            self
        }
    }

    impl fmt::Display for ColoredString<'_> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let colors = self.colors.join(";");
            let string = format!("{}m{}", colors, self.text_to_color).to_string();
            let finish_stringed = format!("\u{001b}[{}\u{001b}[0m", string).to_string();

            write!(f, "{}", finish_stringed)
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::colors::ColoredString;

    #[test]
    fn please_work() {
        println!("{}", ColoredString::new("text?").bg_from_index(5));
        println!("Some more text :)");
    }

    #[test]
    fn lifetimes_are_dumb() {
        let mut text = ColoredString::new("ewe?");
        text.bg_from_index(5);
        
        println!("{}", text);
        println!("Some more text :)");
    }
}