efcl/lib.rs
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
//! EFCL - Efficiency First Color Library
//!
//! The most simple, small, and fast terminal color text library.
//!
//! ```
//! use efcl::{color, Color, bold};
//!
//! fn main() {
//! println!("Hello, {}!", color!(Color::BLUE, "world"));
//!
//! println!("{}!", bold!(&color!(Color::RED, "EFCL").to_string()));
//! }
//! ```
/// Color is an enum that represents the ANSI escape code for terminal colors
#[derive(Copy, Clone)]
pub enum Color {
BLACK = 30,
RED = 31,
GREEN = 32,
YELLOW = 33,
BLUE = 34,
PURPLE = 35,
CYAN = 36,
LIGHTGRAY = 37,
}
impl Color {
pub fn as_str(&self) -> &'static str {
match self {
Color::BLACK => "30",
Color::RED => "31",
Color::GREEN => "32",
Color::YELLOW => "33",
Color::BLUE => "34",
Color::PURPLE => "35",
Color::CYAN => "36",
Color::LIGHTGRAY => "37",
}
}
}
/// Return a String with a Color added to it
///
/// # Examples
///
/// ```
/// use efcl::{color, Color};
///
/// let red_text: String = color!(Color::RED, "This is RED!!");
/// ```
#[macro_export]
macro_rules! color {
($a:expr, $b:expr) => {{
"\x1B[".to_owned() + $a.as_str() + "m" + $b + "\x1B[0m"
}};
}
/// Return a String that is bold
///
/// # Examples
///
/// ```
/// use efcl::bold;
///
/// let bold_text: String = bold!("This is BOLD!!");
/// ```
#[macro_export]
macro_rules! bold {
($a:expr) => {{
"\x1B[1m".to_owned() + $a + "\x1B[0m"
}};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_macro_test() {
let c: String = color!(Color::RED, "This is RED!!");
assert_eq!(c, "\x1B[31mThis is RED!!\x1B[0m");
}
#[test]
fn bold_macro_test() {
let bold_text: String = bold!("This is BOLD!!");
assert_eq!(bold_text, "\x1B[1mThis is BOLD!!\x1B[0m");
assert_eq!(
bold!(&color!(Color::RED, "Hello")),
"\u{1b}[1m\u{1b}[31mHello\u{1b}[0m\u{1b}[0m"
);
}
}