master
Avril 4 years ago
parent d5fe7cd57d
commit 031935cf02
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,5 +1,14 @@
#![allow(dead_code)] #![allow(dead_code)]
macro_rules! flush {
() => {
{
use std::io::Write;
let _ = std::io::stdout().flush();
}
}
}
mod util; mod util;
mod inter; mod inter;
pub use inter::*; pub use inter::*;
@ -7,8 +16,6 @@ pub mod progress;
pub mod wheel; pub mod wheel;
pub mod spinner; pub mod spinner;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

@ -49,6 +49,7 @@ impl Bar
{ {
let mut this = Self::new(width); let mut this = Self::new(width);
this.set_title(title.as_ref()); this.set_title(title.as_ref());
this.update();
this this
} }
/// Create a new bar with max display width of our terminal /// 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`. /// If `width` is larger than or equal to `max_width`.
pub fn with_max(width: usize, max_width: usize) -> Self pub fn with_max(width: usize, max_width: usize) -> Self
{ {
Self { let mut this = Self {
width, width,
max_width, max_width,
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),
} };
this.update();
this
} }
/// Fit to terminal's width if possible. /// 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); let temp = ensure_eq(format!("{}{}", temp, title), self.max_width);
print!("\x1B[0m\x1B[K{}", temp); print!("\x1B[0m\x1B[K{}", temp);
print!("\n\x1B[1A"); print!("\n\x1B[1A");
flush!();
} }
fn blank(&self) fn blank(&self)
{ {
print!("\r{}\r", " ".repeat(self.max_width)); print!("\r{}\r", " ".repeat(self.max_width));
flush!();
} }
fn get_title(&self) -> &str fn get_title(&self) -> &str
@ -182,6 +187,7 @@ impl Display for Bar
fn set_title(&mut self, from: &str) fn set_title(&mut self, from: &str)
{ {
self.title = from.to_string(); self.title = from.to_string();
self.refresh();
} }
fn update_dimensions(&mut self, to: usize) fn update_dimensions(&mut self, to: usize)

@ -82,10 +82,12 @@ impl Display for Spin
fn refresh(&self) fn refresh(&self)
{ {
print!("\r{} {}", self.title, self.current); print!("\r{} {}", self.title, self.current);
flush!();
} }
fn blank(&self) fn blank(&self)
{ {
print!("\r{} \r", " ".repeat(self.title.chars().count())); print!("\r{} \r", " ".repeat(self.title.chars().count()));
flush!();
} }
fn get_title(&self) -> &str fn get_title(&self) -> &str
{ {
@ -98,6 +100,13 @@ impl Display for Spin
self.refresh(); self.refresh();
} }
fn update_dimensions(&mut self, _to:usize){} fn update_dimensions(&mut self, _to:usize){}
fn println(&self, string: &str)
{
self.blank();
println!("{}", string);
self.refresh();
}
} }
impl Spinner for Spin impl Spinner for Spin

Loading…
Cancel
Save