Skip to content

Commit 46cc87e

Browse files
committed
✨ Added clear line function && fixed up docs
1 parent 0a47a4a commit 46cc87e

File tree

5 files changed

+87
-32
lines changed

5 files changed

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

README.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,22 @@ The spinner function creates a console-based spinner animation, offering a visua
6565

6666
```rust
6767
use console_utils::{spinner, SpinnerType};
68-
69-
// Display a standard spinner for 3 seconds
70-
spinner(3.0, SpinnerType::Standard);
71-
72-
// Display a dots spinner for 2 seconds
73-
spinner(2.0, SpinnerType::Dots);
74-
75-
// Display a custom spinner for 1 second (using custom frames)
76-
spinner(1.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
77-
78-
// Display a box spinner for 1.5 seconds
79-
spinner(1.5, SpinnerType::Box);
80-
81-
// Display a flip spinner for 2 seconds
82-
spinner(2.0, SpinnerType::Flip);
68+
fn main() {
69+
// Display a standard spinner for 3 seconds
70+
spinner(3.0, SpinnerType::Standard);
71+
72+
// Display a dots spinner for 2 seconds
73+
spinner(2.0, SpinnerType::Dots);
74+
75+
// Display a custom spinner for 1 second (using custom frames)
76+
spinner(1.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
77+
78+
// Display a box spinner for 1.5 seconds
79+
spinner(1.5, SpinnerType::Box);
80+
81+
// Display a flip spinner for 2 seconds
82+
spinner(2.0, SpinnerType::Flip);
83+
}
8384
```
8485

8586
## Reveal Function
@@ -90,9 +91,27 @@ Displays a string gradually, revealing one character at a time with a specified
9091

9192
```rust
9293
use console_utils::reveal;
94+
fn main() {
95+
// Display "Hello World!" with a time interval of 0.1 seconds between each character and a new line after it's finished.
96+
reveal("Hello World!", 0.1);
97+
}
98+
```
99+
100+
## Clear Line Function
101+
102+
Clears the current line in the console.
93103

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("Hello World!", 0.1);
104+
This function uses ANSI escape codes to clear the entire line and move the cursor to the
105+
beginning of the line.
106+
107+
### Usage
108+
109+
```rust
110+
use console_utils::clear_line;
111+
fn main() {
112+
// Clear the current line
113+
clear_line();
114+
}
96115
```
97116

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

src/lib.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ impl SpinnerType {
211211

212212
/// Displays a console-based spinner animation.
213213
///
214+
/// A spinner is a visual indicator of a long-running process. It consists of a set of frames
215+
/// that are displayed sequentially to create the appearance of motion.
216+
///
214217
/// # Parameters
215218
///
216219
/// - `time`: A floating-point number representing the duration of the spinner animation in seconds.
@@ -228,16 +231,13 @@ impl SpinnerType {
228231
/// spinner(2.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
229232
/// ```
230233
pub fn spinner(mut time: f64, spinner_type: SpinnerType) {
231-
let stdout = Term::buffered_stdout();
232234
let frames = spinner_type.to_frames();
233235
let mut i = 0;
234236

235237
while time > 0.0 {
236-
stdout.clear_line().unwrap();
237-
stdout.write_line(frames[i]).unwrap();
238-
stdout.move_cursor_up(1).unwrap();
239-
stdout.move_cursor_right(frames[i].len()).unwrap();
240-
stdout.flush().unwrap();
238+
clear_line();
239+
print!("{}", frames[i]);
240+
io::stdout().flush().unwrap();
241241
thread::sleep(Duration::from_secs_f64(0.075));
242242
time -= 0.075;
243243
if i < frames.len() - 1 {
@@ -247,14 +247,30 @@ pub fn spinner(mut time: f64, spinner_type: SpinnerType) {
247247
}
248248
}
249249

250-
stdout.clear_line().unwrap();
251-
stdout.flush().unwrap();
250+
clear_line();
251+
}
252+
253+
/// Clears the current line in the console.
254+
///
255+
/// This function uses ANSI escape codes to clear the entire line and move the cursor to the
256+
/// beginning of the line.
257+
///
258+
/// # Example
259+
///
260+
/// ```rust
261+
/// use console_utils::clear_line;
262+
///
263+
/// // Clear the current line
264+
/// clear_line();
265+
/// ```
266+
pub fn clear_line() {
267+
print!("\r\x1b[2K");
268+
io::stdout().flush().unwrap();
252269
}
253270

254-
/// Reveal Function
271+
/// Reveals a string gradually, printing one character at a time with a specified time interval.
255272
///
256-
/// Displays a string gradually, revealing one character at a time with a specified time interval
257-
/// between each character.
273+
/// This function is useful for creating a typing effect or slowly displaying information to the user.
258274
///
259275
/// # Arguments
260276
///

tests/test.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
use std::{
2+
io::{self, Write},
3+
thread,
4+
time::Duration,
5+
};
6+
17
// Import the functions to be tested from the crate root
2-
use console_utils::{input, reveal, select, spinner, SpinnerType};
8+
use console_utils::{clear_line, input, reveal, select, spinner, SpinnerType};
39

410
#[test]
511
#[ignore]
@@ -37,5 +43,19 @@ fn test_spinner() {
3743
#[test]
3844
fn test_reveal() {
3945
// Give the fn the str and time.
40-
reveal("Hello World!\n", 0.1);
46+
reveal("Hello World!", 0.1);
47+
}
48+
49+
#[test]
50+
fn test_clear() {
51+
// Print Something.
52+
print!("Hello World");
53+
// Force update the terminal
54+
io::stdout().flush().unwrap();
55+
56+
// wait
57+
thread::sleep(Duration::from_secs_f64(1.0));
58+
59+
// Clear the current line.
60+
clear_line();
4161
}

0 commit comments

Comments
 (0)