Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/a8m/pb into wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
dunnock committed Nov 18, 2019
2 parents a6654e1 + a69a748 commit ec58291
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 113 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: rust

sudo: required

rust:
- stable
- beta
- nightly

matrix:
allow_failures:
- rust: nightly

script:
- cargo test --all --verbose
- rustup component add rustfmt-preview
# disable it until the formatting will be
# consistent between stable, beta and nightly.
# - cargo fmt -- --check
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
[package]
name = "pbr"
version = "1.0.0"
version = "1.0.2"
authors = ["Ariel Mashraki <ariel@mashraki.co.il>", "Steven Fackler <sfackler@gmail.com>"]
description = "Console progress bar for Rust"
documentation = "http://a8m.github.io/pb/doc/pbr/index.html"
repository = "https://github.com/a8m/pb"
exclude = ["gif/", ".travis.yml"]
keywords = ["cli", "progress", "terminal", "pb"]
license = "MIT"

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

[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = ["wincon", "processenv", "winbase"]

[target.'cfg(target_os = "redox")'.dependencies]
termion = "1.4"

[dev-dependencies]
rand = "0.3.14"
rand = "0.5"
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Terminal progress bar for Rust

[![Latest version](https://img.shields.io/crates/v/pbr.svg)](https://crates.io/crates/pbr)
[![License](https://img.shields.io/crates/l/pbr.svg)](https://github.com/a8m/pb/blob/master/LICENSE.md)
[![Docs](https://img.shields.io/badge/docs-reference-blue.svg)](https://a8m.github.io/pb/doc/pbr/index.html)
[![Build Status](https://travis-ci.org/a8m/pb.svg?branch=master)](https://travis-ci.org/a8m/pb)
[![Gitter](https://badges.gitter.im/a8m/pb.svg)](https://gitter.im/a8m/pb?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

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_v3.gif)

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

### Examples
1. simple example
Expand Down Expand Up @@ -70,7 +76,7 @@ fn main() {
}
```

3. Broadcast writing(simple file copying)
3. Broadcast writing (simple file copying)

```rust
#![feature(io)]
Expand Down
11 changes: 5 additions & 6 deletions examples/multi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate rand;
extern crate pbr;
use rand::Rng;
extern crate rand;
use pbr::MultiBar;
use rand::prelude::*;
use std::thread;
use std::time::Duration;

Expand All @@ -23,7 +23,7 @@ fn main() {
pb.tick();
}
for _ in 0..20 {
let n = rand::thread_rng().gen_range(0, 100);
let n = thread_rng().gen_range(0, 100);
pb.message("Connected: ");
thread::sleep(Duration::from_millis(n));
pb.inc();
Expand All @@ -38,7 +38,6 @@ fn main() {
});
}


mb.println("");
mb.println("Text lines separate between two sections: ");
mb.println("");
Expand All @@ -49,7 +48,7 @@ fn main() {
thread::spawn(move || {
for _ in 0..count {
pb.inc();
let n = rand::thread_rng().gen_range(0, 100);
let n = thread_rng().gen_range(0, 100);
thread::sleep(Duration::from_millis(n));
}
pb.finish();
Expand All @@ -64,7 +63,7 @@ fn main() {
fn rand_string() -> String {
let mut v = Vec::new();
while v.len() < 12 {
let b = rand::random::<u8>();
let b = random::<u8>();
// [0-9a-f]
if b > 47 && b < 58 || b > 96 && b < 103 {
v.push(b);
Expand Down
6 changes: 3 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate rand;
extern crate pbr;
use rand::Rng;
extern crate rand;
use pbr::ProgressBar;
use rand::prelude::*;
use std::thread;
use std::time::Duration;

Expand All @@ -11,7 +11,7 @@ fn main() {
pb.format("╢▌▌░╟");
for _ in 0..count {
pb.inc();
let n = rand::thread_rng().gen_range(0, 100);
let n = thread_rng().gen_range(0, 100);
thread::sleep(Duration::from_millis(n));
}
pb.finish_println("done!");
Expand Down
27 changes: 15 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,36 @@ macro_rules! printfl {
}}
}

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

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;
Expand All @@ -150,8 +152,9 @@ impl<T, I> PbIter<T, I>
}

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

Expand Down
35 changes: 20 additions & 15 deletions src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use pb::ProgressBar;
use std::io::{Result, Stdout, Write};
use std::str::from_utf8;
use tty::move_cursor_up;
use std::io::{Stdout, Result, Write};
use std::sync::mpsc;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::{Receiver, Sender};
use tty::move_cursor_up;

pub struct MultiBar<T: Write> {
nlines: usize,
Expand All @@ -25,10 +25,12 @@ impl MultiBar<Stdout> {
/// ```no_run
/// use std::thread;
/// use pbr::MultiBar;
/// use std::time::Duration;
///
/// let mut mb = MultiBar::new();
/// mb.println("Application header:");
///
/// # let count = 250;
/// let mut p1 = mb.create_bar(count);
/// let _ = thread::spawn(move || {
/// for _ in 0..count {
Expand Down Expand Up @@ -97,12 +99,13 @@ impl<T: Write> MultiBar<T> {
/// let mut mb = MultiBar::new();
/// mb.println("Application header:");
///
/// let mut p1 = MultiBar::create_bar(count);
/// # let count = 250;
/// let mut p1 = mb.create_bar(count);
/// // ...
///
/// mb.println("Text line between bar1 and bar2");
///
/// let mut p2 = MultiBar::create_bar(count);
/// let mut p2 = mb.create_bar(count);
/// // ...
///
/// mb.println("Text line between bar2 and bar3");
Expand Down Expand Up @@ -131,35 +134,37 @@ impl<T: Write> MultiBar<T> {
/// use pbr::MultiBar;
///
/// let mut mb = MultiBar::new();
/// # let (count1, count2, count3) = (250, 62500, 15625000);
///
/// // progress bar in level 1
/// let mut p1 = MultiBar::create_bar(count1);
/// let mut p1 = mb.create_bar(count1);
/// // ...
///
/// // progress bar in level 2
/// let mut p2 = MultiBar::create_bar(count2);
/// let mut p2 = mb.create_bar(count2);
/// // ...
///
/// // progress bar in level 3
/// let mut p3 = MultiBar::create_bar(count3);
/// let mut p3 = mb.create_bar(count3);
///
/// // ...
/// mb.listen();
/// ```
pub fn create_bar(&mut self, total: u64) -> ProgressBar<Pipe> {
self.println("");
self.nbars += 1;
let mut p = ProgressBar::on(Pipe {
level: self.nlines - 1,
chan: self.chan.0.clone(),
},
total);
let mut p = ProgressBar::on(
Pipe {
level: self.nlines - 1,
chan: self.chan.0.clone(),
},
total,
);
p.is_multibar = true;
p.add(0);
p
}


/// listen start listen to all bars changes.
///
/// `ProgressBar` that finish its work, must call `finish()` (or `finish_print`)
Expand All @@ -172,6 +177,7 @@ impl<T: Write> MultiBar<T> {
/// # Examples
///
/// ```no_run
/// use std::thread;
/// use pbr::MultiBar;
///
/// let mut mb = MultiBar::new();
Expand All @@ -191,7 +197,6 @@ impl<T: Write> MultiBar<T> {
let mut first = true;
let mut nbars = self.nbars;
while nbars > 0 {

// receive message
let msg = self.chan.1.recv().unwrap();
if msg.done {
Expand Down
Loading

0 comments on commit ec58291

Please sign in to comment.