diff --git a/src/lib.rs b/src/lib.rs index e126805..282bf85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,14 @@ #![allow(dead_code)] +macro_rules! flush { + () => { + { + use std::io::Write; + let _ = std::io::stdout().flush(); + } + } +} + mod util; mod inter; pub use inter::*; @@ -7,8 +16,6 @@ pub mod progress; pub mod wheel; pub mod spinner; - - #[cfg(test)] mod tests { diff --git a/src/progress.rs b/src/progress.rs index a7bdf99..84f53d0 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -49,6 +49,7 @@ impl Bar { let mut this = Self::new(width); this.set_title(title.as_ref()); + this.update(); this } /// Create a new bar with max display width of our terminal @@ -67,13 +68,15 @@ impl Bar /// If `width` is larger than or equal to `max_width`. pub fn with_max(width: usize, max_width: usize) -> Self { - Self { + let mut this = Self { width, max_width, progress: 0.0, buffer: String::with_capacity(width), title: String::with_capacity(max_width - width), - } + }; + this.update(); + this } /// Fit to terminal's width if possible. @@ -167,11 +170,13 @@ impl Display for Bar let temp = ensure_eq(format!("{}{}", temp, title), self.max_width); print!("\x1B[0m\x1B[K{}", temp); print!("\n\x1B[1A"); + flush!(); } fn blank(&self) { print!("\r{}\r", " ".repeat(self.max_width)); + flush!(); } fn get_title(&self) -> &str @@ -182,6 +187,7 @@ impl Display for Bar fn set_title(&mut self, from: &str) { self.title = from.to_string(); + self.refresh(); } fn update_dimensions(&mut self, to: usize) diff --git a/src/spinner.rs b/src/spinner.rs index 52ef282..1483ef5 100644 --- a/src/spinner.rs +++ b/src/spinner.rs @@ -82,10 +82,12 @@ impl Display for Spin fn refresh(&self) { print!("\r{} {}", self.title, self.current); + flush!(); } fn blank(&self) { print!("\r{} \r", " ".repeat(self.title.chars().count())); + flush!(); } fn get_title(&self) -> &str { @@ -98,6 +100,13 @@ impl Display for Spin self.refresh(); } fn update_dimensions(&mut self, _to:usize){} + + fn println(&self, string: &str) + { + self.blank(); + println!("{}", string); + self.refresh(); + } } impl Spinner for Spin