From e81bf8403fdfaa27a2bdc94ab3588b567f388c17 Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 9 Aug 2020 16:46:31 +0100 Subject: [PATCH] Added `silent` implemetor --- Cargo.toml | 2 +- src/inter.rs | 14 ++++++++++++-- src/lib.rs | 2 ++ src/progress.rs | 16 ++++++++++++++++ src/silent.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/spinner.rs | 17 +++++++++++++++++ 6 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/silent.rs diff --git a/Cargo.toml b/Cargo.toml index ecc691f..43d0eb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "termprogress" description = "A terminal progress bar renderer with status and spinners" license = "GPL-3.0-or-later" -version = "0.2.2" +version = "0.2.3" authors = ["Avril "] edition = "2018" diff --git a/src/inter.rs b/src/inter.rs index cea5b4e..23d88f6 100644 --- a/src/inter.rs +++ b/src/inter.rs @@ -31,13 +31,23 @@ pub trait Display /// A trait for any bar with progress. You can implemnent your own styles through this trait. pub trait ProgressBar: Display -{ +{ fn set_progress(&mut self, value: f64); fn get_progress(&self) -> f64; } /// A trait for any bar without progress. You can implemnent your own styles through this trait. pub trait Spinner: Display -{ +{ + /// Cause the spinner to increment once. fn bump(&mut self); } + +/// A trait for creating a progress bar or spinner with a title. +pub trait WithTitle: Sized + ProgressBar + Display +{ + fn with_title(len: usize, string: impl AsRef) -> Self; + fn update(&mut self); + fn complete(self); +} + diff --git a/src/lib.rs b/src/lib.rs index 38ea3b3..362b79f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub use inter::*; pub mod progress; pub mod wheel; pub mod spinner; +pub mod silent; /// The prelude exposes the traits for spinners and progress bars, and the `spinner::Spin` and `progress::Bar` types for easy access and use. pub mod prelude { @@ -23,5 +24,6 @@ pub mod prelude { pub use super::{ spinner::Spin, progress::Bar, + silent::Silent, }; } diff --git a/src/progress.rs b/src/progress.rs index 9d48279..2f296cd 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -253,3 +253,19 @@ impl ProgressBar for Bar self.refresh(); } } + +impl WithTitle for Bar +{ + fn with_title(len: usize, string: impl AsRef) -> Self + { + Self::with_title(len, string) + } + fn update(&mut self) + { + self.update(); + } + fn complete(self) + { + self.complete(); + } +} diff --git a/src/silent.rs b/src/silent.rs new file mode 100644 index 0000000..41092fc --- /dev/null +++ b/src/silent.rs @@ -0,0 +1,42 @@ +//! A silent progress bar and spinner that does nothing. +//! +//! Useful for when progress bars are optional. + +use super::*; + + +/// An implementor for the `Display`, `ProgressBar`, `Spinner`, and `WithTitle` that does nothing. +/// +/// It also implements `Display::println()` and `Display::eprintln()` to do nothing as well. +#[derive(Debug)] +pub struct Silent; + +impl Display for Silent +{ + #[inline] fn println(&self, _: &str){} + #[inline] fn eprintln(&self, _: &str){} + #[inline] fn refresh(&self){} + #[inline] fn blank(&self){} + #[inline] fn get_title(&self) -> &str{""} + #[inline] fn set_title(&mut self, _: &str){} + #[inline] fn update_dimensions(&mut self, _:usize){} +} + +impl ProgressBar for Silent +{ + #[inline] fn set_progress(&mut self, _:f64){} + #[inline] fn get_progress(&self) -> f64{0.0} +} + +impl Spinner for Silent +{ + #[inline] fn bump(&mut self){} +} + +impl WithTitle for Silent +{ + #[inline] fn with_title(_: usize, _: impl AsRef) -> Self{Self} + #[inline] fn update(&mut self) {} + #[inline] fn complete(self) {} +} + diff --git a/src/spinner.rs b/src/spinner.rs index ad47688..a80c314 100644 --- a/src/spinner.rs +++ b/src/spinner.rs @@ -119,3 +119,20 @@ impl Spinner for Spin self.refresh(); } } + + +impl WithTitle for Spin +{ + fn with_title(_: usize, t: impl AsRef) -> Self + { + Self::with_title(t, Default::default()) + } + fn update(&mut self) + { + Self::update(self) + } + fn complete(self) + { + Self::complete(self) + } +}