added colour

master
Avril 4 years ago
parent bcc5daa5e0
commit 1f0f66cefe
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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
}
}
}

@ -21,6 +21,7 @@ pub use error::{ErrorExt as _, ResultExt as _};
mod stage; mod stage;
mod leanify; mod leanify;
mod colour;
mod dir; mod dir;
mod fixed_stack; mod fixed_stack;
mod process; mod process;
@ -32,6 +33,8 @@ mod work;
async fn work() -> Result<(), Box<dyn std::error::Error>> async fn work() -> Result<(), Box<dyn std::error::Error>>
{ {
//println!("umm {}", colour::style(colour!(Color::Blue), "hiii"));
let args = arg::parse_args().await.with_prefix("failed to parse args")?; let args = arg::parse_args().await.with_prefix("failed to parse args")?;
let leanify = leanify::find_binary().with_prefix("Couldn't find leanify binary")?; let leanify = leanify::find_binary().with_prefix("Couldn't find leanify binary")?;

@ -59,12 +59,12 @@ async fn do_work(process: impl AsRef<Path>, file: impl AsRef<OsStr>, mut prog: P
if err { if err {
cfg_if!{ cfg_if!{
if #[cfg(feature="progress")] { 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 { if let Err(_) = prog.eprintln(&value[..]).await {
eprintln!("\n{}", value); eprintln!("\n{}", value);
} }
} else { } else {
eprintln!("[!] {:?}: {}", file, value); eprintln!("[{}] {:?}: {}", colour::style(colour!(Color::BrightYellow), "!"), file, colour::style(colour!(Color::Yellow),value));
} }
} }
} else { } else {
@ -127,67 +127,72 @@ where I: IntoIterator<Item=T>,
}) })
}; };
let mut i=0usize; let mut i=0usize;
let results = join_all(files let results =
.map(|filename| { join_all(
let semaphore = semaphore.clone(); files
let process = Arc::clone(&process); .map(|filename| {
let mut tx = tx.clone(); let semaphore = semaphore.clone();
let process = Arc::clone(&process);
#[cfg(feature="progress")] let mut progress = progress.clone(); let mut tx = tx.clone();
(tokio::spawn(async move { #[cfg(feature="progress")] let mut progress = progress.clone();
#[cfg(feature="progress")] type Opt<T> = OptionFuture<T>;
#[cfg(not(feature="progress"))] type Opt<T> = std::marker::PhantomData<T>; (tokio::spawn(async move {
let _task_id: Opt<_> = { #[cfg(feature="progress")] type Opt<T> = OptionFuture<T>;
let _lock = maybe_await(semaphore.map(|x| x.acquire_owned())).await; #[cfg(not(feature="progress"))] type Opt<T> = std::marker::PhantomData<T>;
let _task_id: Opt<_> = {
let _lock = maybe_await(semaphore.map(|x| x.acquire_owned())).await;
// (task_id, worker_result) // (task_id, worker_result)
let worker = { let worker = {
cfg_if!{ cfg_if!{
if #[cfg(feature="progress")] { if #[cfg(feature="progress")] {
let worker = do_work(process.as_ref(), &filename, progress.clone()); let worker = do_work(process.as_ref(), &filename, progress.clone());
let task = progress.add_task(format!("{:?}", filename.as_ref())); let task = progress.add_task(format!("{:?}", filename.as_ref()));
future::join(task, worker).await future::join(task, worker).await
} else { } else {
#[cfg(nightly)] type NoReturn = !; #[cfg(nightly)] type NoReturn = !;
#[cfg(not(nightly))] type NoReturn = (); #[cfg(not(nightly))] type NoReturn = ();
(Option::<NoReturn>::None, do_work(process.as_ref(), &filename, ()).await) (Option::<NoReturn>::None, do_work(process.as_ref(), &filename, ()).await)
} }
} }
}; };
match worker.1 { match worker.1 {
Ok(strings) => tx.send((filename, strings, i)).await.map_err(|_| "Child panic").unwrap(), Ok(strings) => tx.send((filename, strings, i)).await.map_err(|_| "Child panic").unwrap(),
Err(error) => { Err(error) => {
#[cfg(not(feature="progress"))] eprintln!("[!] {:?}: {}", filename.as_ref(), 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!("[!] ({}) {:?}: {}", i, filename.as_ref(), error)).await.or_else(|e| {eprintln!("\n{}",e); Err(e)}); #[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!{
cfg_if!{ if #[cfg(feature="progress")] {
if #[cfg(nightly)] { worker.0.ok().into()
std::marker::PhantomData::<!> } else {
} else { cfg_if!{
std::marker::PhantomData::<()> 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() .into_iter()
.filter_map(|x| x.err()); .filter_map(|x| x.err());

Loading…
Cancel
Save