prog_send! helper macro

cli-redesign
Avril 4 years ago
parent 5b975333f1
commit 970a221170
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -14,7 +14,6 @@ mod tasklist;
#[macro_use]
mod progress;
//TODO: Create a progress task module for atomically printing infos and stuff
//TODO: Create a module for temp files, pass the temp file to `perform` and do the regex fixing after `perform`
/// Download a loli async
@ -24,7 +23,8 @@ pub async fn perform(url: impl AsRef<str>, path: impl AsRef<Path>, mut progress:
let path = path.as_ref();
let task = url.to_owned(); //TODO: Real task name
progress.push_task(&task).await;
prog_send!(link progress.push_task(&task));
let mut resp = reqwest::get(url).await?;
let len = resp.content_length();
@ -34,19 +34,22 @@ pub async fn perform(url: impl AsRef<str>, path: impl AsRef<Path>, mut progress:
.create(true)
.truncate(true)
.write(true)
.read(true)
.open(path).await?;
progress.println(format!("req ok {:?}, file ok ({:?}), starting download", len, path)).await;
prog_send!(progress.println(format!("req ok {:?}, file ok ({:?}), starting download", len, path)));
let mut bytes = resp.bytes_stream();
while let Some(buffer) = bytes.next().await {
file.write(buffer?.as_ref()).await?;
progress.bump().await;
prog_send!(progress.bump());
}
progress.println(format!("done for {}", url)).await;
progress.bump().await;
//TODO: Decode `file`.
prog_send!(progress.println(format!("done for {}", url)));
prog_send!(progress.bump());
progress.pop_task(task).await;
prog_send!(progress.pop_task(task));
Ok(())
}
@ -83,7 +86,7 @@ pub async fn work(conf: config::Config) -> Result<(), Box<dyn std::error::Error>
}));
}
prog_writer.println("Waiting for children...").await;
prog_send!(link prog_writer.println("Waiting for children..."));
for child in children.into_iter()
{
match child.await {
@ -95,7 +98,7 @@ pub async fn work(conf: config::Config) -> Result<(), Box<dyn std::error::Error>
}
prog_send!(try link prog_writer.kill());
prog.await.expect("fatal");
prog.await.expect("mpsc fatal");
Ok(())
}

@ -37,6 +37,8 @@ enum CommandInternal
PopTask(String),
}
// TODO: Change from `Spinner` to `Bar`. Have push max, push current, etc.
struct Command
{
internal: CommandInternal,
@ -80,7 +82,7 @@ impl Command
macro_rules! prog_send {
(try link unwind $expression:expr) => {
{
$expression.await.expect("fatal").wait().await.expect("fatal")
$expression.await.expect("mpsc fatal").wait().await.expect("mpsc fatal")
}
};
(try link $expression:expr) => {
@ -88,9 +90,9 @@ macro_rules! prog_send {
$expression.await?.wait().await?
}
};
(try unind $expression:expr) => {
(try unwind $expression:expr) => {
{
let _ = $expression.await.expect("fatal");
let _ = $expression.await.expect("mpsc fatal");
}
};
(try $expression:expr) => {
@ -98,6 +100,13 @@ macro_rules! prog_send {
let _ =$expression.await?;
}
};
(link $expression:expr) => {
{
if let Ok(ok) = $expression.await {
let _ = ok.wait().await;
}
}
};
($expression:expr) => {
{
let _ = $expression.await;

Loading…
Cancel
Save