#applications #terminal #interactive #command-line-interface #interactivity

spinner

A simple library to add more interactivity to your terminal applications

6 releases (breaking)

Uses old Rust 2015

0.5.0 Nov 4, 2019
0.4.0 Jan 11, 2016
0.3.0 Jan 11, 2016
0.2.1 Jan 11, 2016
0.1.0 Jan 10, 2016

#205 in #interactive

Download history 296/week @ 2024-07-27 198/week @ 2024-08-03 274/week @ 2024-08-10 184/week @ 2024-08-17 210/week @ 2024-08-24 238/week @ 2024-08-31 193/week @ 2024-09-07 196/week @ 2024-09-14 226/week @ 2024-09-21 222/week @ 2024-09-28 58/week @ 2024-10-05 141/week @ 2024-10-12 151/week @ 2024-10-19 173/week @ 2024-10-26 173/week @ 2024-11-02 30/week @ 2024-11-09

557 downloads per month
Used in 18 crates (3 directly)

LGPL-3.0

21KB
338 lines

Spinner, a simple library to create interactive terminal applications

Documentation

To use Spinner simply add it to your Cargo.toml

[dependencies.spinner]
version = "0.3"

Usage

There are two parts to Spinner, one part is the spinner itself, the other a simple interface to build menus.

Spinners

To use a Spinner simply go and create one using the SpinnerBuilder:

use spinner::SpinnerBuilder;
let sp = SpinnerBuilder::new("Long Running operation, please wait...".into()).start();

Will inform the user that your app currently is doing background processing. sp is a SpinnerHandle, through which you can tell the user for example how far along the process you are, or perhaps a message in between.

use spinner::SpinnerBuilder;
let sp = SpinnerBuilder::new("Long Running operation, please wait...".into()).start();
sp.message("Updating...".into());
sp.update(format!("Finished {} out of {}.", i, max));

Customizing

A spinner can be customized in three ways:

  • The step duration, which is the 'refresh' period of the message.
  • The format, how a given string is printed, due to limitations this is done through a closure, but it also allows more special formatting than just a format string.
  • The spinner itself, which is the list of characters that change every step.

Menus

Menus are simple, type checked ways to ask the user for information.

A simple menu might look like this:

use spinner::menu::*;
let m = Menu::new(vec![
    MenuOption("First Name".into(), MenuType::Text, MenuOptional::Optional, None),
    MenuOption("Last Name".into(), MenuType::Text, MenuOptional::Required, None),
    MenuOption("Age".into(), MenuType::Integer, MenuOptional::Optional, Some(MenuValue::Integer(1))),
    MenuOption("How much Ketchup?".into(), MenuType::Float, MenuOptional::Optional, None),
]);

let results = m.display();

In results will then be an array of MenuValue, which can then be retrieved by either get_{string,int,float}, calling one of these on the wrong type will panic!. So be careful to take out the correct value out of the correct menu questions.

MenuOption

A MenuOption is a NewType. It consists of a string which will constitute the question being presented to the user. Then a MenuType, telling the checker what input is expected. If you need something else use MenuType::Text and work with that. You also have to tell whether that input is optional or not. (true=optional, false=not optional). At last, an Option<MenuValue> which allows you to put in either None, for no default value or Some<MenuValue> which will be used if the user inputs nothing.

Dependencies

~245–700KB