|
1 | | -# Console Utility Library |
| 1 | +# Console Utils |
2 | 2 |
|
3 | 3 | [](https://crates.io/crates/console-utils) |
4 | 4 | [](https://docs.rs/console-utils) |
5 | 5 |
|
6 | | -A simple Rust library for console-based user input, option selection and more. |
| 6 | +_A Rust library for console-based user input, option selection, control, and more._ |
7 | 7 |
|
8 | | -## Input Function |
| 8 | +# Overview |
9 | 9 |
|
10 | | -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>`. |
| 10 | +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. |
11 | 11 |
|
12 | | -### Usage |
13 | 12 |
|
14 | | -```rust |
15 | | -use console_utils::input; |
16 | 13 |
|
17 | | -fn main() { |
18 | | - // Prompt the user for input |
19 | | - let user_input = input::<String>("Enter something: ", false); |
| 14 | +## Usage |
20 | 15 |
|
21 | | - // Process the user input |
22 | | - match user_input { |
23 | | - Some(value) => println!("You entered: {}", value), |
24 | | - None => panic!("The Input cannot be None, allow_empty is false."), |
25 | | - } |
26 | | -} |
| 16 | +To use Console Utils in your Rust project, you can add the following dependency to your `Cargo.toml` file: |
| 17 | + |
| 18 | +```toml |
| 19 | +[dependencies] |
| 20 | +console-utils = "1.5.4" |
27 | 21 | ``` |
28 | | -## Select Function |
29 | 22 |
|
30 | | -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. |
| 23 | +After adding the dependency, you can import the modules you need in your Rust code. For example: |
31 | 24 |
|
32 | | -### Usage |
| 25 | +```rust |
| 26 | +use console_utils::input::{input, select}; |
| 27 | +use console_utils::control::{flush, clear_line}; |
| 28 | +``` |
| 29 | + |
| 30 | +## Example |
33 | 31 |
|
34 | 32 | ```rust |
35 | | -use console_utils::select; |
| 33 | +use console_utils::input::{input, select, spinner, SpinnerType}; |
36 | 34 |
|
37 | 35 | fn main() { |
| 36 | + // Read user input as a string |
| 37 | + let user_input: Option<String> = input("Enter something: ", false); |
| 38 | + |
| 39 | + match user_input { |
| 40 | + Some(value) => println!("You entered: {}", value), |
| 41 | + None => panic!("Input cannot be None when 'allow_empty' is set to false."), |
| 42 | + } |
| 43 | + |
| 44 | + // Display a standard spinner for 3 seconds |
| 45 | + spinner(3.0, SpinnerType::Standard); |
| 46 | + |
38 | 47 | let options = vec![ |
39 | 48 | "Option 1", |
40 | 49 | "Option 2", |
41 | 50 | "Option 3", |
42 | 51 | ]; |
43 | 52 |
|
44 | | - // Prompt the user to select options |
45 | | - let selected_options = select("Select an option:", &options, false, false); |
| 53 | + // Allow the user to select one option |
| 54 | + let selected_indices = select("Select an option:", &options, false, false); |
46 | 55 |
|
47 | | - // Process the selected options |
48 | | - match selected_options { |
49 | | - Some(selections) => { |
50 | | - for (i, selected) in selections.iter().enumerate() { |
51 | | - println!("Option {} selected: {}", i + 1, selected); |
52 | | - } |
53 | | - } |
| 56 | + match selected_indices { |
| 57 | + Some(indices) => println!("Selected indices: {:?}", indices), |
54 | 58 | None => panic!("The Options cannot be None, allow_empty is false."), |
55 | 59 | } |
56 | | -} |
57 | | -``` |
58 | | - |
59 | | -## Read Function |
60 | | - |
61 | | -The read module provides cross-platform functionality for reading keyboard input. It includes the Key enum representing different keyboard keys and platform-specific implementations for Windows and Unix. |
62 | | - |
63 | | -### Usage |
64 | | - |
65 | | -```rust |
66 | | -use console_utils::read::{Key, read_key}; |
67 | | - |
68 | | -fn main() { |
69 | | - // Cross-platform key reading example |
70 | | - let key = read_key().unwrap(); |
71 | | - |
72 | | - println!("Pressed key: {:?}", key); |
73 | | -} |
74 | | -``` |
75 | | - |
76 | | -## Spinner Function |
77 | | - |
78 | | -The spinner function creates a console-based spinner animation, offering a visually appealing way to indicate ongoing processes. |
79 | | - |
80 | | -### Usage |
81 | | - |
82 | | -```rust |
83 | | -use console_utils::{spinner, SpinnerType}; |
84 | | -fn main() { |
85 | | - // Display a standard spinner for 3 seconds |
86 | | - spinner(3.0, SpinnerType::Standard); |
87 | | - |
88 | | - // Display a dots spinner for 2 seconds |
89 | | - spinner(2.0, SpinnerType::Dots); |
90 | | - |
91 | | - // Display a custom spinner for 1 second (using custom frames) |
92 | | - spinner(1.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"])); |
93 | 60 |
|
94 | | - // Display a box spinner for 1.5 seconds |
95 | | - spinner(1.5, SpinnerType::Box); |
96 | | - |
97 | | - // Display a flip spinner for 2 seconds |
98 | | - spinner(2.0, SpinnerType::Flip); |
99 | | -} |
100 | | -``` |
101 | | - |
102 | | -## Reveal Function |
103 | | - |
104 | | -Displays a string gradually, revealing one character at a time with a specified time interval between each character. |
105 | | - |
106 | | -### Usage |
107 | | - |
108 | | -```rust |
109 | | -use console_utils::reveal; |
110 | | -fn main() { |
111 | | - // Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished. |
| 61 | + // Display "Hello World!" with a time interval of 0.1 seconds between each character |
112 | 62 | reveal("Hello World!", 0.1); |
| 63 | + |
| 64 | + // Clear the current line in the console, so the "Hello World!" |
| 65 | + clear_line(); |
113 | 66 | } |
114 | 67 | ``` |
115 | 68 |
|
116 | | -## Output Control and Cursor Movement |
117 | | - |
118 | | -The library also provides functions for output control and precise cursor movement: |
119 | | - |
120 | | -- `clear_line` |
121 | | -- `flush` |
122 | | -- `move_cursor_down` |
123 | | -- `move_cursor_up` |
124 | | -- `move_cursor_left` |
125 | | -- `move_cursor_right` |
126 | | -- `move_cursor_to` |
127 | | - |
128 | 69 | For more detailed documentation, please refer to the [generated Rust Docs](https://docs.rs/console-utils/latest/console_utils/). |
0 commit comments