use clap::Args;
use cmfy::{Client, Error, Result};
use humantime::Duration;
use tokio::time::timeout;
use super::Run;
#[derive(Debug, Args)]
pub struct Clear {
#[clap(long, short, action, default_value_t = true)]
wait: bool,
#[clap(long, short, action, default_value = "10s")]
timeout: Duration,
#[clap(long, short, action, default_value = "100ms")]
retry: Duration,
}
impl Run for Clear {
async fn run(self, client: Client) -> Result<()> {
client.clear_queue().await?;
client.cancel_running_prompt().await?;
if self.wait {
timeout(self.timeout.into(), async {
loop {
let queue = client.queue().await?;
if queue.running.is_empty() && queue.pending.is_empty() {
break Ok::<(), Error>(());
}
tokio::time::sleep(self.retry.into()).await;
}
})
.await??;
}
client.clear_history().await?;
Ok(())
}
}