downcast + refresh okay

progress
Avril 4 years ago
parent 34ff9847cb
commit 5bb9ee59af
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -119,6 +119,9 @@ async fn begin() -> eyre::Result<i32>
}, },
})) }))
}; };
//let task_id = progress.send_command_and_downcast(progress::CommandKind::AddTask("Task name".to_owned())).await?.unwrap();
//progress.send_command_and_wait(progress::CommandKind::AddTask("Task 2 name".to_owned())).await?;
let state = Arc::new(state::State::new(validate_config(config::Config::default()).await let state = Arc::new(state::State::new(validate_config(config::Config::default()).await
.wrap_err(eyre!("Failed to validate config"))?, .wrap_err(eyre!("Failed to validate config"))?,
@ -134,6 +137,7 @@ async fn begin() -> eyre::Result<i32>
{ {
args::usage(); args::usage();
} }
//progress.send_command_and_wait(progress::CommandKind::RemoveTask(task_id)).await?;
//Cleanup deferred in new `async` block to drop `state` before this block is ran. //Cleanup deferred in new `async` block to drop `state` before this block is ran.
async move { async move {

@ -56,13 +56,16 @@ pub enum CommandKind
/// Will respond with `Result<String, IdNotFoundError>` of the removal operation /// Will respond with `Result<String, IdNotFoundError>` of the removal operation
RemoveTask(TaskId), RemoveTask(TaskId),
/// Set the title directly
SetTitle(String),
Shutdown, Shutdown,
Many(Vec<CommandKind>), Many(Vec<CommandKind>),
} }
/// The type sent in response to a `Command`. /// The type sent in response to a `Command`.
pub type Response = Option<Box<dyn std::any::Any + Send+ Sync + 'static>>; pub type Response = Option<Box<dyn std::any::Any + Send + 'static>>;
#[derive(Debug)] #[derive(Debug)]
enum CommandIter enum CommandIter
@ -198,6 +201,14 @@ impl<B: ProgressBar> Handle<B>
{ {
self.send_command(command).await?.await self.send_command(command).await?.await
} }
/// Send a command to the worker and then wait for it to be processed, then attempt to downcast to type `T`.
pub async fn send_command_and_downcast<T: 'static>(&mut self, command: CommandKind) -> Result<Option<T>, WorkerCommError>
{
let resp = self.send_command(command).await?.await?;
Ok(resp.map(|x| x.downcast::<T>().ok().map(|x| *x)).flatten())
}
/// Send a command to the worker but do not wait for it to be processed /// Send a command to the worker but do not wait for it to be processed
pub async fn send_command_and_detach(&mut self, command: CommandKind) -> Result<(), WorkerCommError> pub async fn send_command_and_detach(&mut self, command: CommandKind) -> Result<(), WorkerCommError>
@ -302,6 +313,14 @@ pub fn host<B: ProgressBar + Send + Sync + 'static>(bar: B) -> (Handle<B>, JoinH
update_bar!($($tt)*); update_bar!($($tt)*);
} }
}; };
(title $(std)? $title:ident $($tt:tt)*) => {
{
let mut bar = bar.write().await;
bar.set_title(&$title[..]);
update_bar!($($tt)*);
}
};
(title $($tt:tt)*) => { (title $($tt:tt)*) => {
{ {
let mut bar = bar.write().await; let mut bar = bar.write().await;
@ -405,6 +424,7 @@ pub fn host<B: ProgressBar + Send + Sync + 'static>(bar: B) -> (Handle<B>, JoinH
CommandKind::RemoveTask(id) => { CommandKind::RemoveTask(id) => {
send_response!(update_bar!(-task id title)); send_response!(update_bar!(-task id title));
}, },
CommandKind::SetTitle(string) => update_bar!(title string),
CommandKind::Refresh => update_bar!(refresh), CommandKind::Refresh => update_bar!(refresh),
CommandKind::Many(_) => unimplemented!(), CommandKind::Many(_) => unimplemented!(),
} }

Loading…
Cancel
Save