Skip to content

Commit 56160bd

Browse files
committed
✨ Major refactor && Change of input functions
1 parent 5dca526 commit 56160bd

File tree

8 files changed

+289
-372
lines changed

8 files changed

+289
-372
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "console-utils"
3-
version = "1.5.9"
3+
version = "1.6.0"
44
edition = "2021"
55
authors = ["Nils Wrenger <[email protected]>"]
66
description = "Simple CLI Input and Control Utilities"

README.md

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ _A Rust library for console-based user input, option selection, control, and mor
1010

1111
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.
1212

13-
14-
1513
## Usage
1614

1715
To use Console Utils in your Rust project, you can add the following dependency to your `Cargo.toml` file:
1816

1917
```toml
2018
[dependencies]
21-
console-utils = "1.5.9"
19+
console-utils = "1.6.0"
2220
```
2321

2422
After adding the dependency, you can import the modules you need in your Rust code. For example:
@@ -28,48 +26,85 @@ use console_utils::input::{input, select};
2826
use console_utils::control::{flush, clear_line};
2927
```
3028

31-
## Example
29+
## Examples
30+
31+
### Reading User Input
32+
33+
```rust, no_run
34+
use console_utils::input::input;
35+
// Read user input as a string
36+
let user_input: String = input("Enter something: ");
37+
38+
println!("You entered: {}", user_input);
39+
```
40+
41+
### Selecting Options
42+
43+
#### Single Option
44+
```rust, no_run
45+
use console_utils::input::select;
46+
let options = [
47+
"Option 1",
48+
"Option 2",
49+
"Option 3",
50+
];
51+
// Allow the user to select one option
52+
let selected_index = select("Select an option:", &options);
53+
54+
println!("Selected option: {}", options[selected_index]);
55+
```
56+
57+
#### Multiple Options
58+
```rust, no_run
59+
use console_utils::input::multiselect;
60+
let options = [
61+
"Option 1",
62+
"Option 2",
63+
"Option 3",
64+
];
65+
// Allow the user to select multiple options
66+
let selected_indices = multiselect("Select options:", &options);
67+
68+
println!("Selected indices: {:?}", selected_indices);
69+
```
70+
71+
### Console Control
72+
73+
```rust
74+
use console_utils::control::{flush, clear_line};
75+
// Flush the output buffer to ensure content is displayed immediately
76+
flush();
77+
// Clear the current line in the console
78+
clear_line();
79+
// and more...
80+
// Consult the docs for more details!
81+
```
82+
83+
### Reading Key
84+
85+
```rust, no_run
86+
use console_utils::read::{read_key};
87+
// Cross-platform key reading
88+
let key = read_key();
89+
println!("Pressed key: {:?}", key);
90+
```
91+
92+
### Displaying a Spinner
93+
94+
```rust
95+
use console_utils::input::{spinner, SpinnerType};
96+
// Display a standard spinner for 3 seconds
97+
spinner(3.0, SpinnerType::Standard);
98+
// Display a custom spinner for 2 seconds
99+
spinner(2.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
100+
```
101+
102+
### Gradual String Reveal
32103

33104
```rust
34-
use console_utils::{input::{input, spinner, SpinnerType, select, reveal}, control::clear_line};
35-
36-
fn main() {
37-
// Read user input as a string
38-
let user_input: Option<String> = input("Enter something: ", false);
39-
40-
match user_input {
41-
Some(value) => println!("You entered: {}", value),
42-
None => panic!("Input cannot be None when 'allow_empty' is set to false."),
43-
}
44-
45-
// Display a standard spinner for 3 seconds
46-
spinner(3.0, SpinnerType::Standard);
47-
48-
// Cross-platform key reading
49-
let key = read_key();
50-
51-
println!("Pressed key: {:?}", key);
52-
53-
let options = vec![
54-
"Option 1",
55-
"Option 2",
56-
"Option 3",
57-
];
58-
59-
// Allow the user to select one option
60-
let selected_indices = select("Select an option:", &options, false, false);
61-
62-
match selected_indices {
63-
Some(indices) => println!("Selected indices: {:?}", indices),
64-
None => panic!("The Options cannot be None, allow_empty is false."),
65-
}
66-
67-
// Display "Hello World!" with a time interval of 0.1 seconds between each character
68-
reveal("Hello World!", 0.1);
69-
70-
// Clear the current line in the console, so the "Hello World!"
71-
clear_line();
72-
}
105+
use console_utils::input::reveal;
106+
// Display "Hello World!" with a time interval of 0.1 seconds between each character
107+
reveal("Hello World!", 0.1);
73108
```
74109

75110
For more detailed documentation, please refer to the [generated Rust Docs](https://docs.rs/console-utils/latest/console_utils/).

src/control.rs

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
use std::io::{self, Write};
66

77
/// Flushes the output buffer, ensuring that all content is written to the console.
8-
///
9-
/// # Example
10-
///
11-
/// ```rust
12-
/// use console_utils::control::flush;
13-
///
14-
/// // Flush the output buffer to ensure content is displayed immediately
15-
/// flush();
16-
/// ```
178
pub fn flush() {
189
io::stdout().flush().unwrap();
1910
}
@@ -22,34 +13,47 @@ pub fn flush() {
2213
///
2314
/// This function uses ANSI escape codes to clear the entire line and move the cursor to the
2415
/// beginning of the line.
25-
///
26-
/// # Example
27-
///
28-
/// ```rust
29-
/// use console_utils::control::clear_line;
30-
///
31-
/// // Clear the current line
32-
/// clear_line();
33-
/// ```
3416
pub fn clear_line() {
3517
print!("\r\x1b[2K");
3618
flush();
3719
}
3820

21+
/// Clears the `i` lines in the console.
22+
pub fn clear_lines(i: usize) {
23+
for _ in 0..i {
24+
print!("\r\x1b[2K");
25+
flush();
26+
}
27+
}
28+
29+
/// Struct for ensuring and changing cursor visibility.
30+
pub struct Visibility;
31+
32+
impl Visibility {
33+
/// Hide the cursor via an ASCII escape sequence.
34+
pub fn hide_cursor() {
35+
print!("\x1B[?25l");
36+
flush();
37+
}
38+
39+
/// Show the cursor via an ASCII escape sequence.
40+
pub fn show_cursor() {
41+
print!("\x1B[?25h");
42+
flush();
43+
}
44+
}
45+
46+
impl Drop for Visibility {
47+
fn drop(&mut self) {
48+
Visibility::show_cursor();
49+
}
50+
}
51+
3952
/// Moves the cursor down by the specified number of lines.
4053
///
4154
/// # Arguments
4255
///
4356
/// * `n` - The number of lines to move the cursor down.
44-
///
45-
/// # Example
46-
///
47-
/// ```rust
48-
/// use console_utils::control::move_cursor_down;
49-
///
50-
/// // Move the cursor down by 2 lines
51-
/// move_cursor_down(2);
52-
/// ```
5357
pub fn move_cursor_down(n: usize) {
5458
if n > 0 {
5559
print!("\x1b[{}B", n);
@@ -62,15 +66,6 @@ pub fn move_cursor_down(n: usize) {
6266
/// # Arguments
6367
///
6468
/// * `n` - The number of lines to move the cursor up.
65-
///
66-
/// # Example
67-
///
68-
/// ```rust
69-
/// use console_utils::control::move_cursor_up;
70-
///
71-
/// // Move the cursor up by 3 lines
72-
/// move_cursor_up(3);
73-
/// ```
7469
pub fn move_cursor_up(n: usize) {
7570
if n > 0 {
7671
print!("\x1b[{}A", n);
@@ -83,15 +78,6 @@ pub fn move_cursor_up(n: usize) {
8378
/// # Arguments
8479
///
8580
/// * `n` - The number of characters to move the cursor to the left.
86-
///
87-
/// # Example
88-
///
89-
/// ```rust
90-
/// use console_utils::control::move_cursor_left;
91-
///
92-
/// // Move the cursor left by 4 characters
93-
/// move_cursor_left(4);
94-
/// ```
9581
pub fn move_cursor_left(n: usize) {
9682
if n > 0 {
9783
print!("\x1b[{}D", n);
@@ -104,15 +90,6 @@ pub fn move_cursor_left(n: usize) {
10490
/// # Arguments
10591
///
10692
/// * `n` - The number of characters to move the cursor to the right.
107-
///
108-
/// # Example
109-
///
110-
/// ```rust
111-
/// use console_utils::control::move_cursor_right;
112-
///
113-
/// // Move the cursor right by 5 characters
114-
/// move_cursor_right(5);
115-
/// ```
11693
pub fn move_cursor_right(n: usize) {
11794
if n > 0 {
11895
print!("\x1b[{}C", n);
@@ -126,15 +103,6 @@ pub fn move_cursor_right(n: usize) {
126103
///
127104
/// * `x` - The horizontal position (column) to move the cursor to.
128105
/// * `y` - The vertical position (row) to move the cursor to.
129-
///
130-
/// # Example
131-
///
132-
/// ```rust
133-
/// use console_utils::control::move_cursor_to;
134-
///
135-
/// // Move the cursor to column 3, row 5
136-
/// move_cursor_to(3, 5);
137-
/// ```
138106
pub fn move_cursor_to(x: usize, y: usize) {
139107
print!("\x1B[{};{}H", y + 1, x + 1);
140108
flush();

0 commit comments

Comments
 (0)