|
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. |
2 | 2 |
|
3 | 3 | use console::{style, Key, Term}; |
4 | 4 | use std::{ |
@@ -251,3 +251,39 @@ pub fn spinner(mut time: f64, spinner_type: SpinnerType) { |
251 | 251 | stdout.clear_line().unwrap(); |
252 | 252 | stdout.flush().unwrap(); |
253 | 253 | } |
| 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 | +} |
0 commit comments