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 leanify;
mod colour;
mod dir;
mod fixed_stack;
mod process;
@ -32,6 +33,8 @@ mod work;
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 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 {
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<Item=T>,
})
};
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<T> = OptionFuture<T>;
#[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;
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<T> = OptionFuture<T>;
#[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)
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::<NoReturn>::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::<NoReturn>::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());

Loading…
Cancel
Save