Struct Throbber

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

Representation of a throbber animation. It can start, succeed, fail or end at any point.

Note that a call to end takes ownership of the struct and drops it, as such it should be called to completely remove the throbber animtion object. If you want to start another animation afterwards, you have to create a new Throbber object. This is done because multiple calls to start do not actually create multiple threads, instead a call to a finish function (like success) simply parks the thread and a following start call unparks that thread again. As such, a call to end kills the thread entirely. If you want to just stop the animation, but potentially start it again later on, use finish instead.

§Examples

use throbber::Throbber;
use std::thread;
use std::time::Duration;

let mut throbber = Throbber::new()
    .message("calculating stuff".to_string())
    .interval(Duration::from_millis(50))
    .frames(&throbber::ROTATE_F);

throbber.start();

// do stuff
thread::sleep(Duration::from_secs(5));

throbber.success("calculation successful".to_string());
throbber.end();

Implementations§

Source§

impl Throbber

Source

pub fn new() -> Self

Creates a new Throbber object.

§Default Values

If you do not customize your throbber animation with message etc., these are the default values:

  • message: ""
  • interval: Duration::from_millis(200)
  • frames: DEFAULT_F (⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)
Examples found in repository?
examples/calculation.rs (line 6)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 6)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn message(self, msg: String) -> Self

Sets the message that’s supposed to print.

This does nothing if start was called before. To change the message after start was called, use change_message instead.

Examples found in repository?
examples/calculation.rs (line 6)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 7)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn interval(self, interval: Duration) -> Self

Sets the interval in which the animation frames are supposed to print.

This does nothing if start was called before. To change the interval after start was called, use change_interval instead.

Examples found in repository?
examples/download.rs (line 9)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn frames(self, frames: &'static [&'static str]) -> Self

Sets the animation frames that are supposed to print.

This does nothing if start was called before. To change the animation frames after start was called, use change_frames instead.

Examples found in repository?
examples/download.rs (line 8)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn change_message(&mut self, msg: String)

Changes the message that’s supposed to print.toml

Unlike message, this will work both before and after start was called.

Examples found in repository?
examples/download.rs (line 13)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn change_interval(&mut self, interval: Duration)

Changes the interval in which the animation frames are supposed to print.

Unlike interval, this will work both before and after start was called.

Source

pub fn change_frames(&mut self, frames: &'static [&'static str])

Changes the animation frames that are supposed to print.

Unlike frames, this will work both before and after start was called.

Source

pub fn start(&mut self)

Starts the animation.

If this is the first call to start, a new thread gets created to play the animation. Otherwise the thread that already exists gets unparked and starts the animation again.

Examples found in repository?
examples/calculation.rs (line 8)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 11)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn start_with_msg(&mut self, msg: String)

Starts the animation with the specified msg.

Equivalent to throbber.change_message(msg); throbber.start();.

Examples found in repository?
examples/calculation.rs (line 13)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 18)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn finish(&mut self)

Stops the current animation, leaving a blank line.

Source

pub fn success(&mut self, msg: String)

Stops the current animation and prints msg as a success message.

Examples found in repository?
examples/calculation.rs (line 11)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 16)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn fail(&mut self, msg: String)

Stops the current animation and prints msg as a fail message.

This does currently not print the fail message onto stderr, but stdout instead. That might change in a future version.

Examples found in repository?
examples/calculation.rs (line 16)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 23)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}
Source

pub fn end(self)

Ends the animation thread and drops the throbber object. If you want to stop the animation without dropping the throbber object, use finish instead.

Examples found in repository?
examples/calculation.rs (line 18)
5fn main() {
6    let mut throbber = Throbber::new().message("calculating stuff".to_string());
7
8    throbber.start();
9    // do stuff
10    thread::sleep(Duration::from_secs(2));
11    throbber.success("Success".to_string());
12
13    throbber.start_with_msg("calculating more stuff".to_string());
14    // do other stuff
15    thread::sleep(Duration::from_secs(2));
16    throbber.fail("Fail".to_string());
17
18    throbber.end();
19}
More examples
Hide additional examples
examples/download.rs (line 25)
5fn main() {
6    let mut throbber = Throbber::new()
7        .message("Downloading file1 0%".to_string())
8        .frames(&throbber::ROTATE_F)
9        .interval(Duration::from_millis(100));
10
11    throbber.start();
12    for i in 0..100 {
13        throbber.change_message(format!("Downloading file1 {}%", i));
14        thread::sleep(Duration::from_millis(30));
15    }
16    throbber.success("Downloaded file1".to_string());
17
18    throbber.start_with_msg("Downloading file2 0%".to_string());
19    for i in 0..69 {
20        throbber.change_message(format!("Downloading file2 {}%", i));
21        thread::sleep(Duration::from_millis(30));
22    }
23    throbber.fail("Download of file2 failed".to_string());
24
25    throbber.end();
26}

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.