Crate throbber

Crate throbber 

Source
Expand description

Crates.io docs.rs GitHub last commit License

This crate serves as an alternative to loading. It is used to display a throbber animation in the terminal while other calculations are done in the main program.

Throbber Preview

§Usage

Add this to your Cargo.toml:

[dependencies]
throbber = "1.0"

To display a throbber animation, first create a Throbber object:

use throbber::Throbber;
let mut throbber = Throbber::default();

You can also customize certain settings like the displayed animation and the displayed message:

let mut throbber = Throbber::default()
    .message("calculating stuff")
    .frames(&throbber::MOVE_EQ_F); // this crate comes with a few predefined animations
                                   // see the Constants section

Then you can simply call start wherever you want to start the animation and a finish function like success where you want to stop it.

throbber.start();
// do calculations
throbber.success("calculations successful!");

After, you can call start or start_with_msg again to start the animation again. Setters are also provided, e.g. set_message and set_frames. This also works while an animation is running.

§Thread Lifetime

The Throbber thread gets spawned on the first call to start or start_with_msg. After that, the thread only ever gets parked. If you want to end the thread, you must drop the Throbber object:

drop(throbber);

§Examples

This is the example from the gif above:

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

fn main() {
    let mut throbber = Throbber::default().message("calculating stuff");

    throbber.start();
    // do stuff
    thread::sleep(Duration::from_secs(2));
    throbber.success("Success");

    throbber.start_with_msg("calculating more stuff");
    // do other stuff
    thread::sleep(Duration::from_secs(2));
    throbber.fail("Fail");
}

You can also keep track of progress with set_message:

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

fn main() {
    let mut throbber = Throbber::new(
        "Downloading file1 0%",
        Duration::from_millis(100),
        &throbber::ROTATE_F,
    );

    throbber.start();
    for i in 0..100 {
        throbber.set_message(format!("Downloading file1 {}%", i));
        thread::sleep(Duration::from_millis(30));
    }
    throbber.success("Downloaded file1");

    throbber.start_with_msg("Downloading file2 0%");
    for i in 0..69 {
        throbber.set_message(format!("Downloading file2 {}%", i));
        thread::sleep(Duration::from_millis(30));
    }
    throbber.fail("Download of file2 failed");
}

Structs§

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

Constants§

CIRCLE_F
◐ ◓ ◑ ◒
DEFAULT_F
⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏
MOVE_EQ_F
[= ] [ = ] [ =] [ = ]
MOVE_EQ_LONG_F
[= ] [== ] [ == ] [ == ] [ ==] [ =] [ ==] [ == ] [ == ] [== ]
MOVE_MIN_F
[- ] [ - ] [ -] [ - ]
MOVE_MIN_LONG_F
[- ] [-- ] [ -- ] [ -- ] [ --] [ -] [ --] [ -- ] [ -- ] [-- ]
ROTATE_F
| / - \