fix: fix README
This commit is contained in:
parent
0a38760780
commit
3c178f7d1b
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "spinoff"
|
name = "spinoff"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["ad4m"]
|
authors = ["ad4m"]
|
||||||
description = "🔨 Simple to use Rust library for displaying spinners in the terminal."
|
description = "🔨 Simple to use Rust library for displaying spinners in the terminal."
|
||||||
|
|
10
README.md
10
README.md
|
@ -14,11 +14,11 @@
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use spinoff::Spinners;
|
use spinoff::{Spinner, Spinners};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
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));
|
sleep(Duration::from_secs(3));
|
||||||
spinner.success("Done!");
|
spinner.success("Done!");
|
||||||
```
|
```
|
||||||
|
@ -26,13 +26,13 @@ spinner.success("Done!");
|
||||||
### Update a spinner
|
### Update a spinner
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use spinoff::Spinners;
|
use spinoff::{Spinner, Spinners};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
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));
|
sleep(Duration::from_secs(3));
|
||||||
spinner = spinner.update(Spinners::Dots2, "Loading...", None);
|
spinner.update(Spinners::Dots2, "Loading...", None);
|
||||||
sleep(Duration::from_secs(3));
|
sleep(Duration::from_secs(3));
|
||||||
spinner.stop_and_persist("👨💻", "Done!");
|
spinner.stop_and_persist("👨💻", "Done!");
|
||||||
```
|
```
|
||||||
|
|
25
src/lib.rs
25
src/lib.rs
|
@ -44,10 +44,8 @@ pub use spinner_enum::Spinners;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Spinner {
|
pub struct Spinner {
|
||||||
thread_handle: Option<JoinHandle<()>>,
|
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.
|
/// 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>,
|
still_spinning: Arc<AtomicBool>,
|
||||||
|
|
||||||
msg: Cow<'static, str>,
|
msg: Cow<'static, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,22 +91,16 @@ impl Spinner {
|
||||||
T: Into<Cow<'static, str>>,
|
T: Into<Cow<'static, str>>,
|
||||||
{
|
{
|
||||||
let still_spinning = Arc::new(AtomicBool::new(true));
|
let still_spinning = Arc::new(AtomicBool::new(true));
|
||||||
|
|
||||||
// Gain ownership of the message for the thread to use
|
// Gain ownership of the message for the thread to use
|
||||||
let msg = msg.into();
|
let msg = msg.into();
|
||||||
|
|
||||||
// We use atomic bools to make the thread stop itself when the `spinner.stop()` method is called.
|
// We use atomic bools to make the thread stop itself when the `spinner.stop()` method is called.
|
||||||
let handle = thread::spawn({
|
let handle = thread::spawn({
|
||||||
// Clone the atomic bool so that we can use it in the thread and return the original one later.
|
// 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 still_spinning = Arc::clone(&still_spinning);
|
||||||
|
|
||||||
let msg = msg.clone();
|
let msg = msg.clone();
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
let spinner_data = SPINNER_FRAMES.get(&spinner_type).unwrap();
|
let spinner_data = SPINNER_FRAMES.get(&spinner_type).unwrap();
|
||||||
|
|
||||||
let stdout = stdout();
|
let stdout = stdout();
|
||||||
|
|
||||||
// Iterate over all the frames of the spinner while the atomic bool is true.
|
// Iterate over all the frames of the spinner while the atomic bool is true.
|
||||||
let frames = spinner_data
|
let frames = spinner_data
|
||||||
.frames
|
.frames
|
||||||
|
@ -117,7 +109,6 @@ impl Spinner {
|
||||||
.take_while(|_| still_spinning.load(std::sync::atomic::Ordering::Relaxed));
|
.take_while(|_| still_spinning.load(std::sync::atomic::Ordering::Relaxed));
|
||||||
|
|
||||||
let mut last_length = 0;
|
let mut last_length = 0;
|
||||||
|
|
||||||
for frame in frames {
|
for frame in frames {
|
||||||
// This is dependant on the stdout attached is a terminal that respects \r
|
// This is dependant on the stdout attached is a terminal that respects \r
|
||||||
let mut stdout_lock = stdout.lock();
|
let mut stdout_lock = stdout.lock();
|
||||||
|
@ -132,12 +123,11 @@ impl Spinner {
|
||||||
spinner_data.interval as u64,
|
spinner_data.interval as u64,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
delete_last_line(last_length);
|
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 {
|
Self {
|
||||||
thread_handle: Some(handle),
|
thread_handle: Some(handle),
|
||||||
still_spinning,
|
still_spinning,
|
||||||
|
@ -184,7 +174,7 @@ impl Spinner {
|
||||||
/// sp.stop_with_message("Bye");
|
/// 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();
|
self.stop_spinner_thread();
|
||||||
|
|
||||||
// put the message over the spinner
|
// put the message over the spinner
|
||||||
|
@ -205,9 +195,8 @@ impl Spinner {
|
||||||
/// sp.stop_and_persist("🍕", "Pizza!");
|
/// 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();
|
self.stop_spinner_thread();
|
||||||
|
|
||||||
println!("{} {}", symbol, msg);
|
println!("{} {}", symbol, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +216,6 @@ impl Spinner {
|
||||||
///
|
///
|
||||||
pub fn success(mut self, msg: StringLiteral) {
|
pub fn success(mut self, msg: StringLiteral) {
|
||||||
self.stop_spinner_thread();
|
self.stop_spinner_thread();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{} {}",
|
"{} {}",
|
||||||
init_color(Some(Color::Green), "✔".to_string()),
|
init_color(Some(Color::Green), "✔".to_string()),
|
||||||
|
@ -251,7 +239,6 @@ impl Spinner {
|
||||||
///
|
///
|
||||||
pub fn fail(mut self, msg: StringLiteral) {
|
pub fn fail(mut self, msg: StringLiteral) {
|
||||||
self.stop_spinner_thread();
|
self.stop_spinner_thread();
|
||||||
|
|
||||||
println!("{} {}", init_color(Some(Color::Red), "✖".to_string()), &msg);
|
println!("{} {}", init_color(Some(Color::Red), "✖".to_string()), &msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +258,6 @@ impl Spinner {
|
||||||
///
|
///
|
||||||
pub fn warn(mut self, msg: StringLiteral) {
|
pub fn warn(mut self, msg: StringLiteral) {
|
||||||
self.stop_spinner_thread();
|
self.stop_spinner_thread();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{} {}",
|
"{} {}",
|
||||||
init_color(Some(Color::Yellow), "⚠".to_string()),
|
init_color(Some(Color::Yellow), "⚠".to_string()),
|
||||||
|
@ -302,7 +288,6 @@ impl Spinner {
|
||||||
T: Into<Cow<'static, str>>,
|
T: Into<Cow<'static, str>>,
|
||||||
{
|
{
|
||||||
self.stop_spinner_thread();
|
self.stop_spinner_thread();
|
||||||
|
|
||||||
let _ = std::mem::replace(self, Self::new(spinner, msg, color));
|
let _ = std::mem::replace(self, Self::new(spinner, msg, color));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,11 +311,11 @@ impl Spinner {
|
||||||
|
|
||||||
/// Stop the spinner thread and wait for it.
|
/// Stop the spinner thread and wait for it.
|
||||||
fn stop_spinner_thread(&mut self) {
|
fn stop_spinner_thread(&mut self) {
|
||||||
// set flag to signal thread to stop
|
// Set flag to signal thread to stop
|
||||||
self.still_spinning
|
self.still_spinning
|
||||||
.store(false, std::sync::atomic::Ordering::Relaxed);
|
.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
|
||||||
// wait for the thread to actually stop
|
// Wait for the thread to actually stop
|
||||||
self.thread_handle
|
self.thread_handle
|
||||||
.take()
|
.take()
|
||||||
.expect("Stopping the spinner thread should only happen once.")
|
.expect("Stopping the spinner thread should only happen once.")
|
||||||
|
|
Loading…
Reference in a new issue