progress_bar.rs
This file
Path
src/output/progress_bar.rs
70 lines · 2.6 KiB · Rust
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
The walk stopped after 200 commits: lines older than 0f91d0c point at it.
Merge pull request #581 from devonhollowood/elapsedDavid Peterbefore adoption5d501b6
lines 1–3why, one step back1use indicatif::{FormattedDuration, ProgressBar, ProgressState, ProgressStyle};2use std::fmt;3use std::time::{Duration, Instant};Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 4–12why, one step back45use crate::options::OutputStyleOption;67#[cfg(not(windows))]8const TICK_SETTINGS: (&str, u64) = ("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏ ", 80);910#[cfg(windows)]11const TICK_SETTINGS: (&str, u64) = (r"+-x| ", 200);12Merge pull request #581 from devonhollowood/elapsedDavid Peterbefore adoption5d501b6
lines 13–18why, one step back13// Keep the message area 30 columns wide across preparation, the initial run,14// and estimates so that switching between them does not move the bar.15const DEFAULT_TEMPLATE: &str = " {spinner} {msg:<30} {wide_bar} ETA {eta_precise} ";16const INITIAL_TEMPLATE: &str =17 " {spinner} Initial run: {initial_elapsed:<17} {wide_bar} ETA {eta_precise} ";18Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 19–33why, one step back19fn style_with_template(bar: &ProgressBar, template: &str) -> ProgressStyle {20 let template = if bar.prefix().is_empty() {21 template.to_owned()22 } else {23 format!("{{prefix}}\n{template}")24 };25 bar.style().template(&template).expect("no template error")26}2728/// Show the benchmark heading above the progress bar, clearing both on completion.29pub fn set_benchmark_header(bar: &ProgressBar, header: String) {30 bar.set_prefix(header);31 bar.set_style(style_with_template(bar, DEFAULT_TEMPLATE));32}33Merge pull request #581 from devonhollowood/elapsedDavid Peterbefore adoption5d501b6
lines 34–35why, one step back34/// Show the time elapsed since the initial benchmark command started.35pub fn start_initial_measurement(bar: &ProgressBar, started: Instant) {Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 36–41why, one step back36 let style = style_with_template(bar, INITIAL_TEMPLATE).with_key(37 "initial_elapsed",38 move |_: &ProgressState, w: &mut dyn fmt::Write| {39 write!(w, "{}", FormattedDuration(started.elapsed())).unwrap();40 },41 );Merge pull request #581 from devonhollowood/elapsedDavid Peterbefore adoption5d501b6
lines 42–47why, one step back42 bar.set_style(style);43}4445/// Stop showing the initial timer and display the next phase's message.46pub fn finish_initial_measurement(bar: &ProgressBar, message: String) {47 bar.set_message(message);Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 48why, one step back48 bar.set_style(style_with_template(bar, DEFAULT_TEMPLATE));Merge pull request #581 from devonhollowood/elapsedDavid Peterbefore adoption5d501b6
lines 49–50why, one step back49}50Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 51–56why, one step back51/// Return a pre-configured progress bar52pub fn get_progress_bar(length: u64, msg: &str, option: OutputStyleOption) -> ProgressBar {53 let progressbar_style = match option {54 OutputStyleOption::Basic | OutputStyleOption::Color => ProgressStyle::default_bar(),55 _ => ProgressStyle::default_spinner()56 .tick_chars(TICK_SETTINGS.0)Merge pull request #581 from devonhollowood/elapsedDavid Peterbefore adoption5d501b6
line 57why, one step back57 .template(DEFAULT_TEMPLATE)Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 58–70why, one step back58 .expect("no template error"),59 };6061 let progress_bar = match option {62 OutputStyleOption::Basic | OutputStyleOption::Color => ProgressBar::hidden(),63 _ => ProgressBar::new(length),64 };65 progress_bar.set_style(progressbar_style);66 progress_bar.enable_steady_tick(Duration::from_millis(TICK_SETTINGS.1));67 progress_bar.set_message(msg.to_owned());6869 progress_bar70}