pub struct ProgressBar {
pub start: u64,
pub goal: u64,
pub get_progress: Arc<Mutex<dyn Fn() -> u64 + Send>>,
pub style: Style,
}Expand description
A ProgressBar handles the animation of a progress bar.
Fields§
§start: u64The start value of the progress.
The start and goal values must be absolute values, not relative to each other.
For example, if your ideal state is 700 and your current state is 500, set start to 500 and goal to 700.
Vice versa, if you want to start at 50% of the progress, set start to 50 and goal to 100.
goal: u64The goal value of the progress. It must be an absolute value, not relative to the start.
get_progress: Arc<Mutex<dyn Fn() -> u64 + Send>>A closure to get the current progress value.
style: StyleImplementations§
Source§impl ProgressBar
impl ProgressBar
Sourcepub fn new<F>(
start: u64,
goal: u64,
get_progress: F,
style: Style,
) -> ProgressBar
pub fn new<F>( start: u64, goal: u64, get_progress: F, style: Style, ) -> ProgressBar
new() initializes a new progress bar.
Examples found in repository?
examples/main.rs (line 37)
9fn main() {
10 // --------------------------------------------- //
11 // ---------- Example: Progress Bar ------------ //
12 // --------------------------------------------- //
13
14 let progress_value = Arc::new(Mutex::new(0));
15
16 // Create a clone of the progress_value for the other thread.
17 let thread_progress_value = progress_value.clone();
18
19 // Some work done in another thread.
20 let do_some_work = thread::spawn(move || {
21 let mut num = 0;
22 while num <= 100 {
23 thread::sleep(time::Duration::from_millis(20));
24
25 let mut val = thread_progress_value.lock().unwrap();
26 *val = num;
27
28 num += 1;
29 }
30 });
31
32 // Initialize a progress bar.
33 let style = ProgressBarStyleBuilder::new() // `Style` provides builder pattern.
34 .color(Color::Green)
35 .bar_length(60)
36 .build();
37 let progress_bar = ProgressBar::new(0, 100, move || *progress_value.lock().unwrap(), style);
38
39 let mut writer = std::io::stdout();
40
41 // Start the progress bar.
42 progress_bar.start(&mut writer);
43
44 // Wait for the worker thread to finish.
45 do_some_work.join().unwrap();
46
47 // ------------------------------------------------- //
48 // ---------- Example: Interactive Menu ------------ //
49 // ------------------------------------------------- //
50
51 let options = vec![
52 "Tokyo".to_string(),
53 "Saitama".to_string(),
54 "Kanagawa".to_string(),
55 ];
56
57 // Initialize an interactive menu.
58 let mut menu = InteractiveMenu::new(
59 options.clone(),
60 InteractiveMenuStyleBuilder::new() // `Style` provides builder pattern.
61 .color(Color::Blue)
62 .selected_prefix('*')
63 .build(),
64 );
65
66 // Run the interactive menu.
67 let selected_index = menu.run().unwrap();
68
69 println!("You selected: {}", options[selected_index]);
70
71 // ------------------------------------------------- //
72 // ---------- Example: Loading Indicator ----------- //
73 // ------------------------------------------------- //
74
75 let loading_indicator = LoadingIndicator::new(Color::Red);
76 loading_indicator.start();
77
78 // Do some work while the loading indicator is running.
79 let mut num = 0;
80 while num <= 100 {
81 thread::sleep(time::Duration::from_millis(20));
82 num += 1;
83 }
84
85 // Stop the loading indicator.
86 loading_indicator.stop();
87}Sourcepub fn start(&self, writer: &mut Stdout)
pub fn start(&self, writer: &mut Stdout)
start() starts the animation of the progress bar.
It displays from 0% and goes to 100%.
Examples found in repository?
examples/main.rs (line 42)
9fn main() {
10 // --------------------------------------------- //
11 // ---------- Example: Progress Bar ------------ //
12 // --------------------------------------------- //
13
14 let progress_value = Arc::new(Mutex::new(0));
15
16 // Create a clone of the progress_value for the other thread.
17 let thread_progress_value = progress_value.clone();
18
19 // Some work done in another thread.
20 let do_some_work = thread::spawn(move || {
21 let mut num = 0;
22 while num <= 100 {
23 thread::sleep(time::Duration::from_millis(20));
24
25 let mut val = thread_progress_value.lock().unwrap();
26 *val = num;
27
28 num += 1;
29 }
30 });
31
32 // Initialize a progress bar.
33 let style = ProgressBarStyleBuilder::new() // `Style` provides builder pattern.
34 .color(Color::Green)
35 .bar_length(60)
36 .build();
37 let progress_bar = ProgressBar::new(0, 100, move || *progress_value.lock().unwrap(), style);
38
39 let mut writer = std::io::stdout();
40
41 // Start the progress bar.
42 progress_bar.start(&mut writer);
43
44 // Wait for the worker thread to finish.
45 do_some_work.join().unwrap();
46
47 // ------------------------------------------------- //
48 // ---------- Example: Interactive Menu ------------ //
49 // ------------------------------------------------- //
50
51 let options = vec![
52 "Tokyo".to_string(),
53 "Saitama".to_string(),
54 "Kanagawa".to_string(),
55 ];
56
57 // Initialize an interactive menu.
58 let mut menu = InteractiveMenu::new(
59 options.clone(),
60 InteractiveMenuStyleBuilder::new() // `Style` provides builder pattern.
61 .color(Color::Blue)
62 .selected_prefix('*')
63 .build(),
64 );
65
66 // Run the interactive menu.
67 let selected_index = menu.run().unwrap();
68
69 println!("You selected: {}", options[selected_index]);
70
71 // ------------------------------------------------- //
72 // ---------- Example: Loading Indicator ----------- //
73 // ------------------------------------------------- //
74
75 let loading_indicator = LoadingIndicator::new(Color::Red);
76 loading_indicator.start();
77
78 // Do some work while the loading indicator is running.
79 let mut num = 0;
80 while num <= 100 {
81 thread::sleep(time::Duration::from_millis(20));
82 num += 1;
83 }
84
85 // Stop the loading indicator.
86 loading_indicator.stop();
87}Auto Trait Implementations§
impl Freeze for ProgressBar
impl RefUnwindSafe for ProgressBar
impl Send for ProgressBar
impl Sync for ProgressBar
impl Unpin for ProgressBar
impl UnwindSafe for ProgressBar
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more