console-utils 1.3.0

Cli input utilities.
Documentation

Console Utility Library

Crate API

A simple Rust library for console-based user input, option selection and more.

Input Function

The input function reads user input from the console. It prompts the user with a message, reads a line of input, and returns an Option<T>.

Usage

use console_utils::input;

fn main() {
    // Prompt the user for input
    let user_input = input::<String>("Enter something: ", false);

    // Process the user input
    match user_input {
        Some(value) => println!("You entered: {}", value),
        None => panic!("The Input cannot be None, allow_empty is false."),
    }
}

Select Function

The select function allows the user to interactively select options from a list. It uses arrow keys or 'w' and 's' keys for navigation, spacebar for selection, and Enter to confirm. It returns an Option<Vec<bool>> indicating which options were selected.

Usage


use console_utils::select;

fn main() {
    let options = vec![
        "Option 1",
        "Option 2",
        "Option 3",
    ];

    // Prompt the user to select options
    let selected_options = select("Select an option:", &options, false, false);

    // Process the selected options
    match selected_options {
        Some(selections) => {
            for (i, selected) in selections.iter().enumerate() {
                println!("Option {} selected: {}", i + 1, selected);
            }
        }
        None => panic!("The Options cannot be None, allow_empty is false."),
    }
}

Spinner Function

The spinner function creates a console-based spinner animation, offering a visually appealing way to indicate ongoing processes.

Usage

use console_utils::{spinner, SpinnerType};
fn main() {
    // Display a standard spinner for 3 seconds
    spinner(3.0, SpinnerType::Standard);
    
    // Display a dots spinner for 2 seconds
    spinner(2.0, SpinnerType::Dots);
    
    // Display a custom spinner for 1 second (using custom frames)
    spinner(1.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
    
    // Display a box spinner for 1.5 seconds
    spinner(1.5, SpinnerType::Box);
    
    // Display a flip spinner for 2 seconds
    spinner(2.0, SpinnerType::Flip);
}

Reveal Function

Displays a string gradually, revealing one character at a time with a specified time interval between each character.

Usage

use console_utils::reveal;
fn main() {
    // Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished.
    reveal("Hello World!", 0.1);
}

Clear Line Function

Clears the current line in the console.

This function uses ANSI escape codes to clear the entire line and move the cursor to the beginning of the line.

Usage

use console_utils::clear_line;
fn main() {
    // Clear the current line
    clear_line();
}

For more detailed documentation, please refer to the generated Rust Docs.