Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .rustfmt.toml #16

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .rustfmt.toml
@@ -0,0 +1 @@
max_width = 100
7 changes: 6 additions & 1 deletion examples/stream.rs
Expand Up @@ -2,7 +2,12 @@ use spinoff::{Spinner, Spinners, Streams};
use std::{thread::sleep, time::Duration};

fn main() {
let sp = Spinner::new_with_stream(Spinners::Aesthetic, "Loading in stderr...", None, Streams::Stderr);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example, this line has 106 characters, so it still gets re-aligned.
I think a max width exceeding 100 is not ideal, so some of the realignments are inescapable.
What do you think?

Copy link
Owner

Choose a reason for hiding this comment

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

It's fine if it gets re-aligned in files like these, which are just examples. It's just a bit more unreadable when it's in the actual internal code itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I guess we don't need to do the formatting stuff.

let sp = Spinner::new_with_stream(
Spinners::Aesthetic,
"Loading in stderr...",
None,
Streams::Stderr,
);
sleep(Duration::from_millis(8000));
sp.success("Done!");
}
70 changes: 45 additions & 25 deletions src/lib.rs
Expand Up @@ -35,20 +35,20 @@ use colored::Colorize;
use std::borrow::Cow;
use std::io::Write;
use std::sync::{atomic::AtomicBool, Arc};
use std::thread::sleep;
use std::thread::{self, JoinHandle};
use std::time::Duration;
use std::thread::sleep;

mod utils;
mod spinner_data;
mod spinner_enum;
mod streams;
mod utils;

pub use utils::Color;
use utils::{colorize, delete_last_line};
use spinner_data::SPINNER_FRAMES;
pub use spinner_enum::Spinners;
pub use streams::Streams;
pub use utils::Color;
use utils::{colorize, delete_last_line};

/// Terminal spinner.
#[derive(Debug)]
Expand Down Expand Up @@ -160,9 +160,9 @@ impl Spinner {
.flush()
.expect("error: failed to flush stream");

thread::sleep(std::time::Duration::from_millis(
u64::from(spinner_data.interval)
));
thread::sleep(std::time::Duration::from_millis(u64::from(
spinner_data.interval,
)));
}
delete_last_line(last_length, stream);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Spinner {

/**
Stops the spinner and prints a message on a new line.

# Example

```
Expand Down Expand Up @@ -256,7 +256,7 @@ impl Spinner {
# use spinoff::{Spinners, Spinner};
# use std::thread::sleep;
# use std::time::Duration;
#
#
let sp = Spinner::new(Spinners::Aesthetic, "Trying to load information...", None);
sleep(Duration::from_millis(800));
sp.success("Success!");
Expand All @@ -265,19 +265,24 @@ impl Spinner {
*/
pub fn success(mut self, msg: &str) {
self.stop_spinner_thread();
writeln!(self.stream, "{} {}", colorize(Some(Color::Green), "✓").bold(), msg);
writeln!(
self.stream,
"{} {}",
colorize(Some(Color::Green), "✓").bold(),
msg
);
}

/**
Deletes the last line of the terminal and prints a failure symbol with a message to stderr.

# Example

```
# use spinoff::{Spinners, Spinner, Color};
# use std::thread::sleep;
# use std::time::Duration;
#
#
let sp = Spinner::new(Spinners::BouncingBar, "Executing code...", Color::Green);
sleep(Duration::from_millis(800));
sp.fail("Code failed to compile!");
Expand All @@ -286,19 +291,24 @@ impl Spinner {
*/
pub fn fail(mut self, msg: &str) {
self.stop_spinner_thread();
writeln!(self.stream, "{} {}", colorize(Some(Color::Red), "✗").bold(), msg);
writeln!(
self.stream,
"{} {}",
colorize(Some(Color::Red), "✗").bold(),
msg
);
}

/**
Deletes the last line of the terminal and prints a warning symbol with a message.

# Example

```
# use spinoff::{Spinners, Spinner};
# use std::thread::sleep;
# use std::time::Duration;
#
#
let sp = Spinner::new(Spinners::Material, "Measuring network speed...", None);
sleep(Duration::from_millis(800));
sp.warn("You might want to check your internet connection...");
Expand All @@ -307,7 +317,12 @@ impl Spinner {
*/
pub fn warn(mut self, msg: &str) {
self.stop_spinner_thread();
writeln!(self.stream, "{} {}", colorize(Some(Color::Yellow), "⚠").bold(), msg);
writeln!(
self.stream,
"{} {}",
colorize(Some(Color::Yellow), "⚠").bold(),
msg
);
}
/**
Deletes the last line of the terminal and prints an info symbol with a message.
Expand All @@ -318,16 +333,21 @@ impl Spinner {
# use spinoff::{Spinners, Spinner};
# use std::thread::sleep;
# use std::time::Duration;

let sp = Spinner::new(Spinners::Dots9, "Loading info message...", None);
sleep(Duration::from_millis(800));
sp.info("This is an info message!");
sp.info("This is an info message!");
```

*/
pub fn info(mut self, msg: &str) {
self.stop_spinner_thread();
writeln!(self.stream, "{} {}", colorize(Some(Color::Blue), "ℹ").bold(), msg);
writeln!(
self.stream,
"{} {}",
colorize(Some(Color::Blue), "ℹ").bold(),
msg
);
}

/**
Expand All @@ -339,7 +359,7 @@ impl Spinner {
# use spinoff::*;
# use std::thread::sleep;
# use std::time::Duration;
#
#
let mut sp = Spinner::new(Spinners::Dots, "Hello", None);

sleep(Duration::from_millis(800));
Expand Down Expand Up @@ -412,20 +432,20 @@ impl Spinner {
* This could be used to assure the user that the program is still running.

*/
pub fn update_after_time<T>(&mut self, updated_msg: T, duration: Duration)
pub fn update_after_time<T>(&mut self, updated_msg: T, duration: Duration)
where
T: Into<Cow<'static, str>>
T: Into<Cow<'static, str>>,
{
sleep(duration);
self.stop_spinner_thread();
let _ = std::mem::replace(
self,
Self::new_with_stream(self.spinner_type, updated_msg, self.color, self.stream)
self,
Self::new_with_stream(self.spinner_type, updated_msg, self.color, self.stream),
);
}
/**
Deletes the last line of the terminal.

# Example

```
Expand Down
1 change: 0 additions & 1 deletion src/streams.rs
Expand Up @@ -24,5 +24,4 @@ impl Streams {
{
write!(self.get_stream(), "{}", fmt).expect("error: failed to write to stream");
}

}
4 changes: 1 addition & 3 deletions src/utils.rs
Expand Up @@ -27,7 +27,7 @@ pub fn colorize(color: Option<Color>, frame: &str) -> ColoredString {
Some(Color::Black) => frame.black(),
Some(Color::Magenta) => frame.magenta(),
Some(Color::TrueColor { r, g, b }) => frame.truecolor(r, g, b),
None => frame.normal()
None => frame.normal(),
}
}

Expand All @@ -40,5 +40,3 @@ pub fn delete_last_line(clear_length: usize, stream: Streams) {
}
write!(stream, "\r");
}