Skip to content

feat: add custom unit support #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const TICK_FORMAT: &str = "\\|/-";
pub enum Units {
Default,
Bytes,
Custom {
unit: String,
speed_formatter: Option<fn(f64) -> String>,
},
}

pub struct ProgressBar<T: Write> {
Expand Down Expand Up @@ -338,9 +342,19 @@ impl<T: Write> ProgressBar<T> {
}
// speed box
if self.show_speed {
match self.units {
match &self.units {
Units::Default => parts.push(format!("{:.*}/s", 2, speed)),
Units::Bytes => parts.push(format!("{}/s", kb_fmt!(speed))),
Units::Custom {
unit,
speed_formatter,
} => {
if let Some(speed_formatter) = speed_formatter {
parts.push(format!("{} {}/s", speed_formatter(speed), unit))
} else {
parts.push(format!("{:.*} {}/s", 2, speed, unit))
}
}
};
}
// time left box
Expand All @@ -361,9 +375,10 @@ impl<T: Write> ProgressBar<T> {
if self.show_counter {
let (c, t) = (self.current as f64, self.total as f64);
prefix = prefix
+ &match self.units {
+ &match &self.units {
Units::Default => format!("{} / {} ", c, t),
Units::Bytes => format!("{} / {} ", kb_fmt!(c), kb_fmt!(t)),
Units::Custom { .. } => format!("{} / {} ", c, t),
};
}
// tick box
Expand Down Expand Up @@ -536,6 +551,44 @@ mod test {
assert_eq!(kb_fmt!(tb), "1.00 TB");
}

#[test]
fn custom_units() {
let mut out = Vec::new();
let mut pb = ProgressBar::on(&mut out, 10);
pb.units = Units::Custom {
unit: "unit".into(),
speed_formatter: None,
};
pb.show_percent = false;
pb.set_width(Some(80));
pb.draw();
assert_eq!(
std::str::from_utf8(&out).unwrap(),
"\r0 / 10 [----------------------------------------------------------] 0.00 unit/s ",
);
}

#[test]
fn custom_units_with_formatter() {
fn format_speed(speed: f64) -> String {
format!("{}", speed.round() as i64)
}

let mut out = Vec::new();
let mut pb = ProgressBar::on(&mut out, 10);
pb.units = Units::Custom {
unit: "unit".into(),
speed_formatter: Some(format_speed),
};
pb.show_percent = false;
pb.set_width(Some(80));
pb.draw();
assert_eq!(
std::str::from_utf8(&out).unwrap(),
"\r0 / 10 [-------------------------------------------------------------] 0 unit/s ",
);
}

#[test]
fn disable_speed_percent() {
let mut out = Vec::new();
Expand Down