diff --git a/examples/logger.rs b/examples/logger.rs index b4bf79d..f3c9728 100644 --- a/examples/logger.rs +++ b/examples/logger.rs @@ -1,6 +1,4 @@ -use log::Level; -use lool::logger::ConsoleLogger; - +use {log::Level, lool::logger::ConsoleLogger}; fn main() { ConsoleLogger::default_setup(Level::Trace, "test").unwrap(); @@ -9,4 +7,4 @@ fn main() { log::error!("log line"); log::debug!("log line"); log::trace!("log line"); -} \ No newline at end of file +} diff --git a/examples/stylizer.rs b/examples/stylizer.rs index 49747a6..6b721bc 100644 --- a/examples/stylizer.rs +++ b/examples/stylizer.rs @@ -1,5 +1,7 @@ -use eyre::{set_hook, DefaultHandler, Result}; -use lool::cli::stylize::{stylize, Stylize}; +use { + eyre::{set_hook, DefaultHandler, Result}, + lool::cli::stylize::{stylize, Stylize}, +}; fn setup_eyre() { let _ = set_hook(Box::new(DefaultHandler::default_with)); @@ -12,7 +14,10 @@ fn main() -> Result<()> { let alt_red_bold = stylize(stylize("alt [red+bold]", "red"), "+bold"); let red_bold_italic = stylize("[red+bold|italic]", "red+bold|italic"); - let alt_red_bold_italic = stylize(stylize(stylize("alt [red+bold|italic]", "red"), "+bold"), "+italic"); + let alt_red_bold_italic = stylize( + stylize(stylize("alt [red+bold|italic]", "red"), "+bold"), + "+italic", + ); let red_on_blue = stylize("[white on blue]", "white on blue"); let rgb = stylize("[#3a95ef]", "#3a95ef"); @@ -31,13 +36,16 @@ fn main() -> Result<()> { println!("pre {} post", "[green]".stl("green").stl("+bold")); println!("pre {} post", "[green+bold]".stl("green+bold")); - + println!("pre {} post", "[.blue()]".blue()); println!("pre {} post", "[.blue().bold()]".blue().bold()); - println!("pre {} post", "[.blue().on_red().bold()]".blue().on_red().bold()); + println!( + "pre {} post", + "[.blue().on_red().bold()]".blue().on_red().bold() + ); println!("pre {} post", "[.dim()]".dim()); println!("pre {} post", "[.blue().dim()]".blue().dim()); Ok(()) -} \ No newline at end of file +} diff --git a/lib/cli/stylize.rs b/lib/cli/stylize.rs index a230d48..0010d03 100644 --- a/lib/cli/stylize.rs +++ b/lib/cli/stylize.rs @@ -4,4 +4,4 @@ mod stylizer; pub use stylizer::{stylize, Stylize}; #[cfg(test)] -mod tests; \ No newline at end of file +mod tests; diff --git a/lib/cli/stylize/style.rs b/lib/cli/stylize/style.rs index 00216d0..71410b9 100644 --- a/lib/cli/stylize/style.rs +++ b/lib/cli/stylize/style.rs @@ -1,6 +1,4 @@ -use bitflags::bitflags; -use eyre::Context; -use std::borrow::Cow; +use {bitflags::bitflags, eyre::Context, std::borrow::Cow}; /// The 8 standard colors. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/lib/cli/stylize/stylizer.rs b/lib/cli/stylize/stylizer.rs index 1652aff..a6f9317 100644 --- a/lib/cli/stylize/stylizer.rs +++ b/lib/cli/stylize/stylizer.rs @@ -1,10 +1,14 @@ -use super::style::{Color, StyleAttributes as StyleBitflags}; -use eyre::Result; +use { + super::style::{Color, StyleAttributes as StyleBitflags}, + eyre::Result, +}; pub mod instructions { - use super::{Result, StyleBitflags, Color}; - use bitflags::parser::{from_str, ParseError}; - + use { + super::{Color, Result, StyleBitflags}, + bitflags::parser::{from_str, ParseError}, + }; + pub struct StyledString { pub fg: Option, pub bg: Option, @@ -27,8 +31,9 @@ pub mod instructions { if instructions.starts_with("+") { // only attributes - styled_string.attrs = attributes_from_str(instructions.trim_start_matches('+')).map_err(|e| eyre::eyre!(e))?; - return Ok(styled_string); + styled_string.attrs = attributes_from_str(instructions.trim_start_matches('+')) + .map_err(|e| eyre::eyre!(e))?; + return Ok(styled_string); } // try to separate the colors from the attributes @@ -71,20 +76,23 @@ pub mod instructions { } 2 => { // Both fg and bg colors are provided - Ok((Some(Color::from_str(colors[0])?), Some(Color::from_str(colors[1])?))) + Ok(( + Some(Color::from_str(colors[0])?), + Some(Color::from_str(colors[1])?), + )) } _ => Err(eyre::eyre!("Invalid color instruction: {}", instruction)), } } - fn attributes_from_str (s: &str) -> Result { - from_str(s.to_uppercase().as_str()) + fn attributes_from_str(s: &str) -> Result { + from_str(s.to_uppercase().as_str()) } } /// 🧉 » Stylize Trait /// -- -/// +/// /// `Trait` that extends `String` and `str` with the ability to stylize them /// with ANSI color and attrs, using methods that return a new string with the given style. pub trait Stylize { @@ -149,9 +157,9 @@ impl Stylize for String { /// 🧉 » stylize fn /// -- -/// +/// /// Stylizes a string with optional ANSI color and attributes. -/// +/// pub fn stylize>(s: S, instructions: &str) -> String { let styled_string = instructions::parse(instructions); diff --git a/lib/cli/stylize/tests/mod.rs b/lib/cli/stylize/tests/mod.rs index 3527187..3fac3d8 100644 --- a/lib/cli/stylize/tests/mod.rs +++ b/lib/cli/stylize/tests/mod.rs @@ -1 +1 @@ -mod stylizo; \ No newline at end of file +mod stylizo; diff --git a/lib/cli/stylize/tests/stylizo.rs b/lib/cli/stylize/tests/stylizo.rs index 07cacf2..679f448 100644 --- a/lib/cli/stylize/tests/stylizo.rs +++ b/lib/cli/stylize/tests/stylizo.rs @@ -2,10 +2,11 @@ mod parse_instruction { use { crate::cli::stylize::{ - stylizer::instructions::{parse, parse_color_instruction}, style::{Color, StyleAttributes} - }, eyre::{set_hook, DefaultHandler} + style::{Color, StyleAttributes}, + stylizer::instructions::{parse, parse_color_instruction}, + }, + eyre::{set_hook, DefaultHandler}, }; - fn setup_eyre() { let _ = set_hook(Box::new(DefaultHandler::default_with)); @@ -16,11 +17,29 @@ mod parse_instruction { setup_eyre(); assert_eq!(parse_color_instruction("red")?, (Some(Color::Red), None)); - assert_eq!(parse_color_instruction("#FF0000")?, (Some(Color::TrueColor { r: 255, g: 0, b: 0 }), None)); - assert_eq!(parse_color_instruction("on blue")?, (None, Some(Color::Blue))); - assert_eq!(parse_color_instruction("on #0000FF")?, (None, Some(Color::TrueColor { r: 0, g: 0, b: 255 }))); - assert_eq!(parse_color_instruction("red on blue")?, (Some(Color::Red), Some(Color::Blue))); - assert_eq!(parse_color_instruction("#FF0000 on #0000FF")?, (Some(Color::TrueColor { r: 255, g: 0, b: 0 }), Some(Color::TrueColor { r: 0, g: 0, b: 255 }))); + assert_eq!( + parse_color_instruction("#FF0000")?, + (Some(Color::TrueColor { r: 255, g: 0, b: 0 }), None) + ); + assert_eq!( + parse_color_instruction("on blue")?, + (None, Some(Color::Blue)) + ); + assert_eq!( + parse_color_instruction("on #0000FF")?, + (None, Some(Color::TrueColor { r: 0, g: 0, b: 255 })) + ); + assert_eq!( + parse_color_instruction("red on blue")?, + (Some(Color::Red), Some(Color::Blue)) + ); + assert_eq!( + parse_color_instruction("#FF0000 on #0000FF")?, + ( + Some(Color::TrueColor { r: 255, g: 0, b: 0 }), + Some(Color::TrueColor { r: 0, g: 0, b: 255 }) + ) + ); assert!(parse_color_instruction("red on blue on green").is_err()); assert!(parse_color_instruction("red on").is_err()); assert!(parse_color_instruction("on").is_err()); @@ -34,7 +53,6 @@ mod parse_instruction { #[test] fn test_parse_instructions() -> eyre::Result<()> { - setup_eyre(); let styled_string = parse("red on blue")?; @@ -54,8 +72,14 @@ mod parse_instruction { // ##RRGGBB let styled_string = parse("#FF0000 on #0000FF")?; - assert_eq!(styled_string.fg, Some(Color::TrueColor { r: 255, g: 0, b: 0 })); - assert_eq!(styled_string.bg, Some(Color::TrueColor { r: 0, g: 0, b: 255 })); + assert_eq!( + styled_string.fg, + Some(Color::TrueColor { r: 255, g: 0, b: 0 }) + ); + assert_eq!( + styled_string.bg, + Some(Color::TrueColor { r: 0, g: 0, b: 255 }) + ); assert_eq!(styled_string.attrs, StyleAttributes::empty()); let styled_string = parse("red on blue+bold")?; @@ -65,19 +89,34 @@ mod parse_instruction { // ##RRGGBB let styled_string = parse("#FF0000 on #0000FF+bold")?; - assert_eq!(styled_string.fg, Some(Color::TrueColor { r: 255, g: 0, b: 0 })); - assert_eq!(styled_string.bg, Some(Color::TrueColor { r: 0, g: 0, b: 255 })); + assert_eq!( + styled_string.fg, + Some(Color::TrueColor { r: 255, g: 0, b: 0 }) + ); + assert_eq!( + styled_string.bg, + Some(Color::TrueColor { r: 0, g: 0, b: 255 }) + ); assert_eq!(styled_string.attrs, StyleAttributes::BOLD); let styled_string = parse("red on blue+bold|underline")?; assert_eq!(styled_string.fg, Some(Color::Red)); assert_eq!(styled_string.bg, Some(Color::Blue)); - assert_eq!(styled_string.attrs, StyleAttributes::BOLD | StyleAttributes::UNDERLINE); + assert_eq!( + styled_string.attrs, + StyleAttributes::BOLD | StyleAttributes::UNDERLINE + ); let styled_string = parse("red on #0000FF+bold|underline|italic")?; assert_eq!(styled_string.fg, Some(Color::Red)); - assert_eq!(styled_string.bg, Some(Color::TrueColor { r: 0, g: 0, b: 255 })); - assert_eq!(styled_string.attrs, StyleAttributes::BOLD | StyleAttributes::UNDERLINE | StyleAttributes::ITALIC); + assert_eq!( + styled_string.bg, + Some(Color::TrueColor { r: 0, g: 0, b: 255 }) + ); + assert_eq!( + styled_string.attrs, + StyleAttributes::BOLD | StyleAttributes::UNDERLINE | StyleAttributes::ITALIC + ); let styled_string = parse("+bold")?; assert_eq!(styled_string.fg, None); @@ -100,4 +139,4 @@ mod parse_instruction { Ok(()) } -} \ No newline at end of file +} diff --git a/lib/lib.rs b/lib/lib.rs index 5c673f6..9b90196 100644 --- a/lib/lib.rs +++ b/lib/lib.rs @@ -1,2 +1,2 @@ pub mod cli; -pub mod logger; \ No newline at end of file +pub mod logger; diff --git a/lib/logger/datetime.rs b/lib/logger/datetime.rs index b0d2346..f72f41f 100644 --- a/lib/logger/datetime.rs +++ b/lib/logger/datetime.rs @@ -33,7 +33,15 @@ pub fn utc_current_time() -> String { let minutes = (secs / 60) % 60; let seconds = secs % 60; - format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}", years, month + 1, day, hours, minutes, seconds) + format!( + "{:04}-{:02}-{:02} {:02}:{:02}:{:02}", + years, + month + 1, + day, + hours, + minutes, + seconds + ) } Err(_) => "0".to_string(), } @@ -57,4 +65,3 @@ fn days_to_date(days: u64, leap: bool) -> (u64, u64) { (month, days_left + 1) // Adding 1 to day to make it 1-based } - diff --git a/lib/logger/mod.rs b/lib/logger/mod.rs index 6d470dd..4bf5451 100644 --- a/lib/logger/mod.rs +++ b/lib/logger/mod.rs @@ -1,7 +1,9 @@ pub mod datetime; -use eyre::{eyre, Result}; -use log::{Level, Metadata, Record}; +use { + eyre::{eyre, Result}, + log::{Level, Metadata, Record}, +}; const RESET: &str = "\x1b[0m"; @@ -17,8 +19,8 @@ impl StyledRecord { fn from(record: &Record, get_time: fn() -> String) -> Self { let ansi_style_level = match record.level() { Level::Error => "\x1b[31m", // red - Level::Warn => "\x1b[33m", // yellow - Level::Info => "\x1b[32m", // green + Level::Warn => "\x1b[33m", // yellow + Level::Info => "\x1b[32m", // green Level::Debug => "\x1b[95m", // magenta Level::Trace => "\x1b[34m", // blue }; @@ -34,28 +36,36 @@ impl StyledRecord { Self { level: format!("{}{:<5}{}", ansi_style_level, record.level(), RESET), message: format!("{}", record.args()), - file: format!("{}{}{}", file_ansi_color, record.file().unwrap_or("unknown"), RESET), + file: format!( + "{}{}{}", + file_ansi_color, + record.file().unwrap_or("unknown"), + RESET + ), line: format!("{}{}{}", line_ansi_color, record.line().unwrap_or(0), RESET), time, } } } -pub struct ConsoleLogger<'a>{ +pub struct ConsoleLogger<'a> { context: &'a str, time_fn: fn() -> String, } impl<'a> ConsoleLogger<'a> { pub fn default_setup(max_level: Level, context: &'static str) -> Result<()> { - let logger = Box::new(ConsoleLogger { context, time_fn: datetime::utc_current_time}); + let logger = Box::new(ConsoleLogger { + context, + time_fn: datetime::utc_current_time, + }); log::set_logger(Box::leak(logger) as &'static ConsoleLogger) .map(|()| log::set_max_level(max_level.to_level_filter())) .map_err(|err| eyre!("failed to set logger: {}", err)) } pub fn setup(max_level: Level, context: &'static str, time_fn: fn() -> String) -> Result<()> { - let logger = Box::new(ConsoleLogger { context, time_fn}); + let logger = Box::new(ConsoleLogger { context, time_fn }); log::set_logger(Box::leak(logger) as &'static ConsoleLogger) .map(|()| log::set_max_level(max_level.to_level_filter())) .map_err(|err| eyre!("failed to set logger: {}", err))