Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/finish api #33

Merged
merged 15 commits into from
Oct 30, 2016
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Console progress bar for Rust Inspired from [pb](http://github.com/cheggaaa/pb), support and
tested on MacOS, Linux and Windows

![Screenshot](https://github.com/a8m/pb/blob/master/gif/rec_v2.gif)
![Screenshot](https://github.com/a8m/pb/blob/master/gif/rec_v3.gif)

[Documentation](http://a8m.github.io/pb/doc/pbr/index.html)

Expand Down
20 changes: 15 additions & 5 deletions examples/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
pb.tick();
}
for _ in 0..20 {
let n = rand::thread_rng().gen_range(0, 100 * i);
let n = rand::thread_rng().gen_range(0, 100);
pb.message("Connected: ");
thread::sleep(Duration::from_millis(n));
pb.inc();
Expand All @@ -34,9 +34,7 @@ fn main() {
thread::sleep(Duration::from_millis(100));
pb.tick();
}
pb.message("Completed! ");
pb.tick();
pb.finish();
pb.finish_print(&format!("{}: Pull complete", rand_string()));
});
}

Expand All @@ -51,7 +49,7 @@ fn main() {
thread::spawn(move || {
for _ in 0..count {
pb.inc();
let n = rand::thread_rng().gen_range(0, 100 * i);
let n = rand::thread_rng().gen_range(0, 100);
thread::sleep(Duration::from_millis(n));
}
pb.finish();
Expand All @@ -62,3 +60,15 @@ fn main() {

println!("\nall bars done!\n");
}

fn rand_string() -> String {
let mut v = Vec::new();
while v.len() < 12 {
let b = rand::random::<u8>();
// [0-9a-f]
if b > 47 && b < 58 || b > 96 && b < 103 {
v.push(b);
}
}
std::str::from_utf8(&v).unwrap().to_string()
}
30 changes: 30 additions & 0 deletions examples/npm_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern crate pbr;
use pbr::ProgressBar;
use std::thread;
use std::time::Duration;

fn main() {
let count = 30;
let mut pb = ProgressBar::new(count * 10);
pb.tick_format("\\|/-");
pb.format("|#--|");
pb.show_tick = true;
pb.show_speed = false;
pb.show_percent = false;
pb.show_counter = false;
pb.show_time_left = false;
pb.inc();
for _ in 0..count {
for _ in 0..10 {
pb.message("normalize -> thing ");
thread::sleep(Duration::from_millis(80));
pb.tick();
}
for _ in 0..10 {
pb.message("fuzz -> tree ");
thread::sleep(Duration::from_millis(80));
pb.inc();
}
}
pb.finish_println("done!");
}
18 changes: 18 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate rand;
extern crate pbr;
use rand::Rng;
use pbr::ProgressBar;
use std::thread;
use std::time::Duration;

fn main() {
let count = 500;
let mut pb = ProgressBar::new(count);
pb.format("╢▌▌░╟");
for _ in 0..count {
pb.inc();
let n = rand::thread_rng().gen_range(0, 100);
thread::sleep(Duration::from_millis(n));
}
pb.finish_println("done!");
}
Binary file added gif/rec_v3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl<T: Write> MultiBar<T> {
chan: self.chan.0.clone(),
},
total);
p.is_multibar = true;
p.add(0);
p
}
Expand Down
65 changes: 50 additions & 15 deletions src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct ProgressBar<T: Write> {
last_refresh_time: SteadyTime,
max_refresh_rate: Option<time::Duration>,
pub is_finish: bool,
pub is_multibar: bool,
pub show_bar: bool,
pub show_speed: bool,
pub show_percent: bool,
Expand Down Expand Up @@ -114,6 +115,7 @@ impl<T: Write> ProgressBar<T> {
start_time: SteadyTime::now(),
units: Units::Default,
is_finish: false,
is_multibar: false,
show_bar: true,
show_speed: true,
show_percent: true,
Expand Down Expand Up @@ -299,14 +301,8 @@ impl<T: Write> ProgressBar<T> {

let time_elapsed = time_to_std(now - self.start_time);
let speed = self.current as f64 / fract_dur(time_elapsed);
let width = self.width();

let width = if let Some(w) = self.width {
w
} else if let Some((Width(w), _)) = terminal_size() {
w as usize
} else {
80
};
let mut base = String::new();
let mut suffix = String::new();
let mut prefix = String::new();
Expand All @@ -315,7 +311,8 @@ impl<T: Write> ProgressBar<T> {
// precent box
if self.show_percent {
let percent = self.current as f64 / (self.total as f64 / 100f64);
suffix = suffix + &format!(" {:.*} % ", 2, if percent.is_nan() { 0.0 } else { percent });
suffix = suffix +
&format!(" {:.*} % ", 2, if percent.is_nan() { 0.0 } else { percent });
}
// speed box
if self.show_speed {
Expand Down Expand Up @@ -364,7 +361,7 @@ impl<T: Write> ProgressBar<T> {
base = self.bar_start.clone();
if rema_count > 0 && curr_count > 0 {
base = base + repeat!(self.bar_current.as_ref(), curr_count - 1) +
&self.bar_current_n;
&self.bar_current_n;
} else {
base = base + repeat!(self.bar_current.as_ref(), curr_count);
}
Expand All @@ -384,9 +381,9 @@ impl<T: Write> ProgressBar<T> {
self.last_refresh_time = SteadyTime::now();
}

/// Calling finish manually will set current to total and draw
/// the last time
pub fn finish(&mut self) {
// finish_draw ensure that the progress bar is reached to its end, and do the
// last drawing if needed.
fn finish_draw(&mut self) {
let mut redraw = false;

if let Some(mrr) = self.max_refresh_rate {
Expand All @@ -404,15 +401,53 @@ impl<T: Write> ProgressBar<T> {
if redraw {
self.draw();
}
self.is_finish = true;
}

/// Calling finish manually will set current to total and draw
/// the last time
pub fn finish(&mut self) {
self.finish_draw();
printfl!(self.handle, "");
self.is_finish = true;
}

/// Call finish and write string 's'

/// Call finish and write string 's' that will replace the progress bar.
pub fn finish_print(&mut self, s: &str) {
self.finish_draw();
let width = self.width();
let mut out = format!("{}", s);
if s.len() < width {
out += repeat!(" ", width - s.len());
};
printfl!(self.handle, "\r{}", out);
self.finish();
printfl!(self.handle, "\n{}", s)
}


/// Call finish and write string 's' below the progress bar.
///
/// If the ProgressBar is part of MultiBar instance, you should use
/// `finish_print` to print message.
pub fn finish_println(&mut self, s: &str) {
// `finish_println` does not allow in MultiBar mode, because printing
// new line will break the multiBar output.
if self.is_multibar {
return self.finish_print(s);
}
self.finish_draw();
printfl!(self.handle, "\n{}", s);
}

/// Get terminal width, from configuration, terminal size, or default(80)
fn width(&mut self) -> usize {
if let Some(w) = self.width {
w
} else if let Some((Width(w), _)) = terminal_size() {
w as usize
} else {
80
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ fn compare_with_stty() {
args[0] = "-f"
}
let output = Command::new("stty")
.args(&args)
.stderr(Stdio::inherit())
.output()
.unwrap();
.args(&args)
.stderr(Stdio::inherit())
.output()
.unwrap();
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(output.status.success());

Expand Down
9 changes: 5 additions & 4 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ pub fn move_cursor_up(n: usize) -> String {
use self::winapi::COORD;
if let Some((hand, csbi)) = get_csbi() {
unsafe {
SetConsoleCursorPosition(hand, COORD {
X: 0,
Y: csbi.dwCursorPosition.Y - n as i16,
});
SetConsoleCursorPosition(hand,
COORD {
X: 0,
Y: csbi.dwCursorPosition.Y - n as i16,
});
}
}
"".to_string()
Expand Down
8 changes: 4 additions & 4 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn simple_example() {
pb.inc();
thread::sleep(Duration::from_millis(5));
}
pb.finish_print("done!");
pb.finish_println("done!");
}

#[test]
Expand All @@ -26,7 +26,7 @@ fn custom_width_example() {
pb.inc();
thread::sleep(Duration::from_millis(5));
}
pb.finish_print("done!");
pb.finish_println("done!");
}

#[test]
Expand Down Expand Up @@ -60,7 +60,7 @@ fn timeout_example() {
thread::sleep(Duration::from_millis(50));
pb.tick();
}
pb.finish_print("done!");
pb.finish_println("done!");
}


Expand Down Expand Up @@ -105,5 +105,5 @@ fn npm_bar() {
pb.inc();
}
}
pb.finish_print("done!");
pb.finish_println("done!");
}