Skip to content

Commit 47166b5

Browse files
committed
✨ Minor Code and Documentation refactor
1 parent 7e57770 commit 47166b5

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-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.1.7"
3+
version = "1.1.8"
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
@@ -72,8 +72,8 @@ spinner(3.0, SpinnerType::Standard);
7272
// Display a dots spinner for 2 seconds
7373
spinner(2.0, SpinnerType::Dots);
7474

75-
// Display a custom spinner for 1 second (using a custom frame)
76-
spinner(1.0, SpinnerType::Custom("@ "));
75+
// Display a custom spinner for 1 second (using custom frames)
76+
spinner(1.0, SpinnerType::Custom(vec!["1", "2", "3", "4", "3", "2"]));
7777

7878
// Display a box spinner for 1.5 seconds
7979
spinner(1.5, SpinnerType::Box);

src/lib.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,34 @@ pub enum SpinnerType {
185185
Custom(Vec<&'static str>),
186186
}
187187

188+
impl SpinnerType {
189+
/// Converts the spinner type to a vector of frames.
190+
///
191+
/// # Returns
192+
///
193+
/// Returns a vector of strings representing frames for the spinner, gives back the following variants:
194+
/// - `SpinnerType::Standard`: Standard spinner with characters / - \ |.
195+
/// - `SpinnerType::Dots`: Spinner with dots . .. ... .....
196+
/// - `SpinnerType::Box`: Spinner with box characters ▌ ▀ ▐ ▄.
197+
/// - `SpinnerType::Flip`: Spinner with flip characters _ _ _ - \ ' ´ - _ _ _.
198+
/// - `SpinnerType::Custom(frames)`: Custom spinner with user-defined frames.
199+
fn to_frames(&self) -> Vec<&'static str> {
200+
match self {
201+
SpinnerType::Standard => vec!["/", "-", "\\", "|"],
202+
SpinnerType::Dots => vec![".", "..", "...", "....", "...", ".."],
203+
SpinnerType::Box => vec!["▌", "▀", "▐", "▄"],
204+
SpinnerType::Flip => vec!["_", "_", "_", "-", "`", "`", "'", "´", "-", "_", "_", "_"],
205+
SpinnerType::Custom(frames) => frames.to_owned(),
206+
}
207+
}
208+
}
209+
188210
/// Displays a console-based spinner animation.
189211
///
190212
/// # Parameters
191213
///
192214
/// - `time`: A floating-point number representing the duration of the spinner animation in seconds.
193-
/// - `spinner_type`: The type of spinner to display, which can be one of the following:
194-
/// - `SpinnerType::Standard`: Standard spinner with characters / - \ |.
195-
/// - `SpinnerType::Dots`: Spinner with dots . .. ... .....
196-
/// - `SpinnerType::Box`: Spinner with box characters ▌ ▀ ▐ ▄.
197-
/// - `SpinnerType::Flip`: Spinner with flip characters _ _ _ - \ ' ´ - _ _ _.
198-
/// - `SpinnerType::Custom(frame)`: Custom spinner with a user-defined frame.
215+
/// - `spinner_type`: The type of spinner to display.
199216
///
200217
/// # Example
201218
///
@@ -210,24 +227,18 @@ pub enum SpinnerType {
210227
/// ```
211228
pub fn spinner(mut time: f64, spinner_type: SpinnerType) {
212229
let stdout = Term::buffered_stdout();
230+
let frames = spinner_type.to_frames();
213231
let mut i = 0;
214232

215233
while time > 0.0 {
216234
stdout.clear_line().unwrap();
217-
let frame = match spinner_type {
218-
SpinnerType::Standard => vec!["/", "-", "\\", "|"],
219-
SpinnerType::Dots => vec![".", "..", "...", "....", "...", ".."],
220-
SpinnerType::Box => vec!["▌", "▀", "▐", "▄"],
221-
SpinnerType::Flip => vec!["_", "_", "_", "-", "`", "`", "'", "´", "-", "_", "_", "_"],
222-
SpinnerType::Custom(ref custom_frame) => custom_frame.to_vec(),
223-
};
224-
stdout.write_line(frame[i]).unwrap();
235+
stdout.write_line(frames[i]).unwrap();
225236
stdout.move_cursor_up(1).unwrap();
226-
stdout.move_cursor_right(frame[i].len()).unwrap();
237+
stdout.move_cursor_right(frames[i].len()).unwrap();
227238
stdout.flush().unwrap();
228239
thread::sleep(Duration::from_secs_f64(0.075));
229240
time -= 0.075;
230-
if i < frame.len() - 1 {
241+
if i < frames.len() - 1 {
231242
i += 1
232243
} else {
233244
i = 0

tests/test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ fn test_select() {
2626
}
2727

2828
#[test]
29-
#[ignore]
3029
fn test_spinner() {
3130
// Give the fn the needed time and SpinnerType
3231
spinner(4.2, SpinnerType::Standard);

0 commit comments

Comments
 (0)