Skip to main content

chroma_print/
lib.rs

1//! > **A lightweight utility for styled terminal printing using ANSI escape codes.**
2//!
3//! ## Install globally
4//! ```console
5//! $ cargo install chroma-print
6//! ```
7//!
8//! ## Install as a library
9//! ```console
10//! $ cargo add chroma-print
11//! ```
12//!
13//! ## Usage
14//! ```rust
15//! use chroma_print::{ChromaPrint, print_error, print_info, print_success, print_warn};
16//!
17//! fn main() {
18//!     // Using the provided macros for convenient styled printing:
19//!     print_success!("This is a success message!");
20//!     print_info!("This is an info message!");
21//!     print_warn!("This is a warning message!");
22//!     print_error!("This is an error message!");
23//!     
24//!     // Alternatively, you can use the ChromaPrint struct directly:
25//!     println!("{}", ChromaPrint::success("Success!"));
26//!     println!("{}", ChromaPrint::info("Info!"));
27//!     println!("{}", ChromaPrint::warn("Warning!"));
28//!     eprintln!("{}", ChromaPrint::error("Error!"));
29//! }   
30//! ```
31
32pub mod colors;
33pub mod printer;
34
35pub use colors::Color;
36pub use printer::ChromaPrint;
37
38// Macros
39#[macro_export]
40/// Macro for printing a success message (green)
41macro_rules! print_success {
42    ($($arg:tt)*) => {
43        println!("{}", $crate::ChromaPrint::success(&format!($($arg)*)))
44    };
45}
46
47#[macro_export]
48/// Macro for printing an info message (cyan)
49macro_rules! print_info {
50    ($($arg:tt)*) => {
51        println!("{}", $crate::ChromaPrint::info(&format!($($arg)*)))
52    };
53}
54
55#[macro_export]
56/// Macro for printing a warning message (yellow)
57macro_rules! print_warn {
58    ($($arg:tt)*) => {
59        println!("{}", $crate::ChromaPrint::warn(&format!($($arg)*)))
60    };
61}
62
63#[macro_export]
64/// Macro for printing an error message (red)
65macro_rules! print_error {
66    ($($arg:tt)*) => {
67        eprintln!("{}", $crate::ChromaPrint::error(&format!($($arg)*)))
68    };
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    const RESET: &str = "\x1b[0m";
76
77    #[test]
78    fn test_color_values() {
79        assert_eq!(Color::Green.value(), "\x1b[32m");
80        assert_eq!(Color::Yellow.value(), "\x1b[33m");
81        assert_eq!(Color::Cyan.value(), "\x1b[36m");
82        assert_eq!(Color::Red.value(), "\x1b[31m");
83        assert_eq!(Color::Reset.value(), "\x1b[0m");
84    }
85
86    #[test]
87    fn test_success_formatting() {
88        let msg: &str = "success";
89        assert_eq!(
90            ChromaPrint::success(msg),
91            format!("\x1b[32msuccess{}", RESET)
92        );
93    }
94
95    #[test]
96    fn test_error_formatting() {
97        let msg: &str = "error";
98        assert_eq!(ChromaPrint::error(msg), format!("\x1b[31merror{}", RESET));
99    }
100
101    #[test]
102    fn test_info_formatting() {
103        let msg = "info";
104        assert_eq!(ChromaPrint::info(msg), format!("\x1b[36minfo{}", RESET));
105    }
106
107    #[test]
108    fn test_warn_formatting() {
109        let msg: &str = "warning";
110        assert_eq!(ChromaPrint::warn(msg), format!("\x1b[33mwarning{}", RESET));
111    }
112}