Git Product home page Git Product logo

spinach's Introduction

🥬 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'");
    // ...

Related

Inspired by:

spinach's People

Contributors

etienne-napoleone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.