update: v0.3.0

This commit is contained in:
ad4mx 2022-07-18 21:15:32 +02:00
parent 1c2e19ac8b
commit a66e983554
6 changed files with 155 additions and 151 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "spinoff" name = "spinoff"
version = "0.2.0" version = "0.3.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."

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 278 KiB

View file

@ -1,4 +1,4 @@
use spinoff::{Spinner, Spinners, Color}; use spinoff::{Color, Spinner, Spinners};
use std::{thread::sleep, time::Duration}; use std::{thread::sleep, time::Duration};
fn main() { fn main() {

View file

@ -59,6 +59,8 @@ pub enum Color {
Yellow, Yellow,
Cyan, Cyan,
White, White,
Black,
Magenta,
} }
impl Spinner { impl Spinner {
@ -77,7 +79,7 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", Some(Color::Blue)); /// let sp = Spinner::new(Spinners::Dots, "Hello World!", Some(Color::Blue));
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.clear(); /// sp.clear();
/// ``` /// ```
@ -107,12 +109,12 @@ impl Spinner {
.iter() .iter()
.cycle() .cycle()
.take_while(|_| still_spinning.load(std::sync::atomic::Ordering::Relaxed)); .take_while(|_| still_spinning.load(std::sync::atomic::Ordering::Relaxed));
// Dynamically delete the last line of the terminal depending on the length of the message + spinner.
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
let mut stdout_lock = stdout.lock(); let mut stdout_lock = stdout.lock();
let frame_str = format!(" {} {}", init_color(color, frame.to_string()), msg); let frame_str = format!(" {} {}", init_color(color, frame.to_string()), msg);
// Get us back to the start of the line.
delete_last_line(last_length); delete_last_line(last_length);
last_length = frame_str.bytes().len(); last_length = frame_str.bytes().len();
write!(stdout_lock, "{}", frame_str).unwrap(); write!(stdout_lock, "{}", frame_str).unwrap();
@ -144,7 +146,7 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", None); /// let sp = Spinner::new(Spinners::Dots9, "Spinning...", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.stop(); /// sp.stop();
/// ``` /// ```
@ -168,7 +170,7 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", None); /// let sp = Spinner::new(Spinners::Dots2, "Hello", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.stop_with_message("Bye"); /// sp.stop_with_message("Bye");
/// ``` /// ```
@ -188,7 +190,7 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", None); /// let sp = Spinner::new(Spinners::Mindblown, "Guess what's coming...", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.stop_and_persist("🍕", "Pizza!"); /// sp.stop_and_persist("🍕", "Pizza!");
/// ``` /// ```
@ -207,7 +209,7 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", None); /// let sp = Spinner::new(Spinners::Aesthetic, "Trying to load information...", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.success("Success!"); /// sp.success("Success!");
/// ``` /// ```
@ -230,9 +232,9 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", None); /// let sp = Spinner::new(Spinners::BouncingBar, "Executing code...", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.fail("Failed!"); /// sp.fail("Code failed to compile!");
/// ``` /// ```
/// ///
pub fn fail(mut self, msg: StringLiteral) { pub fn fail(mut self, msg: StringLiteral) {
@ -249,9 +251,9 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let sp = Spinner::new(Spinners::Dots, "Hello", None); /// let sp = Spinner::new(Spinners::Material, "Measuring network speed...", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.warn("Look out!"); /// sp.warn("You might want to check your internet connection...");
/// ``` /// ```
/// ///
pub fn warn(mut self, msg: StringLiteral) { pub fn warn(mut self, msg: StringLiteral) {
@ -275,7 +277,7 @@ impl Spinner {
/// let mut sp = Spinner::new(Spinners::Dots, "Hello", None); /// let mut sp = Spinner::new(Spinners::Dots, "Hello", None);
/// ///
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.update(Spinners::Dots2, "Goodbye", None); /// sp.update(Spinners::Dots2, "World", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// ///
/// sp.stop(); /// sp.stop();
@ -298,7 +300,7 @@ impl Spinner {
/// # use std::thread::sleep; /// # use std::thread::sleep;
/// # use std::time::Duration; /// # use std::time::Duration;
/// # /// #
/// let mut sp = Spinner::new(Spinners::Dots, "Hello", None); /// let mut sp = Spinner::new(Spinners::Grenade, "Clearing...", None);
/// sleep(Duration::from_millis(800)); /// sleep(Duration::from_millis(800));
/// sp.clear(); /// sp.clear();
/// ``` /// ```

View file

@ -22,6 +22,8 @@ pub fn init_color(color: Option<Color>, spinner: String) -> Paint<String> {
Some(Color::Yellow) => Paint::yellow(spinner), Some(Color::Yellow) => Paint::yellow(spinner),
Some(Color::Cyan) => Paint::cyan(spinner), Some(Color::Cyan) => Paint::cyan(spinner),
Some(Color::White) => Paint::new(spinner), Some(Color::White) => Paint::new(spinner),
Some(Color::Black) => Paint::black(spinner),
Some(Color::Magenta) => Paint::magenta(spinner),
None => Paint::new(spinner), None => Paint::new(spinner),
} }
} }

View file

@ -1,4 +1,4 @@
use crate::{spinner_enum::Spinners, StringLiteral}; use crate::spinner_enum::Spinners;
use maplit::{self, hashmap}; use maplit::{self, hashmap};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashMap; use std::collections::HashMap;
@ -7,7 +7,7 @@ use std::collections::HashMap;
/// Frames is a Vec of &str, each &str is a frame of the spinner. /// Frames is a Vec of &str, each &str is a frame of the spinner.
/// Interval is the number of milliseconds to wait before moving to the next frame. /// Interval is the number of milliseconds to wait before moving to the next frame.
pub struct SpinnerFrames { pub struct SpinnerFrames {
pub frames: Vec<StringLiteral>, pub frames: Vec<&'static str>,
pub interval: u16, pub interval: u16,
} }
@ -807,40 +807,40 @@ pub static SPINNER_FRAMES: Lazy<HashMap<Spinners, SpinnerFrames>> = Lazy::new(||
"(● )" "(● )"
], interval: 80}, ], interval: 80},
Spinners::Smiley => SpinnerFrames {frames: vec![ Spinners::Smiley => SpinnerFrames {frames: vec![
"😄 ", "😄",
"😝 " "😝"
], interval: 200}, ], interval: 200},
Spinners::Monkey => SpinnerFrames {frames: vec![ Spinners::Monkey => SpinnerFrames {frames: vec![
"🙈 ", "🙈",
"🙈 ", "🙈",
"🙉 ", "🙉",
"🙊 " "🙊"
], interval: 300}, ], interval: 300},
Spinners::Hearts => SpinnerFrames {frames: vec![ Spinners::Hearts => SpinnerFrames {frames: vec![
"💛 ", "💛",
"💙 ", "💙",
"💜 ", "💜",
"💚 ", "💚",
"❤️ " "❤️"
], interval: 100}, ], interval: 100},
Spinners::Clock => SpinnerFrames {frames: vec![ Spinners::Clock => SpinnerFrames {frames: vec![
"🕛 ", "🕛",
"🕐 ", "🕐",
"🕑 ", "🕑",
"🕒 ", "🕒",
"🕓 ", "🕓",
"🕔 ", "🕔",
"🕕 ", "🕕",
"🕖 ", "🕖",
"🕗 ", "🕗",
"🕘 ", "🕘",
"🕙 ", "🕙",
"🕚 " "🕚"
], interval: 100}, ], interval: 100},
Spinners::Earth => SpinnerFrames {frames: vec![ Spinners::Earth => SpinnerFrames {frames: vec![
"🌍 ", "🌍",
"🌎 ", "🌎",
"🌏 " "🌏"
], interval: 180}, ], interval: 180},
Spinners::Material => SpinnerFrames {frames: vec![ Spinners::Material => SpinnerFrames {frames: vec![
"█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁", "█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁",
@ -937,18 +937,18 @@ pub static SPINNER_FRAMES: Lazy<HashMap<Spinners, SpinnerFrames>> = Lazy::new(||
"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁" "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁"
], interval: 17}, ], interval: 17},
Spinners::Moon => SpinnerFrames {frames: vec![ Spinners::Moon => SpinnerFrames {frames: vec![
"🌑 ", "🌑",
"🌒 ", "🌒",
"🌓 ", "🌓",
"🌔 ", "🌔",
"🌕 ", "🌕",
"🌖 ", "🌖",
"🌗 ", "🌗",
"🌘 " "🌘"
], interval: 80}, ], interval: 80},
Spinners::Runner => SpinnerFrames {frames: vec![ Spinners::Runner => SpinnerFrames {frames: vec![
"🚶 ", "🚶",
"🏃 " "🏃"
], interval: 140}, ], interval: 140},
Spinners::Pong => SpinnerFrames {frames: vec![ Spinners::Pong => SpinnerFrames {frames: vec![
"▐⠂ ▌", "▐⠂ ▌",
@ -1017,29 +1017,29 @@ pub static SPINNER_FRAMES: Lazy<HashMap<Spinners, SpinnerFrames>> = Lazy::new(||
"b" "b"
], interval: 100}, ], interval: 100},
Spinners::Weather => SpinnerFrames {frames: vec![ Spinners::Weather => SpinnerFrames {frames: vec![
"☀️ ", "☀️",
"☀️ ", "☀️",
"☀️ ", "☀️",
"🌤 ", "🌤",
"⛅️ ", "⛅️",
"🌥 ", "🌥",
"☁️ ", "☁️",
"🌧 ", "🌧",
"🌨 ", "🌨",
"🌧 ", "🌧",
"🌨 ", "🌨",
"🌧 ", "🌧",
"🌨 ", "🌨",
" ", "",
"🌨 ", "🌨",
"🌧 ", "🌧",
"🌨 ", "🌨",
"☁️ ", "☁️",
"🌥 ", "🌥",
"⛅️ ", "⛅️",
"🌤 ", "🌤",
"☀️ ", "☀️",
"☀️ " "☀️"
], interval: 100}, ], interval: 100},
Spinners::Christmas => SpinnerFrames {frames: vec![ Spinners::Christmas => SpinnerFrames {frames: vec![
"🌲", "🌲",
@ -1083,97 +1083,97 @@ pub static SPINNER_FRAMES: Lazy<HashMap<Spinners, SpinnerFrames>> = Lazy::new(||
"ββββββρ" "ββββββρ"
], interval: 80}, ], interval: 80},
Spinners::FingerDance => SpinnerFrames {frames: vec![ Spinners::FingerDance => SpinnerFrames {frames: vec![
"🤘 ", "🤘",
"🤟 ", "🤟",
"🖖 ", "🖖",
" ", "",
"🤚 ", "🤚",
"👆 " "👆"
], interval: 160}, ], interval: 160},
Spinners::FistBump => SpinnerFrames {frames: vec![ Spinners::FistBump => SpinnerFrames {frames: vec![
"🤜    🤛 ", "🤜    🤛",
"🤜    🤛 ", "🤜    🤛",
"🤜    🤛 ", "🤜    🤛",
" 🤜  🤛  ", " 🤜  🤛 ",
"  🤜🤛   ", "  🤜🤛  ",
" 🤜✨🤛   ", " 🤜✨🤛  ",
"🤜 ✨ 🤛  " "🤜 ✨ 🤛 "
], interval: 80}, ], interval: 80},
Spinners::SoccerHeader => SpinnerFrames {frames: vec![ Spinners::SoccerHeader => SpinnerFrames {frames: vec![
" 🧑⚽️ 🧑 ", " 🧑⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️🧑 ", "🧑 ⚽️🧑 ",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 ", "🧑 ⚽️ 🧑",
"🧑 ⚽️ 🧑 " "🧑 ⚽️ 🧑"
], interval: 80}, ], interval: 80},
Spinners::Mindblown => SpinnerFrames {frames: vec![ Spinners::Mindblown => SpinnerFrames {frames: vec![
"😐 ", "😐",
"😐 ", "😐",
"😮 ", "😮",
"😮 ", "😮",
"😦 ", "😦",
"😦 ", "😦",
"😧 ", "😧",
"😧 ", "😧",
"🤯 ", "🤯",
"💥 ", "💥",
" ", "",
"  ", " ",
"  ", " ",
"  " " "
], interval: 160}, ], interval: 160},
Spinners::Speaker => SpinnerFrames {frames: vec![ Spinners::Speaker => SpinnerFrames {frames: vec![
"🔈 ", "🔈",
"🔉 ", "🔉",
"🔊 ", "🔊",
"🔉 " "🔉"
], interval: 160}, ], interval: 160},
Spinners::OrangePulse => SpinnerFrames {frames: vec![ Spinners::OrangePulse => SpinnerFrames {frames: vec![
"🔸 ", "🔸",
"🔶 ", "🔶",
"🟠 ", "🟠",
"🟠 ", "🟠",
"🔶 " "🔶"
], interval: 100}, ], interval: 100},
Spinners::BluePulse => SpinnerFrames {frames: vec![ Spinners::BluePulse => SpinnerFrames {frames: vec![
"🔹 ", "🔹",
"🔷 ", "🔷",
"🔵 ", "🔵",
"🔵 ", "🔵",
"🔷 " "🔷"
], interval: 100}, ], interval: 100},
Spinners::OrangeBluePulse => SpinnerFrames {frames: vec![ Spinners::OrangeBluePulse => SpinnerFrames {frames: vec![
"🔸 ", "🔸",
"🔶 ", "🔶",
"🟠 ", "🟠",
"🟠 ", "🟠",
"🔶 ", "🔶",
"🔹 ", "🔹",
"🔷 ", "🔷",
"🔵 ", "🔵",
"🔵 ", "🔵",
"🔷 " "🔷"
], interval: 100}, ], interval: 100},
Spinners::TimeTravel => SpinnerFrames {frames: vec![ Spinners::TimeTravel => SpinnerFrames {frames: vec![
"🕛 ", "🕛",
"🕚 ", "🕚",
"🕙 ", "🕙",
"🕘 ", "🕘",
"🕗 ", "🕗",
"🕖 ", "🕖",
"🕕 ", "🕕",
"🕔 ", "🕔",
"🕓 ", "🕓",
"🕒 ", "🕒",
"🕑 ", "🕑",
"🕐 " "🕐"
], interval: 100}, ], interval: 100},
Spinners::Aesthetic => SpinnerFrames {frames: vec![ Spinners::Aesthetic => SpinnerFrames {frames: vec![
"▰▱▱▱▱▱▱", "▰▱▱▱▱▱▱",