Skip to content

Commit d814c65

Browse files
committed
✨ Updated Structure and Documentation
1 parent 8b7fc3b commit d814c65

File tree

8 files changed

+564
-528
lines changed

8 files changed

+564
-528
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "console-utils"
3-
version = "1.5.3"
3+
version = "1.5.4"
44
edition = "2021"
55
authors = ["Nils Wrenger <[email protected]>"]
6-
description = "Cli input utilities."
6+
description = "Simple CLI Input and Control Utilities"
77
keywords = ["console", "terminal", "cli"]
88
categories = ["command-line-utilities", "cli"]
99
repository = "https://github.com/nwrenger/console-utils-rs"

README.md

Lines changed: 37 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,69 @@
1-
# Console Utility Library
1+
# Console Utils
22

33
[![Crate](https://img.shields.io/crates/v/console-utils.svg)](https://crates.io/crates/console-utils)
44
[![API](https://docs.rs/console-utils/badge.svg)](https://docs.rs/console-utils)
55

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._
77

8-
## Input Function
8+
# Overview
99

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

12-
### Usage
1312

14-
```rust
15-
use console_utils::input;
1613

17-
fn main() {
18-
// Prompt the user for input
19-
let user_input = input::<String>("Enter something: ", false);
14+
## Usage
2015

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"
2721
```
28-
## Select Function
2922

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:
3124

32-
### Usage
25+
```rust
26+
use console_utils::input::{input, select};
27+
use console_utils::control::{flush, clear_line};
28+
```
29+
30+
## Example
3331

3432
```rust
35-
use console_utils::select;
33+
use console_utils::input::{input, select, spinner, SpinnerType};
3634

3735
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+
3847
let options = vec![
3948
"Option 1",
4049
"Option 2",
4150
"Option 3",
4251
];
4352

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);
4655

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),
5458
None => panic!("The Options cannot be None, allow_empty is false."),
5559
}
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"]));
9360

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
11262
reveal("Hello World!", 0.1);
63+
64+
// Clear the current line in the console, so the "Hello World!"
65+
clear_line();
11366
}
11467
```
11568

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-
12869
For more detailed documentation, please refer to the [generated Rust Docs](https://docs.rs/console-utils/latest/console_utils/).

src/control.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//! Control Utilities
2+
//!
3+
//! This module provides functions for controlling the console, including flushing the output buffer,
4+
//! clearing lines, and moving the cursor in various directions.
5+
use std::io::{self, Write};
6+
7+
/// 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+
/// ```
17+
pub fn flush() {
18+
io::stdout().flush().unwrap();
19+
}
20+
21+
/// Clears the current line in the console.
22+
///
23+
/// This function uses ANSI escape codes to clear the entire line and move the cursor to the
24+
/// 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+
/// ```
34+
pub fn clear_line() {
35+
print!("\r\x1b[2K");
36+
flush();
37+
}
38+
39+
/// Moves the cursor down by the specified number of lines.
40+
///
41+
/// # Arguments
42+
///
43+
/// * `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+
/// ```
53+
pub fn move_cursor_down(n: usize) {
54+
if n > 0 {
55+
print!("\x1b[{}B", n);
56+
flush();
57+
}
58+
}
59+
60+
/// Moves the cursor up by the specified number of lines.
61+
///
62+
/// # Arguments
63+
///
64+
/// * `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+
/// ```
74+
pub fn move_cursor_up(n: usize) {
75+
if n > 0 {
76+
print!("\x1b[{}A", n);
77+
flush();
78+
}
79+
}
80+
81+
/// Moves the cursor to the left by the specified number of characters.
82+
///
83+
/// # Arguments
84+
///
85+
/// * `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+
/// ```
95+
pub fn move_cursor_left(n: usize) {
96+
if n > 0 {
97+
print!("\x1b[{}D", n);
98+
flush();
99+
}
100+
}
101+
102+
/// Moves the cursor to the right by the specified number of characters.
103+
///
104+
/// # Arguments
105+
///
106+
/// * `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+
/// ```
116+
pub fn move_cursor_right(n: usize) {
117+
if n > 0 {
118+
print!("\x1b[{}C", n);
119+
flush();
120+
}
121+
}
122+
123+
/// Moves the cursor to the specified position on the console.
124+
///
125+
/// # Arguments
126+
///
127+
/// * `x` - The horizontal position (column) to move the cursor to.
128+
/// * `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+
/// ```
138+
pub fn move_cursor_to(x: usize, y: usize) {
139+
print!("\x1B[{};{}H", y + 1, x + 1);
140+
flush();
141+
}

0 commit comments

Comments
 (0)