1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
pub mod console {
    use std::{io, thread, time};

    pub struct Console {}

    impl Console {
        pub fn new() -> Console {
            Console {}
        }

        pub fn print<T>(&self, message: T, settings: Settings)
        where
            T: std::string::ToString,
        {
            for c in message.to_string().chars() {
                print!(
                    "{}{}{}{}",
                    settings.color.get_text_code(),
                    settings.background_color.get_bg_code(),
                    c,
                    Color::Clear.get_text_code()
                );
                io::Write::flush(&mut std::io::stdout()).unwrap();
                thread::sleep(settings.speed);
            }
        }

        pub fn print_line<T>(&self, message: T, settings: Settings)
        where
            T: std::string::ToString,
        {
            for c in message.to_string().chars() {
                print!(
                    "{}{}{}{}",
                    settings.color.get_text_code(),
                    settings.background_color.get_bg_code(),
                    c,
                    Color::Clear.get_text_code()
                );
                io::Write::flush(&mut std::io::stdout()).unwrap();
                thread::sleep(settings.speed);
            }

            println!();
        }

        // gets input without new line from the console
        pub fn get_input(&self) -> String {
            let mut input = String::new();
            io::stdin()
                .read_line(&mut input)
                .expect("Error while getting input from operating system");
            input = String::from(input.trim());
            input
        }

        // gets input without new line from the console with custom message and settings
        pub fn get_custom_input<T>(&self, message: T, settings: Settings) -> String
        where
            T: std::string::ToString,
        {
            let mut input = String::new();
            self.print(message, settings);
            io::stdin()
                .read_line(&mut input)
                .expect("Error while getting input from operating system");
            input = String::from(input.trim());
            input
        }
    }

    pub enum Color {
        Clear,
        Black,
        Red,
        Green,
        Yellow,
        Blue,
        Magenta,
        Cyan,
        White,

        BrightBlack,
        BrightRed,
        BrightGreen,
        BrightYellow,
        BrightBlue,
        BrightMagenta,
        BrightCyan,
        BrightWhite,
    }

    impl Color {
        fn get_text_code(&self) -> &str {
            match self {
                Color::Clear => "\x1b[0m",
                Color::Black => "\x1b[30m",
                Color::Red => "\x1b[31m",
                Color::Green => "\x1b[32m",
                Color::Yellow => "\x1b[33m",
                Color::Blue => "\x1b[34m",
                Color::Magenta => "\x1b[35m",
                Color::Cyan => "\x1b[36m",
                Color::White => "\x1b[37m",

                Color::BrightBlack => "\x1b[90m",
                Color::BrightRed => "\x1b[91m",
                Color::BrightGreen => "\x1b[92m",
                Color::BrightYellow => "\x1b[93m",
                Color::BrightBlue => "\x1b[94m",
                Color::BrightMagenta => "\x1b[95m",
                Color::BrightCyan => "\x1b[96m",
                Color::BrightWhite => "\x1b[97m",
            }
        }

        fn get_bg_code(&self) -> &str {
            match self {
                Color::Clear => "",
                Color::Black => "\x1b[40m",
                Color::Red => "\x1b[41m",
                Color::Green => "\x1b[42m",
                Color::Yellow => "\x1b[43m",
                Color::Blue => "\x1b[44m",
                Color::Magenta => "\x1b[45m",
                Color::Cyan => "\x1b[46m",
                Color::White => "\x1b[47m",

                Color::BrightBlack => "\x1b[100m",
                Color::BrightRed => "\x1b[101m",
                Color::BrightGreen => "\x1b[102m",
                Color::BrightYellow => "\x1b[103m",
                Color::BrightBlue => "\x1b[104m",
                Color::BrightMagenta => "\x1b[105m",
                Color::BrightCyan => "\x1b[106m",
                Color::BrightWhite => "\x1b[107m",
            }
        }
    }

    pub struct Settings {
        color: Color,
        background_color: Color,
        speed: time::Duration,
    }

    impl Settings {
        // creates an instance of settings with default values
        pub fn new() -> Settings {
            Settings {
                color: Color::White,
                background_color: Color::Black,
                speed: time::Duration::from_millis(0),
            }
        }

        pub fn with_color(mut self, color: Color) -> Settings {
            self.color = color;
            self
        }

        pub fn with_background_color(mut self, color: Color) -> Settings {
            self.background_color = color;
            self
        }

        pub fn with_speed(mut self, speed: time::Duration) -> Settings {
            self.speed = speed;
            self
        }

        pub fn speed_very_slow(mut self) -> Settings {
            self.speed = time::Duration::from_millis(250);
            self
        }

        pub fn speed_slow(mut self) -> Settings {
            self.speed = time::Duration::from_millis(200);
            self
        }

        pub fn speed_medium(mut self) -> Settings {
            self.speed = time::Duration::from_millis(150);
            self
        }

        pub fn speed_fast(mut self) -> Settings {
            self.speed = time::Duration::from_millis(100);
            self
        }

        pub fn speed_very_fast(mut self) -> Settings {
            self.speed = time::Duration::from_millis(50);
            self
        }
    }
}