Skip to content

Commit

Permalink
🎨 Improve Color Print
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Aug 5, 2021
1 parent 692ac60 commit cf87e55
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
64 changes: 44 additions & 20 deletions sensor_Interface/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
use regex::Regex;

/// Define Text Colors
#[allow(dead_code)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
Reset,
}

/// Get Color as an Integer.
/// Using Ansi Color Codes.
#[rustfmt::skip]
fn get_color_code(color: Color) -> i32 {
match color {
Color::Black => 30,
Color::Red => 31,
Color::Green => 32,
Color::Yellow => 33,
Color::Blue => 34,
Color::Magenta => 35,
Color::Cyan => 36,
Color::White => 37,
Color::Reset => 0,
}
}

/// Return string with ANSI color codes
pub fn color(text: &str, color: i32) -> String {
["\x1B[0;", &color.to_string()[..], "m", text, "\x1B[0;0m"].join("")
pub fn color(text: &str, color: Color) -> String {
format!("\x1B[0;{}m{}\x1B[0;0m", get_color_code(color), text)
}

/// Return string with ANSI color codes for bold text
pub fn color_bold(text: &str, color: i32) -> String {
["\x1B[1;", &color.to_string()[..], "m", text, "\x1B[0;0m"].join("")
pub fn color_bold(text: &str, color: Color) -> String {
format!("\x1B[1;{}m{}\x1B[0m", get_color_code(color), text)
}

/// Removes ANSI color codes from text
pub fn remove_ansi(text: &str) -> String {
let re = Regex::new(r"\[[0-1];[0-9]+m").unwrap();
let re = Regex::new(r"\x1B\[[0-1];[0-9]+m").unwrap();
re.replace_all(text, "").to_string()
}

Expand All @@ -39,18 +70,6 @@ pub fn ret_if(cond: bool, ret: String) -> String {
"".to_string()
}

// COLORS
// ------------
// BLACK - 30
// RED - 31
// GREEN - 32
// YELLOW - 33
// BLUE - 34
// MAGENTA - 35
// CYAN - 36
// WHITE - 37
// RESET - 0

#[allow(dead_code)]
/// Get the type of a value
pub fn get_type<T>(_: &T) -> String {
Expand All @@ -62,12 +81,12 @@ mod tests {
use super::*;
#[test]
fn test_color_1() {
assert_eq!(color("Hello", 32), "\x1B[0;32mHello\x1B[0;0m");
assert_eq!(color("Hello", Color::Green), "\x1B[0;32mHello\x1B[0;0m");
}

#[test]
fn test_color_bold_1() {
assert_eq!(color_bold("Hello", 32), "\x1B[1;32mHello\x1B[0;0m")
assert_eq!(color_bold("Hello", Color::Green), "\x1B[1;32mHello\x1B[0m")
}

#[test]
Expand All @@ -83,11 +102,16 @@ mod tests {
#[test]
fn test_remove_ansi_1() {
assert_eq!(remove_ansi("\x1B[0;32mHello\x1B[0;0m"), "Hello");
assert_eq!(remove_ansi(&color("Nose", 36)), "Nose");
assert_eq!(remove_ansi(&color("Nose", Color::Cyan)), "Nose");
}

#[test]
fn test_ret_if_1() {
assert_eq!(ret_if(true, "Hello".to_string()), "Hello".to_string());
}

#[test]
fn test_ret_if_2() {
assert_eq!(ret_if(false, "Hello".to_string()), "".to_string());
}
}
28 changes: 15 additions & 13 deletions sensor_Interface/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use simple_config_parser::config::Config;
use std::env;
use std::thread;

use common::Color;

mod common;
mod logging;
mod sensor;
Expand Down Expand Up @@ -41,7 +43,7 @@ fn main() {

// If there are no sensors, panic
if sensors.is_empty() {
println!("{}", common::color("[-] No Sensors defined :/", 31));
println!("{}", common::color("[-] No Sensors defined :/", Color::Red));
panic!("No Sensors defined :/");
}

Expand All @@ -50,10 +52,10 @@ fn main() {
&event_log_cfg,
format!(
"{} {} {} {}",
common::color_bold("[*] Starting Sensor Interface", 32),
common::color(&format!("[v{}]", VERSION)[..], 34),
common::ret_if(logging, common::color_bold("LOGGING", 36)),
common::ret_if(debug, common::color_bold("DEBUG", 31))
common::color_bold("[*] Starting Sensor Interface", Color::Green),
common::color(&format!("[v{}]", VERSION)[..], Color::Blue),
common::ret_if(logging, common::color_bold("LOGGING", Color::Cyan)),
common::ret_if(debug, common::color_bold("DEBUG", Color::Red))
),
);

Expand All @@ -66,28 +68,28 @@ fn main() {
&event_log_cfg,
format!(
"{} {} {}",
common::color("[*] Found Devices:", 32),
common::color(&sensors.len().to_string(), 34),
common::color(&format!("[{}]", sensor_names), 34),
common::color("[*] Found Devices:", Color::Green),
common::color(&sensors.len().to_string(), Color::Blue),
common::color(&format!("[{}]", sensor_names), Color::Blue),
),
);

logging::log_event(
&event_log_cfg,
format!(
"{} {} {}",
common::color("[*] Main Device ID:", 32),
common::color(&sensors[0].id[..], 34),
common::color(&format!("[{}]", &sensors[0].name[..]), 34),
common::color("[*] Main Device ID:", Color::Green),
common::color(&sensors[0].id[..], Color::Blue),
common::color(&format!("[{}]", &sensors[0].name[..]), Color::Blue),
),
);

logging::log_event(
&event_log_cfg,
format!(
"{}{}",
common::color("[*] Serving on: ", 32),
common::color(&format!("{}:{}", ip, port)[..], 36)
common::color("[*] Serving on: ", Color::Green),
common::color(&format!("{}:{}", ip, port)[..], Color::Cyan)
),
);

Expand Down
5 changes: 4 additions & 1 deletion sensor_Interface/src/sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ impl Sensor {
let sensor_data = get_sensor_data(&self.id[..]);
let temp: Vec<&str> = sensor_data.split("t=").collect();
if temp.len() != 2 {
println!("{}", common::color("[-] Error Parsing Sensor Data :/", 31));
println!(
"{}",
common::color("[-] Error Parsing Sensor Data :/", common::Color::Red)
);
return None;
}
let temp_str = temp[1].to_string();
Expand Down
2 changes: 1 addition & 1 deletion sensor_Interface/src/server/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn all(req: &tiny_http::Request, event_log_cfg: &logging::LogCfg) {
&event_log_cfg,
common::color(
&format!("[+] {:?}: \"{}\"", req.method(), req.url())[..],
32,
common::Color::Green,
),
);
}
Expand Down

0 comments on commit cf87e55

Please sign in to comment.