Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ include = ["src/**/*", "README.md"]
lazy_static = { version = "1.4.0" }
maplit = { version = "1.0.2" }
strum = { version = "0.24.0", features = ["derive"] }
yansi = { version = "0.5.1" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::thread::sleep;
use std::time::Duration;

fn main() {
let mut sp = Spinner::new(Spinners::Dots9, "Waiting for 3 seconds".into());
let mut sp = Spinner::new(Spinners::Dots9, "Waiting for 3 seconds".into(), None);
sleep(Duration::from_secs(3));
sp.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use strum::IntoEnumIterator;
fn main() {
// loop through each spinner and display them for 2 seconds
for spinner in Spinners::iter() {
let mut sp = Spinner::new(spinner.clone(), format!("{:?}", spinner));
let mut sp = Spinner::new(spinner.clone(), format!("{:?}", spinner), None);
sleep(Duration::from_secs(2));
sp.stop();
}
Expand Down
3 changes: 2 additions & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use spinners::{Spinner, Spinners};
use spinners::{Color, Spinner, Spinners};
use std::{env, str::FromStr, thread::sleep, time::Duration};

fn main() {
Expand All @@ -8,6 +8,7 @@ fn main() {
let mut sp = Spinner::new(
Spinners::from_str(&spinner_name).unwrap(),
"Waiting for 3 seconds".into(),
Color::Green,
);
sleep(Duration::from_secs(3));
sp.stop_with_message("Finishing waiting for 3 seconds\n".into());
Expand Down
1 change: 1 addition & 0 deletions examples/stop_persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
let mut sp = Spinner::new(
Spinners::from_str(&spinner_name).unwrap(),
"Waiting for 3 seconds".into(),
None,
);
sleep(Duration::from_secs(3));
sp.stop_and_persist("✔", "That worked!".to_string())
Expand Down
1 change: 1 addition & 0 deletions examples/stop_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
let mut sp = Spinner::new(
Spinners::from_str(&spinner_name).unwrap(),
"Waiting for 3 seconds".into(),
None,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ad4mx can we keep our backward compatibility and make the parameter color with a default none value? so the upgrade of spinners will be painless?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, this is impossible in Rust, because you have to supply all needed arguments. I'll look more into it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I think moving it to a dedicated function would be a better idea then. I've also edited the code according to clippy.

);
sleep(Duration::from_secs(3));
sp.stop_with_symbol("\x1b[32m🗸\x1b[0m");
Expand Down
1 change: 1 addition & 0 deletions examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
let mut sp = Spinner::with_timer(
Spinners::from_str(&spinner_name).unwrap(),
"Waiting for 3 seconds".into(),
None,
);
sleep(Duration::from_secs(3));
sp.stop_with_newline();
Expand Down
38 changes: 21 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::{

pub use crate::utils::spinner_names::SpinnerNames as Spinners;
use crate::utils::spinners_data::SPINNERS as SpinnersMap;

pub use crate::utils::color::Color;
use crate::utils::color::colorize;
mod utils;

pub struct Spinner {
Expand All @@ -34,28 +35,31 @@ impl Spinner {
/// Basic Usage:
///
/// ```
/// use spinners::{Spinner, Spinners};
/// use spinners::{Spinner, Spinners, Color};
///
/// let sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), Some(Color::Blue));
/// ```
///
/// No Message:
///
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let sp = Spinner::new(Spinners::Dots, String::new());
/// let sp = Spinner::new(Spinners::Dots, String::new(), None);
/// ```
pub fn new(spinner: Spinners, message: String) -> Self {
Self::new_inner(spinner, message, None)
pub fn new<T>(spinner: Spinners, message: String, color: T) -> Self
where T: Into<Option<Color>> + std::marker::Send + 'static + std::marker::Copy {
Self::new_inner(spinner, message, color, None)
}

/// Create a new spinner that logs the time since it was created
pub fn with_timer(spinner: Spinners, message: String) -> Self {
Self::new_inner(spinner, message, Some(Instant::now()))
pub fn with_timer<T>(spinner: Spinners, message: String, color: T) -> Self
where T: Into<Option<Color>> + std::marker::Send + 'static + std::marker::Copy {
Self::new_inner(spinner, message, color, Some(Instant::now()))
}

fn new_inner(spinner: Spinners, message: String, start_time: Option<Instant>) -> Self {
fn new_inner<T>(spinner: Spinners, message: String, color: T, start_time: Option<Instant>) -> Self
where T: Into<Option<Color>> + std::marker::Send + 'static + std::marker::Copy {
let spinner_name = spinner.to_string();
let spinner_data = SpinnersMap
.get(&spinner_name)
Expand All @@ -75,12 +79,12 @@ impl Spinner {
let frame = stop_symbol.unwrap_or_else(|| frame.to_string());
match start_time {
None => {
print!("\r{} {}", frame, message);
print!("\r{} {}", colorize(frame, color.into()), message);
}
Some(start_time) => {
let now = stop_time.unwrap_or_else(Instant::now);
let duration = now.duration_since(start_time).as_secs_f64();
print!("\r{}{:>10.3} s\t{}", frame, duration, message);
print!("\r{}{:>10.3} s\t{}", colorize(frame, color.into()), duration, message);
}
}

Expand Down Expand Up @@ -118,7 +122,7 @@ impl Spinner {
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), None);
///
/// sp.stop();
/// ```
Expand All @@ -137,7 +141,7 @@ impl Spinner {
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), None);
///
/// sp.stop_with_symbol("🗸");
/// ```
Expand All @@ -147,7 +151,7 @@ impl Spinner {
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), None);
///
/// sp.stop_with_symbol("\x1b[32m🗸\x1b[0m");
/// ```
Expand All @@ -165,7 +169,7 @@ impl Spinner {
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), None);
///
/// sp.stop_with_newline();
/// ```
Expand All @@ -183,7 +187,7 @@ impl Spinner {
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), None);
///
/// sp.stop_with_message("Finished loading things into memory!".into());
/// ```
Expand All @@ -201,7 +205,7 @@ impl Spinner {
/// ```
/// use spinners::{Spinner, Spinners};
///
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into());
/// let mut sp = Spinner::new(Spinners::Dots, "Loading things into memory...".into(), None);
///
/// sp.stop_and_persist("✔", "Finished loading things into memory!".into());
/// ```
Expand Down
27 changes: 27 additions & 0 deletions src/utils/color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use yansi::Paint;

#[derive(Debug, Clone, Copy)]
pub enum Color {
Blue,
Green,
Red,
Yellow,
Cyan,
White,
Magenta,
Black,
}

pub fn colorize(input: String, color: Option<Color>) -> Paint<String> {
match color {
Some(Color::Blue) => Paint::blue(input),
Some(Color::Green) => Paint::green(input),
Some(Color::Red) => Paint::red(input),
Some(Color::Yellow) => Paint::yellow(input),
Some(Color::Cyan) => Paint::cyan(input),
Some(Color::White) => Paint::new(input),
Some(Color::Magenta) => Paint::magenta(input),
Some(Color::Black) => Paint::black(input),
None => Paint::new(input),
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod color;
pub mod spinner_data;
pub mod spinner_names;
pub mod spinners_data;