Skip to content

Commit 776c449

Browse files
committed
✨ Refactored input and reveal function
1 parent eca3233 commit 776c449

File tree

5 files changed

+15
-21
lines changed

5 files changed

+15
-21
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.2.2"
3+
version = "1.2.3"
44
edition = "2021"
55
authors = ["Nils Wrenger <[email protected]>"]
66
description = "Cli input utilities."

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use console_utils::input;
1616

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

2121
// Process the user input
2222
match user_input {
@@ -92,7 +92,7 @@ Displays a string gradually, revealing one character at a time with a specified
9292
use console_utils::reveal;
9393

9494
// Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished.
95-
reveal("Hello World!", 0.1, true);
95+
reveal("Hello World!", 0.1);
9696
```
9797

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

src/lib.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ use std::{
1515
///
1616
/// # Arguments
1717
///
18-
/// * `before` - The text to display before prompting for input.
18+
/// * `before` - The text to display before prompting for input. Add here `\n` for a new line.
1919
/// * `allow_empty` - If true, allows the input to be empty.
20-
/// * `new_line` - If true, adds a newline character after the prompt.
2120
///
2221
/// # Returns
2322
///
@@ -29,20 +28,20 @@ use std::{
2928
/// ```no_run
3029
/// use console_utils::input;
3130
///
32-
/// let user_input = input::<String>("Enter something: ", false, false);
31+
/// let user_input = input::<String>("Enter something: ", false);
3332
///
3433
/// match user_input {
3534
/// Some(value) => println!("You entered: {}", value),
3635
/// None => panic!("The Input cannot be None, allow_empty is false."),
3736
/// }
3837
/// ```
39-
pub fn input<T>(before: &str, allow_empty: bool, new_line: bool) -> Option<T>
38+
pub fn input<T>(before: &str, allow_empty: bool) -> Option<T>
4039
where
4140
T: std::str::FromStr,
4241
T::Err: std::fmt::Debug,
4342
{
4443
loop {
45-
print!("{before} {}", if new_line { '\n' } else { '\0' });
44+
print!("{before}");
4645
io::stdout().flush().unwrap();
4746

4847
let mut cli = String::new();
@@ -259,26 +258,21 @@ pub fn spinner(mut time: f64, spinner_type: SpinnerType) {
259258
///
260259
/// # Arguments
261260
///
262-
/// * `str` - The string to reveal gradually.
261+
/// * `str` - The string to reveal gradually. Add here `\n` for a new line.
263262
/// * `time_between` - The time interval (in seconds) between each revealed character.
264-
/// * `new_line` - If true, adds a newline character after the revelation.
265263
///
266264
/// # Example
267265
///
268266
/// ```rust
269267
/// use console_utils::reveal;
270268
///
271269
/// // Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished.
272-
/// reveal("Hello World!", 0.1, true);
270+
/// reveal("Hello World!", 0.1);
273271
/// ```
274-
pub fn reveal(str: &str, time_between: f64, new_line: bool) {
272+
pub fn reveal(str: &str, time_between: f64) {
275273
for i in 0..str.len() {
276-
print!("{}", str.chars().nth(i).unwrap());
274+
print!("{:?}", str.chars().nth(i).unwrap());
277275
io::stdout().flush().unwrap();
278276
thread::sleep(Duration::from_secs_f64(time_between));
279277
}
280-
281-
if new_line {
282-
println!();
283-
}
284278
}

tests/test.rs

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

1010
// Input anything
1111

@@ -36,6 +36,6 @@ fn test_spinner() {
3636

3737
#[test]
3838
fn test_reveal() {
39-
// Give the fn the str and time, prints it delayed with a new line.
40-
reveal("Hello World!", 0.1, true);
39+
// Give the fn the str and time.
40+
reveal("Hello World!\n", 0.1);
4141
}

0 commit comments

Comments
 (0)