diff --git a/doc/duck.ai_2025-06-05_04-03-23.md b/doc/duck.ai_2025-06-05_04-03-23.md new file mode 100644 index 0000000..0403195 --- /dev/null +++ b/doc/duck.ai_2025-06-05_04-03-23.md @@ -0,0 +1,87 @@ +Diese Konversation wurde mit Duck.ai (https://duck.ai) unter Verwendung des o3-mini-Modells von OpenAI generiert. KI-Chats zeigen möglicherweise falsche oder anstößige Informationen an (weitere Informationen siehe https://duckduckgo.com/duckai/privacy-terms). + +==================== + +Benutzereingabe 1 von 2 - 5.6.2025, 02:27:36: +in Rust, what can I use to display multiple progress bars in the terminal? + +o3-mini: +You can use the indicatif crate, which provides a MultiProgress type that lets you manage and display multiple progress bars concurrently in a terminal. + +Below is a simple example of how to use indicatif’s MultiProgress to display multiple progress bars: + +```rust +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use std::{thread, time::Duration}; + +fn main() { + // Create a MultiProgress container. + let m = MultiProgress::new(); + + // Create several progress bars. + let pb1 = m.add(ProgressBar::new(100)); + pb1.set_style( + ProgressStyle::default_bar() + .template("{msg} {bar:40.cyan/blue} {pos:>3}/{len:3}") + .expect("Failed to set progress bar style"), + ); + pb1.set_message("Downloading"); + + let pb2 = m.add(ProgressBar::new(50)); + pb2.set_style( + ProgressStyle::default_bar() + .template("{msg} {bar:40.green/black} {pos:>3}/{len:3}") + .expect("Failed to set progress bar style"), + ); + pb2.set_message("Processing"); + + // Optionally, spawn threads to update the progress bars concurrently. + let h1 = std::thread::spawn(move || { + for i in 0..=100 { + pb1.set_position(i); + thread::sleep(Duration::from_millis(50)); + } + pb1.finish_with_message("Download complete"); + }); + + let h2 = std::thread::spawn(move || { + for i in 0..=50 { + pb2.set_position(i); + thread::sleep(Duration::from_millis(100)); + } + pb2.finish_with_message("Processing complete"); + }); + + // Wait for threads to complete. MultiProgress takes care of rendering. + h1.join().expect("Thread 1 panicked"); + h2.join().expect("Thread 2 panicked"); + + // You may also iterate on the multi-progress which blocks until all bars finish: + // m.join().unwrap(); +} +``` + +### Steps Explained + +1. **MultiProgress Container**: + Create a `MultiProgress` instance which is used to manage multiple progress bars. + +2. **Progress Bars**: + Create individual progress bars using `MultiProgress::add()`. You can customize each progress bar with a message, style, and length. + +3. **Concurrent Updates**: + The example spawns two threads – one for each progress bar. Each thread updates its progress bar independently using `set_position()` and finishes the progress bar with `finish_with_message()`. + +4. **Thread Joining**: + Ensure the main thread waits for the worker threads to complete to prevent the program from exiting prematurely. + +### Installation + +Make sure to add indicatif to your Cargo.toml: + +```toml +[dependencies] +indicatif = "0.17" # or the latest version available on crates.io +``` + +This crate provides many options for customizing and using progress bars in a concurrent and effective manner. The documentation is available at [indicatif on docs.rs](https://docs.rs/indicatif) for further details and advanced usage. diff --git a/src/main.rs b/src/main.rs index 6e79ec2..ddb38a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,18 +35,16 @@ fn main() { .build() .into(); - let mut state = AppState::try_resume(&args) - .map(|state| { + let mut state = match AppState::try_resume(&args) { + Some(state) => { info!("loaded state: {state:?}"); - state - }) - .unwrap_or_else(|| { - AppState::from_args(&args, &agent).unwrap_or_else(|e| { - error!("could not create new state: {e}"); - exit(1); - }) - }); + } + None => AppState::from_args(&args, &agent).unwrap_or_else(|e| { + error!("could not create new state: {e}"); + exit(1); + }), + }; info!("continuing with state: {state:?}"); state.save().unwrap();