ColorShell/
lib.rs

1use std::ops::Add;
2use std::io;
3use std::io::Write;
4
5pub struct Color {
6    pub r: u8,
7    pub g: u8,
8    pub b: u8
9}
10
11pub struct Weight {
12    pub id: i8,
13}
14
15impl Color {
16    pub const BLACK: Color = Color { r: 0, g: 0, b: 0 };
17    pub const RED: Color = Color { r: 255, g: 78, b: 78 };
18    pub const ORANGE: Color = Color { r: 255, g: 158, b: 86 };
19    pub const YELLOW: Color = Color { r: 255, g: 240, b: 86 };
20    pub const LIGHTGREEN: Color = Color { r: 102, g: 255, b: 124 };
21    pub const DARKGREEN: Color = Color { r: 27, g: 141, b: 43 };
22    pub const MINT: Color = Color { r: 65, g: 255, b: 160 };
23    pub const CYAN: Color = Color { r: 74, g: 255, b: 252 };
24    pub const LIGHTBLUE: Color = Color { r: 88, g: 221, b: 255 };
25    pub const SKYBLUE: Color = Color { r: 0, g: 169, b: 255 };
26    pub const BLUE: Color = Color { r: 0, g: 91, b: 255 };
27    pub const DARKBLUE: Color = Color { r: 0, g: 31, b: 255 };
28    pub const DEEPPURPLE: Color = Color { r: 78, g: 0, b: 255 };
29    pub const PURPLE: Color = Color { r: 123, g: 0, b: 255 };
30    pub const VIOLET: Color = Color { r: 172, g: 108, b: 255 };
31    pub const MAGENTA: Color = Color { r: 213, g: 0, b: 255 };
32    pub const WARMPINK: Color = Color { r: 255, g: 0, b: 255 };
33    pub const WATERMELON: Color = Color { r: 255, g: 113, b: 166 };
34    pub const LIGHTGRAY: Color = Color { r: 153, g: 153, b: 153 };
35    pub const DARKGRAY: Color = Color { r: 91, g: 91, b: 91 };
36}
37
38impl Weight {
39    pub const BOLD: Weight = Weight {id: 1};
40    pub const DIM: Weight = Weight {id: 2};
41    pub const ITALIC: Weight = Weight {id: 3};
42    pub const UNDERLINE: Weight = Weight {id: 4};
43    pub const SLOWBLINK: Weight = Weight {id: 5};
44    pub const FASTBLINK: Weight = Weight {id: 6};
45}
46
47impl Add<&str> for Color {
48    type Output = String;
49
50    fn add(self, rhs: &str) -> Self::Output{
51        format!("\x1B[38;2;{};{};{}m{}{}", self.r, self.g, self.b, rhs, "\x1B[0m")
52    }
53}
54
55impl Add<&str> for Weight {
56    type Output = String;
57
58    fn add(self, rhs: &str) -> Self::Output {
59        return format!("\x1B[{}m{}\x1B[0m", self.id, rhs);
60    }
61}
62
63pub fn color(text: &'static str, clr: Color) -> String{
64    return clr + text;
65}
66
67pub fn weigh(text: &'static str, wht: Weight) -> String{
68    return wht + text;
69}
70
71pub fn input_color(iptext: &'static str, clr: Color) -> String {
72    let mut ip = String::new();
73    print!("{}\x1B[38;2;{};{};{}m", iptext, clr.r, clr.g, clr.b);
74    io::stdout().flush().expect("Failed to flush stdout"); // Ensure the prompt is displayed before input
75    io::stdin().read_line(&mut ip).expect("Error getting input");
76    print!("\x1B[0m");
77    return ip.trim_end().to_owned();
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn it_works() {
86        println!("{}", color("red", Color {255, 255, 255}));
87    }
88}