Bar

Struct Bar 

Source
pub struct Bar { /* private fields */ }

Implementations§

Source§

impl Bar

Core implementation of console progress bar.

§Example

A simple progress bar with a max value.


fn main() {
    use rpb::bar::Bar;
    let mut bar = Bar::new(100);

    for _ in 0..100 {
        bar.add(1);
    }
}
Source

pub fn new(max: i64) -> Self

Create a new instance of Bar with a max value.

§Example
use rpb::bar::Bar;
let mut bar = Bar::new(100);
Examples found in repository?
examples/basic.rs (line 6)
5fn main() {
6    let mut bar = Bar::new(100);
7    for _i in 0..100 {
8        bar.add(1);
9        sleep(time::Duration::from_millis(100))
10    }
11}
More examples
Hide additional examples
examples/custom.rs (line 7)
6fn main() {
7    let mut bar = Bar::new(100);
8    let mut bar2 = Bar::new(100);
9    let mut bar3 = Bar::new(100);
10    let mut bar4 = Bar::new(100);
11
12    bar.set_theme(Themes::Basic);
13    bar.set_description("bar1");
14
15    bar2.set_theme(Themes::Small);
16    bar2.set_description("bar2");
17
18    bar3.set_theme(Themes::ColoredSmall);
19    bar3.set_description("bar3");
20
21    bar4.set_theme(Themes::ColoredMedium);
22    bar4.set_description("bar4");
23
24    bar2.set_position(1);
25    bar3.set_position(2);
26    bar4.set_position(3);
27
28    for _i in 0..100 {
29        bar.add(1);
30        bar2.add(1);
31        bar3.add(1);
32        bar4.add(1);
33        sleep(time::Duration::from_millis(50))
34    }
35    eprint!("{}", "\n".repeat(4));
36}
Source

pub fn default_bytes(max_bytes: i64, desc: &str) -> Bar

DefaultBytes provides a progressbar to measure byte throughput with recommended defaults.

§Example:
use rpb::bar::Bar;
let mut bar = Bar::default_bytes(100, "downloading");
Examples found in repository?
examples/download.rs (line 8)
5fn main() -> io::Result<()> {
6    let source = File::open("indicateurs_v2.sql")?;
7    let mut target = File::create("src.sql")?;
8    let bar = Bar::default_bytes(source.metadata()?.len() as i64, "downloading");
9    io::copy(&mut bar.reader(source), &mut target).unwrap();
10    Ok(())
11}
Source

pub fn set_theme(&mut self, theme: Themes)

Instantiate a new theme.

§Example
use rpb::bar::Bar;
use rpb::styles::Themes;
let mut bar = Bar::new(100);
bar.set_theme(Themes::Small);
Examples found in repository?
examples/custom.rs (line 12)
6fn main() {
7    let mut bar = Bar::new(100);
8    let mut bar2 = Bar::new(100);
9    let mut bar3 = Bar::new(100);
10    let mut bar4 = Bar::new(100);
11
12    bar.set_theme(Themes::Basic);
13    bar.set_description("bar1");
14
15    bar2.set_theme(Themes::Small);
16    bar2.set_description("bar2");
17
18    bar3.set_theme(Themes::ColoredSmall);
19    bar3.set_description("bar3");
20
21    bar4.set_theme(Themes::ColoredMedium);
22    bar4.set_description("bar4");
23
24    bar2.set_position(1);
25    bar3.set_position(2);
26    bar4.set_position(3);
27
28    for _i in 0..100 {
29        bar.add(1);
30        bar2.add(1);
31        bar3.add(1);
32        bar4.add(1);
33        sleep(time::Duration::from_millis(50))
34    }
35    eprint!("{}", "\n".repeat(4));
36}
Source

pub fn set_position(&mut self, position: u32)

Sets the position of the progress bar. By default, the progress bar are position 0.

§Example
use rpb::bar::Bar;
let mut bar = Bar::new(100);
bar.set_position(1)
Examples found in repository?
examples/custom.rs (line 24)
6fn main() {
7    let mut bar = Bar::new(100);
8    let mut bar2 = Bar::new(100);
9    let mut bar3 = Bar::new(100);
10    let mut bar4 = Bar::new(100);
11
12    bar.set_theme(Themes::Basic);
13    bar.set_description("bar1");
14
15    bar2.set_theme(Themes::Small);
16    bar2.set_description("bar2");
17
18    bar3.set_theme(Themes::ColoredSmall);
19    bar3.set_description("bar3");
20
21    bar4.set_theme(Themes::ColoredMedium);
22    bar4.set_description("bar4");
23
24    bar2.set_position(1);
25    bar3.set_position(2);
26    bar4.set_position(3);
27
28    for _i in 0..100 {
29        bar.add(1);
30        bar2.add(1);
31        bar3.add(1);
32        bar4.add(1);
33        sleep(time::Duration::from_millis(50))
34    }
35    eprint!("{}", "\n".repeat(4));
36}
Source

pub fn set_description(&mut self, desc: &str)

Sets description of progress bar.

Examples found in repository?
examples/custom.rs (line 13)
6fn main() {
7    let mut bar = Bar::new(100);
8    let mut bar2 = Bar::new(100);
9    let mut bar3 = Bar::new(100);
10    let mut bar4 = Bar::new(100);
11
12    bar.set_theme(Themes::Basic);
13    bar.set_description("bar1");
14
15    bar2.set_theme(Themes::Small);
16    bar2.set_description("bar2");
17
18    bar3.set_theme(Themes::ColoredSmall);
19    bar3.set_description("bar3");
20
21    bar4.set_theme(Themes::ColoredMedium);
22    bar4.set_description("bar4");
23
24    bar2.set_position(1);
25    bar3.set_position(2);
26    bar4.set_position(3);
27
28    for _i in 0..100 {
29        bar.add(1);
30        bar2.add(1);
31        bar3.add(1);
32        bar4.add(1);
33        sleep(time::Duration::from_millis(50))
34    }
35    eprint!("{}", "\n".repeat(4));
36}
Source

pub fn set_spinner(&mut self, spinner: Spinners)

Sets spinner of progress bar.

Source

pub fn inc(&mut self)

Increment current value of one.

Source

pub fn set_unit(&mut self, u: Units)

Set units, default is Units::Default

§Examples
use rpb::bar::{Bar, Units};

let n_bytes = 100;
let mut bar = Bar::new(n_bytes);
bar.set_unit(Units::Bytes);
Source

pub fn reader<R: Read>(&self, read: R) -> BarIter<R>

Wraps an io::Read with the progress bar

§Example:
use std::fs::File;
use std::io;
use rpb::bar::Bar;
use rpb::bar::Units;

fn main () -> io::Result<()> {
    let src = File::open("src.txt")?;
    let mut tgt = File::create("tgt.txt")?;
    let mut bar = Bar::new(src.metadata()?.len() as i64);
    bar.set_unit(Units::Bytes);
    io::copy(&mut bar.reader(src), &mut tgt).unwrap();
    Ok(())
}
Examples found in repository?
examples/download.rs (line 9)
5fn main() -> io::Result<()> {
6    let source = File::open("indicateurs_v2.sql")?;
7    let mut target = File::create("src.sql")?;
8    let bar = Bar::default_bytes(source.metadata()?.len() as i64, "downloading");
9    io::copy(&mut bar.reader(source), &mut target).unwrap();
10    Ok(())
11}
Source

pub fn writer<R: Write>(&self, write: R) -> BarIter<R>

Wraps an io::Write with the progress bar

§Example:
use std::fs::File;
use std::io;
use rpb::bar::Bar;
use rpb::bar::Units;

fn main () -> io::Result<()> {
    let mut src = File::open("src.txt")?;
    let mut tgt = File::create("tgt.txt")?;
    let mut bar = Bar::new(src.metadata()?.len() as i64);
    bar.set_unit(Units::Bytes);
    io::copy(&mut src, &mut bar.writer(tgt)).unwrap();
    Ok(())
}
Source

pub fn add(&mut self, num: usize)

Manually update the progress bar, advances the position by num.

Examples found in repository?
examples/basic.rs (line 8)
5fn main() {
6    let mut bar = Bar::new(100);
7    for _i in 0..100 {
8        bar.add(1);
9        sleep(time::Duration::from_millis(100))
10    }
11}
More examples
Hide additional examples
examples/custom.rs (line 29)
6fn main() {
7    let mut bar = Bar::new(100);
8    let mut bar2 = Bar::new(100);
9    let mut bar3 = Bar::new(100);
10    let mut bar4 = Bar::new(100);
11
12    bar.set_theme(Themes::Basic);
13    bar.set_description("bar1");
14
15    bar2.set_theme(Themes::Small);
16    bar2.set_description("bar2");
17
18    bar3.set_theme(Themes::ColoredSmall);
19    bar3.set_description("bar3");
20
21    bar4.set_theme(Themes::ColoredMedium);
22    bar4.set_description("bar4");
23
24    bar2.set_position(1);
25    bar3.set_position(2);
26    bar4.set_position(3);
27
28    for _i in 0..100 {
29        bar.add(1);
30        bar2.add(1);
31        bar3.add(1);
32        bar4.add(1);
33        sleep(time::Duration::from_millis(50))
34    }
35    eprint!("{}", "\n".repeat(4));
36}

Trait Implementations§

Source§

impl Clone for Bar

Source§

fn clone(&self) -> Bar

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Bar

§

impl RefUnwindSafe for Bar

§

impl Send for Bar

§

impl Sync for Bar

§

impl Unpin for Bar

§

impl UnwindSafe for Bar

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.