Skip to content

Commit 1edbb5d

Browse files
committed
✨ Made Input Generic and Fixed Docs
1 parent 1978513 commit 1edbb5d

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
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.1.9"
3+
version = "1.1.10"
44
edition = "2021"
55
authors = ["Nils Wrenger <[email protected]>"]
66
description = "Cli input utilities."

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A simple Rust library for console-based user input, option selection and more.
77

88
## Input Function
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<String>`.
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>`.
1111

1212
### Usage
1313

@@ -16,12 +16,12 @@ use console_utils::input;
1616

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

2121
// Process the user input
2222
match user_input {
2323
Some(value) => println!("You entered: {}", value),
24-
None => println!("Input is empty."),
24+
None => panic!("The Input cannot be None, allow_empty is false."),
2525
}
2626
}
2727
```
@@ -52,7 +52,7 @@ fn main() {
5252
println!("Option {} selected: {}", i + 1, selected);
5353
}
5454
}
55-
None => println!("No options selected."),
55+
None => panic!("The Options cannot be None, allow_empty is false."),
5656
}
5757
}
5858
```

src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ use std::{
2121
///
2222
/// # Returns
2323
///
24-
/// Returns an `Option<String>` containing the user's input or `None` if the input is empty and
25-
/// `allow_empty` is `false`.
24+
/// Returns an `Option<T>` containing the user's input converted to the specified type,
25+
/// or `None` if the input is empty and `allow_empty` is `true`.
2626
///
2727
/// # Example
2828
///
2929
/// ```no_run
3030
/// use console_utils::input;
3131
///
32-
/// let user_input = input("Enter something: ", false, false);
32+
/// let user_input = input::<String>("Enter something: ", false, false);
3333
///
3434
/// match user_input {
3535
/// Some(value) => println!("You entered: {}", value),
36-
/// None => println!("Input is empty."),
36+
/// None => panic!("The Input cannot be None, allow_empty is false."),
3737
/// }
3838
/// ```
39-
pub fn input(before: &str, allow_empty: bool, new_line: bool) -> Option<String> {
39+
pub fn input<T>(before: &str, allow_empty: bool, new_line: bool) -> Option<T>
40+
where
41+
T: std::str::FromStr,
42+
T::Err: std::fmt::Debug,
43+
{
4044
loop {
4145
print!("{before} {}", if new_line { '\n' } else { '\0' });
4246
io::stdout().flush().unwrap();
@@ -47,7 +51,10 @@ pub fn input(before: &str, allow_empty: bool, new_line: bool) -> Option<String>
4751
if allow_empty && cli.trim().is_empty() {
4852
return None;
4953
} else if !cli.trim().is_empty() {
50-
return Some(cli.trim().to_owned());
54+
match cli.trim().parse() {
55+
Ok(value) => return Some(value),
56+
Err(_) => println!("\nWrong Input Type\n"),
57+
}
5158
} else {
5259
println!("\nWrong Input\n");
5360
}
@@ -71,7 +78,7 @@ pub fn input(before: &str, allow_empty: bool, new_line: bool) -> Option<String>
7178
/// # Returns
7279
///
7380
/// Returns an `Option<Vec<bool>>` containing a vector of booleans indicating which options were
74-
/// selected. Returns `None` if no option was selected and `allow_empty` is `false`.
81+
/// selected. Returns `None` if no option was selected and `allow_empty` is `true`.
7582
///
7683
/// # Example
7784
///
@@ -163,7 +170,7 @@ pub fn select(
163170
reset(stdout, "", options.len());
164171
return Some(matrix);
165172
} else {
166-
reset(stdout, "\nWrong Input\n", options.len());
173+
reset(stdout, "\nPlease Select any option!\n", options.len());
167174
}
168175
}
169176
}

tests/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use console_utils::{input, select, spinner, SpinnerType};
55
#[ignore]
66
fn test_input() {
77
// Run the function
8-
let result = input("Enter something: ", false, false);
8+
let result = input::<u8>("Enter something: ", false, false);
99

10-
// Put in Hello
10+
// Input anything
1111

1212
// Check the result
13-
assert_eq!(result, Some("Hello".to_owned()));
13+
assert!(result.is_some());
1414
}
1515

1616
#[test]
@@ -22,7 +22,7 @@ fn test_select() {
2222
// select the first option using spacebar and click enter
2323

2424
// Check the result
25-
assert_eq!(result, Some(vec![true]));
25+
assert!(result.is_some());
2626
}
2727

2828
#[test]

0 commit comments

Comments
 (0)