Skip to content

Commit f9862b5

Browse files
committed
✨ Added reveal function
1 parent 1edbb5d commit f9862b5

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
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.10"
3+
version = "1.2.0"
44
edition = "2021"
55
authors = ["Nils Wrenger <[email protected]>"]
66
description = "Cli input utilities."

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@ spinner(1.5, SpinnerType::Box);
8282
spinner(2.0, SpinnerType::Flip);
8383
```
8484

85+
## Reveal Function
86+
87+
Displays a string gradually, revealing one character at a time with a specified time interval between each character.
88+
89+
### Usage
90+
91+
```rust
92+
use console_utils::reveal;
93+
94+
// Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished.
95+
reveal(0.1, "Hello World!", true);
96+
```
97+
8598
For more detailed documentation, please refer to the [generated Rust Docs](https://docs.rs/console-utils/latest/console_utils/).

src/lib.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A simple library for console-based user input and option selection.
1+
//! A simple library for console-based user input, option selection and more.
22
33
use console::{style, Key, Term};
44
use std::{
@@ -251,3 +251,39 @@ pub fn spinner(mut time: f64, spinner_type: SpinnerType) {
251251
stdout.clear_line().unwrap();
252252
stdout.flush().unwrap();
253253
}
254+
255+
/// Reveal Function
256+
///
257+
/// Displays a string gradually, revealing one character at a time with a specified time interval
258+
/// between each character.
259+
///
260+
/// # Arguments
261+
///
262+
/// * `time_between` - The time interval (in seconds) between each revealed character.
263+
/// * `str` - The string to reveal gradually.
264+
/// * `new_line` - If true, adds a newline character after the revelation.
265+
///
266+
/// # Example
267+
///
268+
/// ```rust
269+
/// use console_utils::reveal;
270+
///
271+
/// // Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished.
272+
/// reveal(0.1, "Hello World!", true);
273+
/// ```
274+
pub fn reveal(time_between: f64, str: &str, new_line: bool) {
275+
let stdout = Term::buffered_stdout();
276+
277+
for i in 0..str.len() {
278+
stdout.clear_line().unwrap();
279+
stdout.write_line(str.get(0..=i).unwrap()).unwrap();
280+
stdout.move_cursor_up(1).unwrap();
281+
stdout.move_cursor_right(i + 1).unwrap();
282+
stdout.flush().unwrap();
283+
thread::sleep(Duration::from_secs_f64(time_between));
284+
}
285+
286+
if new_line {
287+
println!();
288+
}
289+
}

tests/test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import the functions to be tested from the crate root
2-
use console_utils::{input, select, spinner, SpinnerType};
2+
use console_utils::{input, reveal, select, spinner, SpinnerType};
33

44
#[test]
55
#[ignore]
@@ -33,3 +33,9 @@ fn test_spinner() {
3333
// Custom Spinner
3434
spinner(1.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]))
3535
}
36+
37+
#[test]
38+
fn test_reveal() {
39+
// Give the fn the str and time, prints it delayed with a new line.
40+
reveal(0.1, "Hello World!", true);
41+
}

0 commit comments

Comments
 (0)