fix: fix README

This commit is contained in:
ad4mx 2022-07-17 14:24:43 +02:00
parent 0a38760780
commit 3c178f7d1b
3 changed files with 11 additions and 26 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "spinoff"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
authors = ["ad4m"]
description = "🔨 Simple to use Rust library for displaying spinners in the terminal."

View file

@ -14,11 +14,11 @@
## Usage
```rust
use spinoff::Spinners;
use spinoff::{Spinner, Spinners};
use std::thread::sleep;
use std::time::Duration;
let spinner = spinoff::new(Spinners::Dots, "Loading...", "blue".into()); // Can also be Some("blue") or None
let spinner = Spinner::new(Spinners::Dots, "Loading...", "blue".into()); // Can also be Some("blue") or None
sleep(Duration::from_secs(3));
spinner.success("Done!");
```
@ -26,13 +26,13 @@ spinner.success("Done!");
### Update a spinner
```rust
use spinoff::Spinners;
use spinoff::{Spinner, Spinners};
use std::thread::sleep;
use std::time::Duration;
let mut spinner = spinoff::new(Spinners::Dots, "Loading...", "blue".into());
let mut spinner = Spinner::new(Spinners::Dots, "Loading...", "blue".into());
sleep(Duration::from_secs(3));
spinner = spinner.update(Spinners::Dots2, "Loading...", None);
spinner.update(Spinners::Dots2, "Loading...", None);
sleep(Duration::from_secs(3));
spinner.stop_and_persist("👨‍💻", "Done!");
```

View file

@ -44,10 +44,8 @@ pub use spinner_enum::Spinners;
#[derive(Debug)]
pub struct Spinner {
thread_handle: Option<JoinHandle<()>>,
/// This struct has an `Arc<AtomicBool>` field, which is later used in the `stop` type methods to stop the thread printing the spinner.
still_spinning: Arc<AtomicBool>,
msg: Cow<'static, str>,
}
@ -93,22 +91,16 @@ impl Spinner {
T: Into<Cow<'static, str>>,
{
let still_spinning = Arc::new(AtomicBool::new(true));
// Gain ownership of the message for the thread to use
let msg = msg.into();
// We use atomic bools to make the thread stop itself when the `spinner.stop()` method is called.
let handle = thread::spawn({
// Clone the atomic bool so that we can use it in the thread and return the original one later.
let still_spinning = Arc::clone(&still_spinning);
let msg = msg.clone();
move || {
let spinner_data = SPINNER_FRAMES.get(&spinner_type).unwrap();
let stdout = stdout();
// Iterate over all the frames of the spinner while the atomic bool is true.
let frames = spinner_data
.frames
@ -117,7 +109,6 @@ impl Spinner {
.take_while(|_| still_spinning.load(std::sync::atomic::Ordering::Relaxed));
let mut last_length = 0;
for frame in frames {
// This is dependant on the stdout attached is a terminal that respects \r
let mut stdout_lock = stdout.lock();
@ -132,12 +123,11 @@ impl Spinner {
spinner_data.interval as u64,
));
}
delete_last_line(last_length);
}
});
// Return a Spinner struct so we can implement methods on it instead of `spinoff::stop()` etc.
// Return a Spinner struct
Self {
thread_handle: Some(handle),
still_spinning,
@ -184,7 +174,7 @@ impl Spinner {
/// sp.stop_with_message("Bye");
/// ```
///
pub fn stop_with_message(mut self, msg: &str) {
pub fn stop_with_message(mut self, msg: StringLiteral) {
self.stop_spinner_thread();
// put the message over the spinner
@ -205,9 +195,8 @@ impl Spinner {
/// sp.stop_and_persist("🍕", "Pizza!");
/// ```
///
pub fn stop_and_persist(mut self, symbol: &str, msg: &str) {
pub fn stop_and_persist(mut self, symbol: StringLiteral, msg: StringLiteral) {
self.stop_spinner_thread();
println!("{} {}", symbol, msg);
}
@ -227,7 +216,6 @@ impl Spinner {
///
pub fn success(mut self, msg: StringLiteral) {
self.stop_spinner_thread();
println!(
"{} {}",
init_color(Some(Color::Green), "".to_string()),
@ -251,7 +239,6 @@ impl Spinner {
///
pub fn fail(mut self, msg: StringLiteral) {
self.stop_spinner_thread();
println!("{} {}", init_color(Some(Color::Red), "".to_string()), &msg);
}
@ -271,7 +258,6 @@ impl Spinner {
///
pub fn warn(mut self, msg: StringLiteral) {
self.stop_spinner_thread();
println!(
"{} {}",
init_color(Some(Color::Yellow), "".to_string()),
@ -302,7 +288,6 @@ impl Spinner {
T: Into<Cow<'static, str>>,
{
self.stop_spinner_thread();
let _ = std::mem::replace(self, Self::new(spinner, msg, color));
}
@ -326,11 +311,11 @@ impl Spinner {
/// Stop the spinner thread and wait for it.
fn stop_spinner_thread(&mut self) {
// set flag to signal thread to stop
// Set flag to signal thread to stop
self.still_spinning
.store(false, std::sync::atomic::Ordering::Relaxed);
// wait for the thread to actually stop
// Wait for the thread to actually stop
self.thread_handle
.take()
.expect("Stopping the spinner thread should only happen once.")