6 stable releases

2.1.0 Mar 26, 2022
2.0.0 Mar 26, 2022
1.2.0 Mar 24, 2022
1.1.0 Mar 24, 2022
1.0.2 Mar 23, 2022

#269 in Command-line interface

Download history 28/week @ 2024-01-01 80/week @ 2024-01-08 98/week @ 2024-01-15 55/week @ 2024-01-22 52/week @ 2024-01-29 43/week @ 2024-02-05 114/week @ 2024-02-12 136/week @ 2024-02-19 118/week @ 2024-02-26 122/week @ 2024-03-04 144/week @ 2024-03-11 200/week @ 2024-03-18 148/week @ 2024-03-25 180/week @ 2024-04-01 145/week @ 2024-04-08 91/week @ 2024-04-15

594 downloads per month
Used in 8 crates

GPL-3.0-or-later

21KB
334 lines

🥬 spinach

Crates.io Docs.rs License CI

Practical spinner for Rust

Install

Add as a dependency to your Cargo.toml.

[dependencies]
spinach = "2"

Usage

Basic example.

use std::thread::sleep;
use std::time::Duration;

use spinach::Spinach;

fn main() {
    let s = Spinach::new("Running task 1");
    sleep(Duration::from_secs(1));

    s.text("Running task 2");
    sleep(Duration::from_secs(1));

    s.succeed("Ran tasks successfully");
}

For general convenience, text can be passed as String or &str. When an Option, can be passed as String, &str or Option<String>

Creating

use spinach::{Color, Spinach, Spinner};

// Using defaults + custom text
let s = Spinach::new("custom text");

// Using custom spinner
let spinner = Spinner::new(vec!["",""], 80);
let s = Spinach::new_with(spinner, "custom text", Color::Red));

// Also with partial config (fallback to defaults)
let s = Spinach::new_with(None, "custom text", Color::Green);

Updating

use spinach::{Color, Spinach};

let s = Spinach::new("custom text");

// Updating text
s.text("new text");

// Updating color
s.color(Color::White);

// Updating multiple
s.update_with("new text", Color::Red);

// Also with partial update (keep current)
s.update_with(None, Color::Red);

Stopping

use spinach::{Color, Spinach};

let s = Spinach::new("custom text");

// Stop with final `✔` frame, green color and optional text change.
s.success("gg");

// Stop with final `✖` frame, red color and optional text change.
s.fail("ups");

// Stop with final `⚠` frame, yellow color and optional text change.
s.warn(None);

// Stop with final `ℹ` frame, blue color and optional text change.
s.info("notice");

// Stop current spinner (freeze the frame)
s.stop();

// Stopping with custom final frame, text and color
s.stop_with("🥬", "spinach'd", Color::Ignore);

// Also with partial update (keep current)
s.stop_with(None, None, Color::Blue);

Freezing

Will freeze the current line with passed options and continue on a new line.

use spinach::{Color, Spinach};

let s = Spinach::new("Cutting spinaches");

// Similar to `stop_with`, but with an extra argument to change the spinner text.
s.freeze("🥬", "Spinaches cut", None, "Cutting carottes");

FAQ

How to avoid leaving terminal without prompt on interupt (ctrl^c)?

You can use a library like ctrlc to handle interupts.

The most basic way to handle it would be in conjuction with this lib QoL show_cursor function like this:

use spinach::{term, Spinach};

fn main() {
    ctrlc::set_handler(|| {
        term::show_cursor();
        std::process::exit(0);
    })
    .expect("Error setting Ctrl-C handler");

    let s = Spinach::new("spinnin'");
    // ...

Inspired by:

No runtime deps