Skip to main content

dsd_util/
printer.rs

1const ANSI_RESET: &str = "\x1b[0m"; // ANSI reset code
2
3/// Color options for printing to the terminal
4#[derive(Debug, Clone, Copy)]
5pub enum Color {
6    Red,
7    Green,
8    Blue,
9    Yellow,
10    Magenta,
11    Cyan,
12    White,
13}
14
15/// Implement Color to match on proper ANSI code
16impl Color {
17    /// Get ANSI code for color
18    fn code(&self) -> &str {
19        match self {
20            Color::Red => "\x1b[1;31m",
21            Color::Green => "\x1b[1;32m",
22            Color::Blue => "\x1b[1;34m",
23            Color::Yellow => "\x1b[1;33m",
24            Color::Magenta => "\x1b[1;35m",
25            Color::Cyan => "\x1b[1;36m",
26            Color::White => "\x1b[1;37m",
27        }
28    }
29}
30
31/// Print line function that uses ANSI code to display colored text on terminal
32pub fn color_println(color: Color, text: &str) {
33    println!("{}{}{}", color.code(), text, ANSI_RESET);
34}
35
36/// Format string function that uses ANSI code to return string formatted for color
37pub fn color_println_fmt(color: Color, text: &str) -> String {
38    format!("{}{}{}", color.code(), text, ANSI_RESET)
39}