pub struct Bar {Show 24 fields
pub animation: Animation,
pub bar_format: Option<Template>,
pub colour: Option<Colour>,
pub desc: String,
pub delay: f32,
pub disable: bool,
pub dynamic_miniters: bool,
pub dynamic_ncols: bool,
pub force_refresh: bool,
pub inverse_unit: bool,
pub leave: bool,
pub mininterval: f32,
pub miniters: usize,
pub ncols: Option<u16>,
pub position: u16,
pub postfix: String,
pub total: usize,
pub spinner: Option<Spinner>,
pub unit: String,
pub unit_divisor: usize,
pub unit_scale: bool,
pub writer: Writer,
pub bar_length: u16,
pub counter: usize,
/* private fields */
}
Expand description
Core implemention of console progress bar.
Example
use kdam::{tqdm, Bar, BarExt};
let mut pb = Bar::new(100);
// let mut pb = tqdm!(total = 100);
// let mut pb = Bar::builder().total(100).build().unwrap();
for _ in 0..100 {
pb.update(1).unwrap();
}
Fields§
§animation: Animation
§bar_format: Option<Template>
§colour: Option<Colour>
§desc: String
§delay: f32
§disable: bool
§dynamic_miniters: bool
§dynamic_ncols: bool
§force_refresh: bool
§inverse_unit: bool
§leave: bool
§mininterval: f32
§miniters: usize
§ncols: Option<u16>
§position: u16
§postfix: String
§total: usize
§spinner: Option<Spinner>
§unit: String
§unit_divisor: usize
§unit_scale: bool
§writer: Writer
§bar_length: u16
§counter: usize
Implementations§
source§impl Bar
impl Bar
sourcepub fn builder() -> BarBuilder
pub fn builder() -> BarBuilder
Create a new BarBuilder.
Example
use kdam::Bar;
let pb = Bar::builder().total(100).build().unwrap();
sourcepub fn set_bar_format<T: Into<String>>(
&mut self,
bar_format: T
) -> Result<(), String>
Available on crate feature template
only.
pub fn set_bar_format<T: Into<String>>( &mut self, bar_format: T ) -> Result<(), String>
template
only.Set/Modify bar_format property.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<()> {
let mut pb = tqdm!(
total = 300,
ncols = 40,
force_refresh = true,
bar_format = "{desc suffix=' '}|{animation}| {spinner} {count}/{total} [{percentage:.0}%] in {elapsed human=true} ({rate:.1}/s, eta: {remaining human=true})",
spinner = Spinner::new(
&["▁▂▃", "▂▃▄", "▃▄▅", "▄▅▆", "▅▆▇", "▆▇█", "▇█▇", "█▇▆", "▇▆▅", "▆▅▄", "▅▄▃", "▄▃▂", "▃▂▁"],
30.0,
1.0,
)
);
for _ in 0..300 {
std::thread::sleep(std::time::Duration::from_secs_f32(0.02));
pb.update(1)?;
}
pb.set_bar_format("{desc suffix=' '}|{animation}| {count}/{total} [{percentage:.0}%] in {elapsed human=true} ({rate:.1}/s)").unwrap();
pb.clear()?;
pb.refresh()?;
eprintln!();
Ok(())
}
sourcepub fn set_description<T: Into<String>>(&mut self, description: T)
pub fn set_description<T: Into<String>>(&mut self, description: T)
Set/Modify description property.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() -> Result<()> {
let mut pb = tqdm!(total = 10);
pb.set_postfix(format!("str={}, lst={:?}", "h", [1, 2]));
pb.refresh()?;
for i in 0..10 {
std::thread::sleep(std::time::Duration::from_secs_f32(0.5));
pb.set_description(format!("GEN {}", i));
pb.update(1)?;
}
eprintln!();
Ok(())
}
sourcepub fn set_postfix<T: Into<String>>(&mut self, postfix: T)
pub fn set_postfix<T: Into<String>>(&mut self, postfix: T)
Set/Modify postfix property.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() -> Result<()> {
let mut pb = tqdm!(total = 10);
pb.set_postfix(format!("str={}, lst={:?}", "h", [1, 2]));
pb.refresh()?;
for i in 0..10 {
std::thread::sleep(std::time::Duration::from_secs_f32(0.5));
pb.set_description(format!("GEN {}", i));
pb.update(1)?;
}
eprintln!();
Ok(())
}
sourcepub fn completed(&self) -> bool
pub fn completed(&self) -> bool
Returns whether progress is completed or not.
If total
is 0
, it always returns false
.
sourcepub fn elapsed_time(&mut self) -> f32
pub fn elapsed_time(&mut self) -> f32
Set and returns progress elapsed time.
Examples found in repository?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() -> Result<()> {
term::init(stderr().is_terminal());
term::hide_cursor()?;
let mut pb = RichProgress::new(
tqdm!(
total = 231231231,
unit_scale = true,
unit_divisor = 1024,
unit = "B"
),
vec![
Column::Spinner(Spinner::new(
&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
80.0,
1.0,
)),
Column::Text("[bold blue]?".to_owned()),
Column::Animation,
Column::Percentage(1),
Column::Text("•".to_owned()),
Column::CountTotal,
Column::Text("•".to_owned()),
Column::Rate,
Column::Text("•".to_owned()),
Column::RemainingTime,
],
);
pb.write("download will begin in 5 seconds".colorize("bold red"))?;
while pb.pb.elapsed_time() <= 5.0 {
pb.refresh()?;
}
pb.replace(1, Column::Text("[bold blue]docker.exe".to_owned()));
pb.write("downloading docker.exe".colorize("bold cyan"))?;
let total_size = 231231231;
let mut downloaded = 0;
while downloaded < total_size {
let new = std::cmp::min(downloaded + 223211, total_size);
downloaded = new;
pb.update_to(new)?;
std::thread::sleep(std::time::Duration::from_millis(12));
}
pb.write("downloaded docker.exe".colorize("bold green"))?;
eprintln!();
Ok(())
}
sourcepub fn fmt_counter(&self) -> String
pub fn fmt_counter(&self) -> String
Returns formatted counter value.
sourcepub fn fmt_elapsed_time(&self) -> String
pub fn fmt_elapsed_time(&self) -> String
Returns formatted elapsed time.
sourcepub fn fmt_percentage(&self, precision: usize) -> String
pub fn fmt_percentage(&self, precision: usize) -> String
Returns formatted progress percentage.
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn render(&mut self) -> String {
let fmt_percentage = self.pb.fmt_percentage(0);
let padding = 1 + fmt_percentage.chars().count() as u16 + self.pb.animation.spaces() as u16;
let ncols = self.pb.ncols_for_animation(padding);
if ncols == 0 {
self.pb.bar_length = padding - 1;
fmt_percentage
} else {
self.pb.bar_length = padding + ncols;
self.pb.animation.fmt_render(
NonZeroU16::new(ncols).unwrap(),
self.pb.percentage(),
&None,
) + " "
+ &fmt_percentage
}
}
sourcepub fn fmt_remaining_time(&self) -> String
pub fn fmt_remaining_time(&self) -> String
Returns formatted remaining time.
sourcepub fn indefinite(&self) -> bool
pub fn indefinite(&self) -> bool
Returns whether progress is indefinite i.e. total
is 0
or not.
sourcepub fn ncols_for_animation(&mut self, padding: u16) -> u16
pub fn ncols_for_animation(&mut self, padding: u16) -> u16
Set and returns number of columns for bar animation with given padding.
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn render(&mut self) -> String {
let fmt_percentage = self.pb.fmt_percentage(0);
let padding = 1 + fmt_percentage.chars().count() as u16 + self.pb.animation.spaces() as u16;
let ncols = self.pb.ncols_for_animation(padding);
if ncols == 0 {
self.pb.bar_length = padding - 1;
fmt_percentage
} else {
self.pb.bar_length = padding + ncols;
self.pb.animation.fmt_render(
NonZeroU16::new(ncols).unwrap(),
self.pb.percentage(),
&None,
) + " "
+ &fmt_percentage
}
}
sourcepub fn percentage(&self) -> f32
pub fn percentage(&self) -> f32
Returns progress percentage, like 0.62
, 0.262
, 1.0
.
If total
is 0
, it always returns 1.0
.
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn render(&mut self) -> String {
let fmt_percentage = self.pb.fmt_percentage(0);
let padding = 1 + fmt_percentage.chars().count() as u16 + self.pb.animation.spaces() as u16;
let ncols = self.pb.ncols_for_animation(padding);
if ncols == 0 {
self.pb.bar_length = padding - 1;
fmt_percentage
} else {
self.pb.bar_length = padding + ncols;
self.pb.animation.fmt_render(
NonZeroU16::new(ncols).unwrap(),
self.pb.percentage(),
&None,
) + " "
+ &fmt_percentage
}
}
sourcepub fn rate(&self) -> f32
pub fn rate(&self) -> f32
Returns progress/iterations rate.
Note
Before calling this method, elapsed_time method should be called.
sourcepub fn remaining_time(&self) -> f32
pub fn remaining_time(&self) -> f32
Returns remaining time (ETA) for progress completion.
If total
is 0
, it always returns infinity.
sourcepub fn should_refresh(&mut self) -> bool
pub fn should_refresh(&mut self) -> bool
Returns whether to trigger a display update or not.