20 stable releases
1.6.0 | Mar 27, 2024 |
---|---|
1.5.9 | Dec 10, 2023 |
1.5.5 |
|
#1312 in Command line utilities
22KB
363 lines
Console Utils
A Rust library for console-based user input, option selection, control, and more.
Overview
This crate offers utility functions for various console-related operations in Rust programs. From obtaining user input to achieving precise terminal control, its main focus is to remain simple while providing extensive functionality.
Usage
To use Console Utils in your Rust project, you can add the following dependency to your Cargo.toml
file:
[dependencies]
console-utils = "1.6.0"
After adding the dependency, you can import the modules you need in your Rust code. For example:
use console_utils::input::{input, select};
use console_utils::control::{flush, clear_line};
Examples
Reading User Input
use console_utils::input::input;
// Read user input as a string
let user_input: String = input("Enter something: ");
println!("You entered: {}", user_input);
Selecting Options
Single Option
use console_utils::input::select;
let options = [
"Option 1",
"Option 2",
"Option 3",
];
// Allow the user to select one option
let selected_index = select("Select an option:", &options);
println!("Selected option: {}", options[selected_index]);
Multiple Options
use console_utils::input::multiselect;
let options = [
"Option 1",
"Option 2",
"Option 3",
];
// Allow the user to select multiple options
let selected_indices = multiselect("Select options:", &options);
println!("Selected indices: {:?}", selected_indices);
Console Control
use console_utils::control::{flush, clear_line};
// Flush the output buffer to ensure content is displayed immediately
flush();
// Clear the current line in the console
clear_line();
// and more...
// Consult the docs for more details!
Reading Key
use console_utils::read::{read_key};
// Cross-platform key reading
let key = read_key();
println!("Pressed key: {:?}", key);
Displaying a Spinner
use console_utils::input::{spinner, SpinnerType};
// Display a standard spinner for 3 seconds
spinner(3.0, SpinnerType::Standard);
// Display a custom spinner for 2 seconds
spinner(2.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
Gradual String Reveal
use console_utils::input::reveal;
// Display "Hello World!" with a time interval of 0.1 seconds between each character
reveal("Hello World!", 0.1);
For more detailed documentation, please refer to the generated Rust Docs.
Dependencies
~0–7.5MB
~58K SLoC