a better coloring crate
add comma color syntax
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/cfstr.rs | 37 | ||||
| -rw-r--r-- | src/lib.rs | 19 |
3 files changed, 39 insertions, 19 deletions
@@ -1,6 +1,6 @@ [package] name = "comat" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = ["bendn <[email protected]>"] license = "MIT" diff --git a/src/cfstr.rs b/src/cfstr.rs index c42fe9f..898da31 100644 --- a/src/cfstr.rs +++ b/src/cfstr.rs @@ -61,7 +61,7 @@ pub struct CFStr(String); impl Parse for CFStr { fn parse(stream: syn::parse::ParseStream) -> Result<Self> { let input = stream.parse::<LitStr>()?.value(); - let mut chars = input.chars().peekable(); + let mut chars = input.chars(); let mut temp = String::new(); let mut out = String::new(); while let Some(ch) = chars.next() { @@ -80,26 +80,37 @@ impl Parse for CFStr { Some(ch) => temp.push(ch), None => return Err(stream.error("unexpected eof")), } - for ch in chars.by_ref() { + 'outer: for ch in chars.by_ref() { match ch { '}' => { if let Some(a) = name2ansi(&temp) { out.push_str(a); temp.clear(); break; - } else if let Some((b, a)) = temp.split_once(':') { - if let Some(ansi) = name2ansi(a) { - if a != "reset" { - out.push_str(name2ansi("reset").unwrap()); - } - out.push_str(ansi); - out.push('{'); - out.push_str(b); - out.push('}'); + } else if let Some((b, a)) = temp + .split_once(':') + .map(|(a, b)| (a.to_string(), b.to_string())) + { + if a != "reset" { out.push_str(name2ansi("reset").unwrap()); - temp.clear(); - break; } + for a in a.split(',') { + if let Some(ansi) = name2ansi(a) { + out.push_str(ansi); + } else { + out.push('{'); + out.push_str(&temp); + out.push('}'); + temp.clear(); + break 'outer; + } + } + out.push('{'); + out.push_str(&b); + out.push('}'); + temp.clear(); + out.push_str(name2ansi("reset").unwrap()); + break; } out.push('{'); out.push_str(&temp); @@ -12,7 +12,7 @@ //! cprintln!("the traffic light will be {green}green{reset} at {:?}.", Instant::now() + Duration::from_secs(40)); //! ``` //! -//! ## why you should use comat instead of {yansi, owo_colors, colored, ..} +//! ## why you should use comat instead of {`yansi`, `owo_colors`, `colored`, ..} //! //! - no method pollution, your intellisense remains fine //! - compact: shorter than even raw ansi. see: @@ -27,18 +27,20 @@ //! print!("\x1b[0;34;31m{thing}\x1b[0m."); //! ``` //! vs -//! ```ignore +//! ``` +//! # #[cfg(doc)] //! print!("{}.", thing.red()); //! ``` //! - intuitive: you dont have to -//! ```ignore -//! println!("{} {} {}", thing1.red().on_blue(), thing2.red().on_blue(), thing3.italic());. +//! ``` +//! # #[cfg(doc)] +//! println!("{} {} {}", thing1.red().on_blue(), thing2.red().on_blue(), thing3.italic().yellow()); //! ``` //! instead, simply //! ``` //! # use comat::cprintln; //! # let thing1 = 0; let thing2 = 5; let thing3 = 4; -//! cprintln!("{red}{on_blue}{thing1} {thing2} {thing3:italic}"); +//! cprintln!("{red}{on_blue}{thing1} {thing2} {thing3:italic,yellow}"); //! ``` //! //! ## syntax @@ -50,6 +52,13 @@ //! if the color inside a `{}` is not found, it doesnt touch the block, for convenience. //! //! `{thing:color}` will reset everything before the block, color it, and reset that color. similar to `thing.color()` with other libs. +//! it can also contain more than one color: `{thing:yelow,italic,on_red}` +//! +//! ## colors +//! +//! `black` `red` `green` `yellow` `blue` `magenta` `cyan` `white` `default` `bold_black` `bold_red` `bold_green` `bold_yellow` `bold_blue` `bold_magenta` `bold_cyan` `bold_white` +//! `bold_default` `on_black_bold` `on_red_bold` `on_green_bold` `on_yellow_bold` `on_blue_bold` `on_magenta_bold` `on_cyan_bold` `on_white_bold` `on_default_bold` `on_black` `on_red` +//! `on_green` `on_yellow` `on_blue` `on_magenta` `on_cyan` `on_white` `on_default` `reset` `dim` `italic` `underline` `blinking` `hide` `strike` `bold` #![forbid(unsafe_code)] #![warn(clippy::pedantic, clippy::dbg_macro, missing_docs)] use proc_macro::TokenStream; |