rusty_ci/helper/
stdio.rs

1#![macro_use]
2use std::io::{stdin, stdout, Write};
3
4/// This function prompts the user with a message and returns the user's input.
5/// It also pops off trailing carriage returns.
6pub fn input<S: ToString>(prompt: S) -> String {
7    let mut buf = String::new();
8    print!("{}", prompt.to_string());
9    let _ = stdout().flush();
10
11    stdin()
12        .read_line(&mut buf)
13        .expect("Could not get user input");
14
15    while let Some('\n') = buf.chars().next_back() {
16        buf.pop();
17    }
18
19    while let Some('\r') = buf.chars().next_back() {
20        buf.pop();
21    }
22
23    buf
24}
25
26/// Used to prompt the user with a yes or no question.
27/// If they answer with Y or y, this function returns true.
28pub fn yes_or_no<S: ToString>(prompt: S) -> bool {
29    let response = input(prompt);
30
31    response.to_lowercase().trim() == "y"
32}
33
34/// This prints a format string with a specific color.
35/// The color must be one of the following.
36/// - Black
37/// - Blue
38/// - Green
39/// - Red
40/// - Cyan
41/// - Magenta
42/// - Yellow
43/// - White
44#[macro_export]
45macro_rules! color_print {
46    ($color:ident, $fmt:expr $(,$e:expr)*) => {{
47        // I know this implementation is ugly as hell,
48        // the thing is: this code doesnt really matter fam
49        use std::io::Write;
50        use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
51
52        let bufwtr = BufferWriter::stderr(ColorChoice::Always);
53        let mut buffer = bufwtr.buffer();
54        match buffer.set_color(ColorSpec::new().set_fg(Some(Color::$color))) {_=>{}};
55        match write!(&mut buffer, $fmt $(,$e)*) {_=>{}};
56        match bufwtr.print(&buffer) {_=>{}};
57
58        // Reset color
59        let mut reset_buf = BufferWriter::stderr(ColorChoice::Always).buffer();
60        match reset_buf.reset() {_=>{}};
61        match bufwtr.print(&reset_buf) {_=>{}};
62    }};
63}
64
65/// Write green text to the console, and then reset color
66#[macro_export]
67macro_rules! green {
68    ($fmt:expr $(,$e:expr)*) => {
69        color_print!(Green, $fmt $(,$e)*);
70    };
71}
72
73/// Write red text to the console, and then reset color
74#[macro_export]
75macro_rules! red {
76    ($fmt:expr $(,$e:expr)*) => {
77        color_print!(Red, $fmt $(,$e)*);
78    };
79}
80
81/// Write blue text to the console, and then reset color
82#[macro_export]
83macro_rules! blue {
84    ($fmt:expr $(,$e:expr)*) => {
85        color_print!(Blue, $fmt $(,$e)*);
86    };
87}
88
89/// Write yellow text to the console, and then reset color
90#[macro_export]
91macro_rules! yellow {
92    ($fmt:expr $(,$e:expr)*) => {
93        color_print!(Yellow, $fmt $(,$e)*);
94    };
95}
96
97/// Flush stdout
98#[macro_export]
99macro_rules! flush {
100    () => {{
101        use std::io::stdout;
102        use std::io::Write;
103        match stdout().flush() {
104            _ => {}
105        };
106    }};
107}
108
109/// Prints info message colored green
110#[macro_export]
111macro_rules! info {
112    ($fmt:expr $(,$e:expr)*) => {
113        let user = format!($fmt $(, $e)*);
114        print!("==[");
115        flush!();
116        green!("INFO{}", "");
117        print!("]===> {}\n", user);
118    };
119}
120
121/// Prints debug message colored blue
122#[macro_export]
123macro_rules! debug {
124    ($fmt:expr $(,$e:expr)*) => {
125        let user = format!($fmt $(, $e)*);
126        print!("==[");
127        flush!();
128        blue!("DEBUG");
129        print!("]==> {}\n", user);
130    };
131}
132
133/// Prints error message colored red
134#[macro_export]
135macro_rules! error {
136    ($fmt:expr $(,$e:expr)*) => {
137        let user = format!($fmt $(, $e)*);
138        print!("==[");
139        flush!();
140        red!("ERROR");
141        print!("]==> {}\n", user);
142    };
143}
144
145/// Prints warning message colored yellow
146#[macro_export]
147macro_rules! warn {
148    ($fmt:expr $(,$e:expr)*) => {
149        let user = format!($fmt $(, $e)*);
150        print!("==[");
151        flush!();
152        yellow!("WARN");
153        print!("]===> {}\n", user);
154    };
155}