diff --git a/Cargo.toml b/Cargo.toml index 3863565..ecc691f 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.1.2" +version = "0.2.2" authors = ["Avril "] edition = "2018" diff --git a/src/inter.rs b/src/inter.rs index d1f9665..cea5b4e 100644 --- a/src/inter.rs +++ b/src/inter.rs @@ -12,6 +12,13 @@ pub trait Display println!("{}", string); self.refresh(); } + /// Blank then print a line std stderr, and redisplay. + fn eprintln(&self, string: &str) + { + self.blank(); + eprintln!("{}", string); + self.refresh(); + } /// Get the title for this display fn get_title(&self) -> &str; diff --git a/src/progress.rs b/src/progress.rs index 8d888d4..9d48279 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -29,6 +29,8 @@ pub struct Bar progress: f64, buffer: String, title: String, + #[cfg(feature="size")] + fit_to_term: bool, } impl Default for Bar @@ -49,6 +51,14 @@ impl Bar this.update(); this } + + #[inline] + fn autofit(&mut self) + { + #[cfg(feature="size")] + self.fit(); + } + /// Create a new bar with max display width of our terminal /// /// # Notes @@ -60,9 +70,13 @@ impl Bar return { if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() { let tw = usize::from(tw); - Self::with_max(if width < tw {width} else {tw}, tw) + let mut o = Self::with_max(if width < tw {width} else {tw}, tw); + o.fit_to_term = true; + o } else { - Self::with_max(width, width +20) + let mut o = Self::with_max(width, width +20); + o.fit_to_term = true; + o } }; #[cfg(not(feature="size"))] @@ -82,6 +96,7 @@ impl Bar progress: 0.0, buffer: String::with_capacity(width), title: String::with_capacity(max_width - width), + fit_to_term: false, }; this.update(); this @@ -102,6 +117,19 @@ impl Bar } false } + + #[inline] fn widths(&self) -> (usize, usize) + { + #[cfg(feature="size")] + if self.fit_to_term { + if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() { + let tw = usize::from(tw); + let width = if self.width < tw {self.width} else {tw}; + return (width, tw); + } + }; + (self.width, self.max_width) + } /// Update the buffer. pub fn update(&mut self) @@ -175,10 +203,11 @@ impl Display for Bar { fn refresh(&self) { + let (_, max_width) = self.widths(); let temp = format!("[{}]: {:.2}%", self.buffer, self.progress * 100.00); - let title = ensure_lower(format!(" {}", self.title), self.max_width - temp.chars().count()); + let title = ensure_lower(format!(" {}", self.title), max_width - temp.chars().count()); - let temp = ensure_eq(format!("{}{}", temp, title), self.max_width); + let temp = ensure_eq(format!("{}{}", temp, title), max_width); print!("\x1B[0m\x1B[K{}", temp); print!("\n\x1B[1A"); flush!(); @@ -186,7 +215,8 @@ impl Display for Bar fn blank(&self) { - print!("\r{}\r", " ".repeat(self.max_width)); + let (_, max_width) = self.widths(); + print!("\r{}\r", " ".repeat(max_width)); flush!(); }