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
impl Throbber
Sourcepub fn new() -> Self
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?
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
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}
Sourcepub fn message(self, msg: String) -> Self
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?
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
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}
Sourcepub fn interval(self, interval: Duration) -> Self
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?
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}
Sourcepub fn frames(self, frames: &'static [&'static str]) -> Self
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?
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}
Sourcepub fn change_message(&mut self, msg: String)
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?
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}
Sourcepub fn change_interval(&mut self, interval: Duration)
pub fn change_interval(&mut self, interval: Duration)
Sourcepub fn change_frames(&mut self, frames: &'static [&'static str])
pub fn change_frames(&mut self, frames: &'static [&'static str])
Sourcepub fn start(&mut self)
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?
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
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}
Sourcepub fn start_with_msg(&mut self, msg: String)
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?
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
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}
Sourcepub fn success(&mut self, msg: String)
pub fn success(&mut self, msg: String)
Stops the current animation and prints msg
as a success message.
Examples found in repository?
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
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}
Sourcepub fn fail(&mut self, msg: String)
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?
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
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}
Sourcepub fn end(self)
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?
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
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}