use futures_util::StreamExt;
use progressor::{Progress, State, progress};
#[tokio::main]
async fn main() {
println!("Starting cancellation example...");
let task = progress(100, |mut updater| async move {
for i in 0..=100 {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
if i % 10 == 0 {
updater.update_with_message(i, format!("Processing step {i}/100"));
} else {
updater.update(i);
}
if i == 30 {
println!("\n⏸️ Pausing task at 30%...");
updater.pause();
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
println!("▶️ Resuming task...");
}
if i >= 60 {
println!("\n⚠️ Cancelling task...");
updater.cancel();
return "Task cancelled by user";
}
}
"Task completed successfully!"
});
let mut progress_stream = task.progress();
tokio::select! {
result = task => {
println!("\nTask result: {result}");
}
() = async {
while let Some(update) = progress_stream.next().await {
print!("\rProgress: {:.1}% ({}/{})",
update.completed_fraction() * 100.0,
update.current(),
update.total());
if let Some(message) = update.message() {
print!(" - {message}");
}
match update.state() {
State::Working => {
}
State::Paused => {
print!(" [PAUSED]");
}
State::Completed => {
println!("\n✅ Progress completed!");
break;
}
State::Cancelled => {
println!("\n❌ Progress was cancelled!");
break;
}
}
}
} => {}
}
}