Struct LoadingIndicator

Source
pub struct LoadingIndicator { /* private fields */ }
Expand description

A LoadingIndicator handles the animation of a loading indicator.

Implementations§

Source§

impl LoadingIndicator

Source

pub fn new(color: Color) -> Self

new() initializes a new loading indicator.

Examples found in repository?
examples/main.rs (line 75)
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}
Source

pub fn start(&self)

start() spawn a new thread and display the loading indicator. It ends when stop() is called.

Examples found in repository?
examples/main.rs (line 76)
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}
Source

pub fn stop(&self)

stop() stops the loading indicator.

Examples found in repository?
examples/main.rs (line 86)
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}

Trait Implementations§

Source§

impl Default for LoadingIndicator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.