From 1f0f66cefed43285b2e7d95723d6ed9f0a7e8484 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 11 Aug 2020 20:47:03 +0100 Subject: [PATCH] added colour --- src/colour.rs | 63 ++++++++++++++++++++++++++ src/main.rs | 3 ++ src/work.rs | 123 ++++++++++++++++++++++++++------------------------ 3 files changed, 130 insertions(+), 59 deletions(-) create mode 100644 src/colour.rs diff --git a/src/colour.rs b/src/colour.rs new file mode 100644 index 0000000..3a6ee3e --- /dev/null +++ b/src/colour.rs @@ -0,0 +1,63 @@ +use super::*; + +use std::{ + fmt::Display, +}; +#[cfg(feature="colour")] +#[macro_export] macro_rules! colour { + ($things:path) => { + { + #[allow(unused_imports)] + use recolored::{ + Color, + }; + + $things + } + } +} +#[cfg(not(feature="colour"))] +#[macro_export] macro_rules! colour { + ($things:path) => { + () + } +} + +#[cfg(not(feature="colour"))] +/// Dummy enum +pub enum Color { + Black, + Red, + Green, + Yellow, + Blue, + Magenta, + Cyan, + White, + BrightBlack, + BrightRed, + BrightGreen, + BrightYellow, + BrightBlue, + BrightMagenta, + BrightCyan, + BrightWhite, + Palette(u8), + True(u8, u8, u8), +} + +#[cfg(feature="colour")] pub type Colour = recolored::Color; +#[cfg(not(feature="colour"))] pub type Colour = (); + +#[inline] pub fn style(_col: Colour, from: impl Display) -> impl Display +{ + cfg_if! { + if #[cfg(feature="colour")] { + use recolored::Colorize; + let string = format!("{}", from); + string.color(_col) + } else { + from + } + } +} diff --git a/src/main.rs b/src/main.rs index ec77f11..6da2225 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ pub use error::{ErrorExt as _, ResultExt as _}; mod stage; mod leanify; +mod colour; mod dir; mod fixed_stack; mod process; @@ -32,6 +33,8 @@ mod work; async fn work() -> Result<(), Box> { + + //println!("umm {}", colour::style(colour!(Color::Blue), "hiii")); let args = arg::parse_args().await.with_prefix("failed to parse args")?; let leanify = leanify::find_binary().with_prefix("Couldn't find leanify binary")?; diff --git a/src/work.rs b/src/work.rs index f5f80b6..8d8f867 100644 --- a/src/work.rs +++ b/src/work.rs @@ -59,12 +59,12 @@ async fn do_work(process: impl AsRef, file: impl AsRef, mut prog: P if err { cfg_if!{ if #[cfg(feature="progress")] { - let value = format!("[!] {:?}: {}", file, value); + let value = format!("[{}] {:?}: {}", colour::style(colour!(Color::BrightYellow), "!"), file, colour::style(colour!(Color::Yellow),value)); if let Err(_) = prog.eprintln(&value[..]).await { eprintln!("\n{}", value); } } else { - eprintln!("[!] {:?}: {}", file, value); + eprintln!("[{}] {:?}: {}", colour::style(colour!(Color::BrightYellow), "!"), file, colour::style(colour!(Color::Yellow),value)); } } } else { @@ -127,67 +127,72 @@ where I: IntoIterator, }) }; let mut i=0usize; - let results = join_all(files - .map(|filename| { - let semaphore = semaphore.clone(); - let process = Arc::clone(&process); - let mut tx = tx.clone(); - - #[cfg(feature="progress")] let mut progress = progress.clone(); - - (tokio::spawn(async move { - #[cfg(feature="progress")] type Opt = OptionFuture; - #[cfg(not(feature="progress"))] type Opt = std::marker::PhantomData; - let _task_id: Opt<_> = { - let _lock = maybe_await(semaphore.map(|x| x.acquire_owned())).await; + let results = + join_all( + files + .map(|filename| { + let semaphore = semaphore.clone(); + let process = Arc::clone(&process); + let mut tx = tx.clone(); + + #[cfg(feature="progress")] let mut progress = progress.clone(); + + (tokio::spawn(async move { + #[cfg(feature="progress")] type Opt = OptionFuture; + #[cfg(not(feature="progress"))] type Opt = std::marker::PhantomData; + let _task_id: Opt<_> = { + let _lock = maybe_await(semaphore.map(|x| x.acquire_owned())).await; - // (task_id, worker_result) - let worker = { - cfg_if!{ - if #[cfg(feature="progress")] { - let worker = do_work(process.as_ref(), &filename, progress.clone()); - let task = progress.add_task(format!("{:?}", filename.as_ref())); - future::join(task, worker).await - } else { - #[cfg(nightly)] type NoReturn = !; - #[cfg(not(nightly))] type NoReturn = (); - (Option::::None, do_work(process.as_ref(), &filename, ()).await) - } - } - }; + // (task_id, worker_result) + let worker = { + cfg_if!{ + if #[cfg(feature="progress")] { + let worker = do_work(process.as_ref(), &filename, progress.clone()); + let task = progress.add_task(format!("{:?}", filename.as_ref())); + future::join(task, worker).await + } else { + #[cfg(nightly)] type NoReturn = !; + #[cfg(not(nightly))] type NoReturn = (); + (Option::::None, do_work(process.as_ref(), &filename, ()).await) + } + } + }; - match worker.1 { - Ok(strings) => tx.send((filename, strings, i)).await.map_err(|_| "Child panic").unwrap(), - Err(error) => { - #[cfg(not(feature="progress"))] eprintln!("[!] {:?}: {}", filename.as_ref(), error); - #[cfg(feature="progress")] let _ = progress.eprintln(format!("[!] ({}) {:?}: {}", i, filename.as_ref(), error)).await.or_else(|e| {eprintln!("\n{}",e); Err(e)}); - }, - } - cfg_if!{ - if #[cfg(feature="progress")] { - worker.0.ok().into() - } else { - cfg_if!{ - if #[cfg(nightly)] { - std::marker::PhantomData:: - } else { - std::marker::PhantomData::<()> - } - } - } + match worker.1 { + Ok(strings) => tx.send((filename, strings, i)).await.map_err(|_| "Child panic").unwrap(), + Err(error) => { + #[cfg(not(feature="progress"))] eprintln!("[{}] {:?}: {}", colour::style(colour!(Color::Yellow),"!"), filename.as_ref(), colour::style(colour!(Color::Yellow), error)); + #[cfg(feature="progress")] let _ = progress.eprintln(format!("[{}] ({}) {:?}: {}", colour::style(colour!(Color::Yellow),"!"),i, filename.as_ref(), colour::style(colour!(Color::Yellow), error))).await + .or_else(|e| { + eprintln!("\n{}",e); Err(e) + }); + }, + } + cfg_if!{ + if #[cfg(feature="progress")] { + worker.0.ok().into() + } else { + cfg_if!{ + if #[cfg(nightly)] { + std::marker::PhantomData:: + } else { + std::marker::PhantomData::<()> } - }; - #[cfg(feature="progress")] - if let Some(Ok(id)) = _task_id.await { - let mut builder = progress.builder(); - builder.bump_min(1); - builder.remove_task(id); - let _ = builder.send().await; - } else { - let _ = progress.bump_min(1).await; } - }),i+=1).0 - })).await + } + } + }; + #[cfg(feature="progress")] + if let Some(Ok(id)) = _task_id.await { + let mut builder = progress.builder(); + builder.bump_min(1); + builder.remove_task(id); + let _ = builder.send().await; + } else { + let _ = progress.bump_min(1).await; + } + }),i+=1).0 + })).await .into_iter() .filter_map(|x| x.err());