ProgressBar

Struct ProgressBar 

Source
pub struct ProgressBar { /* private fields */ }
Expand description

A convenient progress bar.

See the module documentation for example code and more documentation.

Implementations§

Source§

impl ProgressBar

Source

pub fn new() -> Self

Creates a new progress bar.

use headway::ProgressBar;

let p = ProgressBar::new().with_message("Calibrating flux capacitors");
for _ in p.wrap(0..100) {
    sleep(Duration::from_millis(20));
}
Source

pub fn hidden() -> Self

Creates a new progress bar which will never be rendered.

This can be useful if you need to pass a progress bar to some function, but you don’t actually want a bar to show up.

Source

pub fn split_weighted(self) -> ProgressBarWeightedNester

Splits the bar into children of given proportions.

This is useful if you have many tasks, but you only want to show a single progress bar. Then you can split the bar so that for example the first task gets 40% of the bar and the second task gets 60% of the bar.

The display will automatically change to percentages if this mode of splitting is used.

use headway::{ProgressBar, ProgressBarIterable};

let mut p = ProgressBar::new().split_weighted();
let first_half = p.take(0.4).with_message("First part");
let second_half = p.take(0.6).with_message("Second part");
for _ in (0..50).progress_with(first_half) {
    sleep(Duration::from_millis(20));
}
for _ in (0..50).progress_with(second_half) {
    sleep(Duration::from_millis(30));
}
Source

pub fn split_sized(self) -> ProgressBarSizedNester

Splits the bar into children of fixed sizes.

Each child bar will represent N items of the parent. Note that the children can have any length, but a filled child bar will be remapped to N items in the parent.

use headway::ProgressBar;

let mut p = ProgressBar::new().split_sized();
// Create the bars up front so that the bar knows how many items
// there are in total.
let first = p.take(5).with_message("First");
let second = p.take(20).with_message("Second");

for _ in first.wrap(0..5) {
    sleep(Duration::from_millis(300));
}

// Here we only loop over 5 items, but we make the child bar represent
// 20 items in the parent bar.
for _ in second.wrap(0..5) {
    sleep(Duration::from_millis(300));
}
Source

pub fn split_summed(self) -> ProgressBarSummedNester

Splits the bar into children and displays the summed progress of all children.

use headway::ProgressBar;

let p = ProgressBar::new().split_summed();
let tasks = (0..4)
    .map(|_| {
        let child_bar = p.take();
        std::thread::spawn(move || {
            for _ in child_bar.wrap(0..100) {
                sleep(Duration::from_millis(20));
            }
        })
    })
    .collect::<Vec<_>>();
for task in tasks {
    task.join().unwrap()
}
Source

pub fn split_each<It: Iterator>( self, it: It, ) -> impl Iterator<Item = (ProgressBar, It::Item)>

Splits the bar into N bars, each representing an item in the iterator.

This is useful if each item takes a long time and you want progress for it. It is also useful if you are sending each item to separate threads to process them independently.

use headway::ProgressBar;

let p = ProgressBar::new();
// Split the progress bar into 10 nested bars
for (nested_bar, i) in p.split_each(0..10) {
    // Wrap the nested bar around an iterator representing this subtask
    nested_bar.set_message(format!("Subtask {}", i));
    for _ in nested_bar.wrap(0..200) {
        sleep(Duration::from_millis(5));
    }
}
Source

pub fn length(&self) -> Option<usize>

Length of the bar, if it has been set

Source

pub fn set_length(&self, len: usize)

Sets the length of this progress bar.

This has no effect if the bar has already been finished or abandoned.

Source

pub fn set_position(&self, pos: usize)

Sets the amount of progress this bar has made.

Should usually be between 0 and Self::length.

This has no effect if the bar has already been finished or abandoned.

Source

pub fn clear_message(&self)

Clears any message set using Self::set_message or Self::with_message.

Source

pub fn with_message(self, message: impl Into<String>) -> Self

Equivalent to Self::set_message, but may be more ergonomic in some situations since it returns self.

Source

pub fn with_length(self, length: usize) -> Self

Equivalent to Self::set_length, but may be more ergonomic in some situations since it returns self.

Source

pub fn set_message(&self, message: impl Into<String>)

Sets a message which will show up next to the bar.

If the root bar has been split into multiple children, then the message that is displayed is from the first bar that is not finished. Or if all bars are finished then the last bar with a message will be used.

Source

pub fn inc(&self)

Increments the progress of this bar by 1.

Usually it’s more convenient to work with the iterator-wrapping functions like Self::wrap

Source

pub fn finish_with_message(&mut self, message: impl Into<String>)

Marks the bar as finished and sets the message.

Equivalent to first setting the message and then marking the bar as finished.

Source

pub fn abandon(&mut self)

Abandons the progress bar.

The remaining part of the progress bar will be colored red to indicate it will never be completed. Progress bars are automatically marked as abandoned when they are dropped and they are only partially complete.

Source

pub fn finish(&mut self)

Marks the bar as finished.

If the bar has a length, the position of the bar will be set to Self::length.

Source

pub fn wrap<It: Iterator>(self, it: It) -> ProgressBarIterator<It>

Wraps the bar around an iterator.

If the iterator has a known length, the bar’s length will be set to that length. The iterator will headway the progress by 1 each step. When reaching the end of the iterator, the bar will be marked as finished.

See also ProgressBarIterable::progress and ProgressBarIterable::progress_with

use headway::ProgressBar;

let p = ProgressBar::new().with_message("Calibrating flux capacitors");
for _ in p.wrap(0..100) {
    sleep(Duration::from_millis(20));
}

Trait Implementations§

Source§

impl Default for ProgressBar

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for ProgressBar

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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> 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, 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.