Skip to content

Commit

Permalink
Merge pull request #32 from a8m/feat/multibar
Browse files Browse the repository at this point in the history
Feat/multibar
  • Loading branch information
a8m committed Oct 19, 2016
2 parents 62bc2bb + 303f461 commit 8c25c80
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ keywords = ["cli", "progress", "terminal", "pb"]
license = "MIT"

[dependencies]
time = "0.1.35"
libc = "0.2.9"
time = "0.1.35"
winapi = "0.2"
kernel32-sys = "0.2"

[dev-dependencies]
rand = "*"
64 changes: 64 additions & 0 deletions examples/multi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
extern crate rand;
extern crate pbr;
use rand::Rng;
use pbr::MultiBar;
use std::thread;
use std::time::Duration;

fn main() {
let mut mb = MultiBar::new();
mb.println("Your Application Header:");
mb.println("");

for i in 1..6 {
let count = 100 * i;
let mut pb = mb.create_bar(count);
pb.tick_format("▏▎▍▌▋▊▉██▉▊▋▌▍▎▏");
pb.show_message = true;
thread::spawn(move || {
for _ in 0..count / 20 {
for _ in 0..20 {
pb.message("Waiting : ");
thread::sleep(Duration::from_millis(50));
pb.tick();
}
for _ in 0..20 {
let n = rand::thread_rng().gen_range(0, 100 * i);
pb.message("Connected: ");
thread::sleep(Duration::from_millis(n));
pb.inc();
}
}
for _ in 0..20 {
pb.message("Cleaning :");
thread::sleep(Duration::from_millis(100));
pb.tick();
}
pb.message("Completed! ");
pb.tick();
pb.finish();
});
}


mb.println("");
mb.println("Text lines separate between two sections: ");
mb.println("");

for i in 1..4 {
let count = 100 * i;
let mut pb = mb.create_bar(count);
thread::spawn(move || {
for _ in 0..count {
pb.inc();
let n = rand::thread_rng().gen_range(0, 100 * i);
thread::sleep(Duration::from_millis(n));
}
pb.finish();
});
}

mb.listen();

println!("\nall bars done!\n");
}
41 changes: 33 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,66 @@
//! println!("done!");
//! }
//! ```

// Macro for writing to the giving writer.
// Used in both pb.rs and multi.rs modules.
//
// # Examples
//
// ```
// let w = io::stdout();
// printfl!(w, "");
// printfl!(w, "\r{}", out);
//
// ```
macro_rules! printfl {
($w:expr, $($tt:tt)*) => {{
$w.write(&format!($($tt)*).as_bytes()).ok().expect("write() fail");
$w.flush().ok().expect("flush() fail");
}}
}

#[macro_use]
extern crate time;
mod tty;
mod pb;
mod multi;
pub use pb::{ProgressBar, Units};
pub use multi::MultiBar;
use std::io::{Write, Stdout, stdout};

pub struct PbIter<T, I>
where I: Iterator,
T: Write
where I: Iterator,
T: Write
{
iter: I,
progress_bar: ProgressBar<T>,
}

impl<I> PbIter<Stdout, I>
where I: Iterator
where I: Iterator
{
pub fn new(iter: I) -> Self {
Self::on(stdout(), iter)
}
}

impl<T, I> PbIter<T, I>
where I: Iterator,
T: Write
where I: Iterator,
T: Write
{
pub fn on(handle: T, iter: I) -> Self {
let size = iter.size_hint().0;
PbIter {iter: iter, progress_bar: ProgressBar::on(handle, size as u64)}
PbIter {
iter: iter,
progress_bar: ProgressBar::on(handle, size as u64),
}
}
}

impl<T, I> Iterator for PbIter<T, I>
where I: Iterator,
T: Write
where I: Iterator,
T: Write
{
type Item = I::Item;

Expand Down
Loading

0 comments on commit 8c25c80

Please sign in to comment.