Struct termprogress::progress::Bar

source ·
pub struct Bar<T: ?Sized = Stdout> { /* private fields */ }
Expand description

A progress bar with a size and optionally title. It implements the ProgressBar trait, and is the default progress bar.

The bar has a max size, that is usually derived from the size of your terminal (if it can be detected) or can be set yourself, to stop the title line going over the side. It also has a configurable width, which is defaulted to 50.

Usage

To create a new progress::Bar with a max size tuned to your terminal (or Width+20, if it cannot be detected), and of the default size, Bar implements the Default trait:

let mut bar = Bar::default(); //Creates a bar of width 50 by default.

You can configure sizes and initial title with new(), with_title(), and with_max() functions.

How it looks

It renders in the terminal like: [========================= ]: 50% this is a title that may get cut if it reaches max le...

Thread Syncsafety

This type is safely Sync (where T is), the behaviour is defined to prevent overlapping writes to T. Though it is advised to not render a Bar from more than a single thread, you still safely can.

Rendering functions should not be called on multiple threads at the same time, though it is safe to do so. A thread-sync’d rendering operation will safetly (and silently) give up before writing if another thread is already engaging in one.

A display operation on one thread will cause any other threads attempting on to silently and safely abort their display attempt before anything is written to output.

Implementations§

source§

impl Bar

source

pub fn with_title_default(width: usize, title: impl AsRef<str>) -> Self

Create a new bar width long with a title using the default output descriptor stdout.

source

pub fn try_new_with_title_default( width: usize, title: impl AsRef<str> ) -> Option<Self>

Attempt to create a new bar with max display width of our terminal and a title.

If stdout is not a terminal, then None is returned.

source

pub fn new_default(width: usize) -> Self

Create a new bar with max display width of our terminal

Notes

Without feature size, will be the same as Self::with_max(width, width +20)

To try to create one that always adheres to size, use the try_new() family of functions.

source

pub fn try_new_default(width: usize) -> Option<Self>

Attempt to create a new bar with max display width of our terminal.

If stdout is not a terminal, then None is returned.

source

pub fn try_new_default_size_default() -> Option<Self>

Attempt to create a new bar with max display width of our terminal.

If stdout is not a terminal, then None is returned.

source

pub fn with_max_default(width: usize, max_width: usize) -> Self

Create a bar with a max display width

Panics

If width is larger than or equal to max_width.

source§

impl<T: Write + AsRawFd> Bar<T>

source

pub fn with_title( output: impl Into<T> + AsRawFd, width: usize, title: impl AsRef<str> ) -> Self

Create a new bar width long with a title.

source

pub fn try_new_with_title( output: impl Into<T> + AsRawFd, width: usize, title: impl AsRef<str> ) -> Option<Self>

Attempt to create a new bar with max display width of our terminal and a title.

If output is not a terminal, then None is returned.

source

pub fn new(output: impl Into<T> + AsRawFd, width: usize) -> Self

Create a new bar with max display width of our terminal

Notes

Without feature size, will be the same as Self::with_max(width, width +20)

To try to create one that always adheres to size, use the try_new() family of functions.

source

pub fn try_new(output: impl Into<T> + AsRawFd, width: usize) -> Option<Self>

Attempt to create a new bar with max display width of our terminal.

If output is not a terminal, then None is returned.

source

pub fn try_new_default_size(to: impl Into<T> + AsRawFd) -> Option<Self>

Attempt to create a new bar with max display width of our terminal.

If output is not a terminal, then None is returned.

source

pub fn with_max(output: impl Into<T>, width: usize, max_width: usize) -> Self

Create a bar with a max display width

Panics

If width is larger than or equal to max_width.

source§

impl<T: ?Sized + Write + AsRawFd> Bar<T>

source

pub fn fit(&mut self) -> bool

Fit to terminal’s width if possible.

Notes

Available with feature size only.

Returns

If the re-fit succeeded. A fit() will also fail if another thread is already engaging in a display operation.

source

pub fn update(&mut self)

Update the buffer.

source§

impl<T: Write> Bar<T>

source

pub fn complete(self) -> Result<()>

Consume the bar and complete it, regardless of progress.

Trait Implementations§

source§

impl<T: Debug + ?Sized> Debug for Bar<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Bar

source§

fn default() -> Self

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

impl<T: ?Sized + Write + AsRawFd> Display for Bar<T>

source§

fn refresh(&self)

Refresh the display
source§

fn blank(&self)

Blank the display
source§

fn get_title(&self) -> &str

Get the title for this display
source§

fn set_title(&mut self, from: &str)

Set the title for this display
source§

fn update_dimensions(&mut self, to: usize)

Update the max size if needed
source§

fn println(&self, string: &str)

Blank then print a line, and redisplay.
source§

fn eprintln(&self, string: &str)

Blank then print a line std stderr, and redisplay.
source§

impl<T: ?Sized + Write + AsRawFd> ProgressBar for Bar<T>

source§

fn get_progress(&self) -> f64

source§

fn set_progress(&mut self, value: f64)

source§

impl<T: Write + AsRawFd> WithTitle for Bar<T>

source§

fn add_title(&mut self, string: impl AsRef<str>)

Add a title to this indicator.
source§

fn update(&mut self)

source§

fn complete(self)

source§

fn with_title(self, string: impl AsRef<str>) -> Self

Add a title to this indicator.

Auto Trait Implementations§

§

impl<T = Stdout> !RefUnwindSafe for Bar<T>

§

impl<T: ?Sized> Send for Bar<T>where T: Send,

§

impl<T: ?Sized> Sync for Bar<T>where T: Send + Sync,

§

impl<T: ?Sized> Unpin for Bar<T>where T: Unpin,

§

impl<T: ?Sized> UnwindSafe for Bar<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.