fixed terminal size changing

master
Avril 4 years ago
parent 10742e1cac
commit 60a070d18e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -2,7 +2,7 @@
name = "termprogress" name = "termprogress"
description = "A terminal progress bar renderer with status and spinners" description = "A terminal progress bar renderer with status and spinners"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
version = "0.1.2" version = "0.2.2"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018" edition = "2018"

@ -12,6 +12,13 @@ pub trait Display
println!("{}", string); println!("{}", string);
self.refresh(); 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 /// Get the title for this display
fn get_title(&self) -> &str; fn get_title(&self) -> &str;

@ -29,6 +29,8 @@ pub struct Bar
progress: f64, progress: f64,
buffer: String, buffer: String,
title: String, title: String,
#[cfg(feature="size")]
fit_to_term: bool,
} }
impl Default for Bar impl Default for Bar
@ -49,6 +51,14 @@ impl Bar
this.update(); this.update();
this this
} }
#[inline]
fn autofit(&mut self)
{
#[cfg(feature="size")]
self.fit();
}
/// Create a new bar with max display width of our terminal /// Create a new bar with max display width of our terminal
/// ///
/// # Notes /// # Notes
@ -60,9 +70,13 @@ impl Bar
return { return {
if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() { if let Some((terminal_size::Width(tw), _)) = terminal_size::terminal_size() {
let tw = usize::from(tw); 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 { } 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"))] #[cfg(not(feature="size"))]
@ -82,6 +96,7 @@ impl Bar
progress: 0.0, progress: 0.0,
buffer: String::with_capacity(width), buffer: String::with_capacity(width),
title: String::with_capacity(max_width - width), title: String::with_capacity(max_width - width),
fit_to_term: false,
}; };
this.update(); this.update();
this this
@ -103,6 +118,19 @@ impl Bar
false 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. /// Update the buffer.
pub fn update(&mut self) pub fn update(&mut self)
{ {
@ -175,10 +203,11 @@ impl Display for Bar
{ {
fn refresh(&self) fn refresh(&self)
{ {
let (_, max_width) = self.widths();
let temp = format!("[{}]: {:.2}%", self.buffer, self.progress * 100.00); 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!("\x1B[0m\x1B[K{}", temp);
print!("\n\x1B[1A"); print!("\n\x1B[1A");
flush!(); flush!();
@ -186,7 +215,8 @@ impl Display for Bar
fn blank(&self) fn blank(&self)
{ {
print!("\r{}\r", " ".repeat(self.max_width)); let (_, max_width) = self.widths();
print!("\r{}\r", " ".repeat(max_width));
flush!(); flush!();
} }

Loading…
Cancel
Save