Skip to content

Commit

Permalink
Use inline format arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-laplante authored and djc committed Feb 6, 2023
1 parent 98ab0fe commit 1cbb573
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 63 deletions.
2 changes: 1 addition & 1 deletion examples/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
let pb = ProgressBar::new(100);
for i in 0..100 {
thread::sleep(Duration::from_millis(25));
pb.println(format!("[+] finished #{}", i));
pb.println(format!("[+] finished #{i}"));
pb.inc(1);
}
pb.finish_with_message("done");
Expand Down
2 changes: 1 addition & 1 deletion examples/yarnish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn main() {
let pkg = PACKAGES.choose(&mut rng).unwrap();
for _ in 0..count {
let cmd = COMMANDS.choose(&mut rng).unwrap();
pb.set_message(format!("{}: {}", pkg, cmd));
pb.set_message(format!("{pkg}: {cmd}"));
pb.inc(1);
thread::sleep(Duration::from_millis(rng.gen_range(25..200)));
}
Expand Down
71 changes: 34 additions & 37 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ impl fmt::Display for FormattedDuration {
t /= 24;
if t > 0 {
let days = t;
write!(f, "{}d {:02}:{:02}:{:02}", days, hours, minutes, seconds)
write!(f, "{days}d {hours:02}:{minutes:02}:{seconds:02}")
} else {
write!(f, "{:02}:{:02}:{:02}", hours, minutes, seconds)
write!(f, "{hours:02}:{minutes:02}:{seconds:02}")
}
}
}
Expand Down Expand Up @@ -90,9 +90,9 @@ impl fmt::Display for HumanDuration {
}

match (f.alternate(), t) {
(true, _) => write!(f, "{}{}", t, alt),
(false, 1) => write!(f, "{} {}", t, name),
(false, _) => write!(f, "{} {}s", t, name),
(true, _) => write!(f, "{t}{alt}"),
(false, 1) => write!(f, "{t} {name}"),
(false, _) => write!(f, "{t} {name}s"),
}
}
}
Expand All @@ -109,26 +109,26 @@ const UNITS: &[(Duration, &str, &str)] = &[
impl fmt::Display for HumanBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match NumberPrefix::binary(self.0 as f64) {
NumberPrefix::Standalone(number) => write!(f, "{:.0}B", number),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{:.2} {}B", number, prefix),
NumberPrefix::Standalone(number) => write!(f, "{number:.0}B"),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{number:.2} {prefix}B"),
}
}
}

impl fmt::Display for DecimalBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match NumberPrefix::decimal(self.0 as f64) {
NumberPrefix::Standalone(number) => write!(f, "{:.0}B", number),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{:.2} {}B", number, prefix),
NumberPrefix::Standalone(number) => write!(f, "{number:.0}B"),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{number:.2} {prefix}B"),
}
}
}

impl fmt::Display for BinaryBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match NumberPrefix::binary(self.0 as f64) {
NumberPrefix::Standalone(number) => write!(f, "{:.0}B", number),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{:.2} {}B", number, prefix),
NumberPrefix::Standalone(number) => write!(f, "{number:.0}B"),
NumberPrefix::Prefixed(prefix, number) => write!(f, "{number:.2} {prefix}B"),
}
}
}
Expand Down Expand Up @@ -185,10 +185,7 @@ mod tests {
#[test]
fn human_duration_alternate() {
for (unit, _, alt) in UNITS {
assert_eq!(
format!("2{}", alt),
format!("{:#}", HumanDuration(2 * *unit))
);
assert_eq!(format!("2{alt}"), format!("{:#}", HumanDuration(2 * *unit)));
}
}

Expand Down Expand Up @@ -226,31 +223,31 @@ mod tests {
// this one is actually done at 1.5 unit - half of the next smaller unit - epsilon
// and should display the next smaller unit
let d = HumanDuration(MINUTE + MINUTE / 2 - SECOND / 2 - MILLI);
assert_eq!("89 seconds", format!("{}", d));
assert_eq!("89 seconds", format!("{d}"));
let d = HumanDuration(HOUR + HOUR / 2 - MINUTE / 2 - MILLI);
assert_eq!("89 minutes", format!("{}", d));
assert_eq!("89 minutes", format!("{d}"));
let d = HumanDuration(DAY + DAY / 2 - HOUR / 2 - MILLI);
assert_eq!("35 hours", format!("{}", d));
assert_eq!("35 hours", format!("{d}"));
let d = HumanDuration(WEEK + WEEK / 2 - DAY / 2 - MILLI);
assert_eq!("10 days", format!("{}", d));
assert_eq!("10 days", format!("{d}"));
let d = HumanDuration(YEAR + YEAR / 2 - WEEK / 2 - MILLI);
assert_eq!("78 weeks", format!("{}", d));
assert_eq!("78 weeks", format!("{d}"));
}

#[test]
fn human_duration_one_and_a_half_unit() {
// this one is actually done at 1.5 unit - half of the next smaller unit
// and should still display "2 units"
let d = HumanDuration(MINUTE + MINUTE / 2 - SECOND / 2);
assert_eq!("2 minutes", format!("{}", d));
assert_eq!("2 minutes", format!("{d}"));
let d = HumanDuration(HOUR + HOUR / 2 - MINUTE / 2);
assert_eq!("2 hours", format!("{}", d));
assert_eq!("2 hours", format!("{d}"));
let d = HumanDuration(DAY + DAY / 2 - HOUR / 2);
assert_eq!("2 days", format!("{}", d));
assert_eq!("2 days", format!("{d}"));
let d = HumanDuration(WEEK + WEEK / 2 - DAY / 2);
assert_eq!("2 weeks", format!("{}", d));
assert_eq!("2 weeks", format!("{d}"));
let d = HumanDuration(YEAR + YEAR / 2 - WEEK / 2);
assert_eq!("2 years", format!("{}", d));
assert_eq!("2 years", format!("{d}"));
}

#[test]
Expand All @@ -266,33 +263,33 @@ mod tests {
#[test]
fn human_duration_less_than_two_and_a_half_units() {
let d = HumanDuration(2 * SECOND + SECOND / 2 - MILLI);
assert_eq!("2 seconds", format!("{}", d));
assert_eq!("2 seconds", format!("{d}"));
let d = HumanDuration(2 * MINUTE + MINUTE / 2 - MILLI);
assert_eq!("2 minutes", format!("{}", d));
assert_eq!("2 minutes", format!("{d}"));
let d = HumanDuration(2 * HOUR + HOUR / 2 - MILLI);
assert_eq!("2 hours", format!("{}", d));
assert_eq!("2 hours", format!("{d}"));
let d = HumanDuration(2 * DAY + DAY / 2 - MILLI);
assert_eq!("2 days", format!("{}", d));
assert_eq!("2 days", format!("{d}"));
let d = HumanDuration(2 * WEEK + WEEK / 2 - MILLI);
assert_eq!("2 weeks", format!("{}", d));
assert_eq!("2 weeks", format!("{d}"));
let d = HumanDuration(2 * YEAR + YEAR / 2 - MILLI);
assert_eq!("2 years", format!("{}", d));
assert_eq!("2 years", format!("{d}"));
}

#[test]
fn human_duration_two_and_a_half_units() {
let d = HumanDuration(2 * SECOND + SECOND / 2);
assert_eq!("3 seconds", format!("{}", d));
assert_eq!("3 seconds", format!("{d}"));
let d = HumanDuration(2 * MINUTE + MINUTE / 2);
assert_eq!("3 minutes", format!("{}", d));
assert_eq!("3 minutes", format!("{d}"));
let d = HumanDuration(2 * HOUR + HOUR / 2);
assert_eq!("3 hours", format!("{}", d));
assert_eq!("3 hours", format!("{d}"));
let d = HumanDuration(2 * DAY + DAY / 2);
assert_eq!("3 days", format!("{}", d));
assert_eq!("3 days", format!("{d}"));
let d = HumanDuration(2 * WEEK + WEEK / 2);
assert_eq!("3 weeks", format!("{}", d));
assert_eq!("3 weeks", format!("{d}"));
let d = HumanDuration(2 * YEAR + YEAR / 2);
assert_eq!("3 years", format!("{}", d));
assert_eq!("3 years", format!("{d}"));
}

#[test]
Expand Down
24 changes: 4 additions & 20 deletions src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,28 @@ impl TermLike for InMemoryTerm {
fn move_cursor_up(&self, n: usize) -> std::io::Result<()> {
match n {
0 => Ok(()),
_ => self
.state
.lock()
.unwrap()
.write_str(&format!("\x1b[{}A", n)),
_ => self.state.lock().unwrap().write_str(&format!("\x1b[{n}A")),
}
}

fn move_cursor_down(&self, n: usize) -> std::io::Result<()> {
match n {
0 => Ok(()),
_ => self
.state
.lock()
.unwrap()
.write_str(&format!("\x1b[{}B", n)),
_ => self.state.lock().unwrap().write_str(&format!("\x1b[{n}B")),
}
}

fn move_cursor_right(&self, n: usize) -> std::io::Result<()> {
match n {
0 => Ok(()),
_ => self
.state
.lock()
.unwrap()
.write_str(&format!("\x1b[{}C", n)),
_ => self.state.lock().unwrap().write_str(&format!("\x1b[{n}C")),
}
}

fn move_cursor_left(&self, n: usize) -> std::io::Result<()> {
match n {
0 => Ok(()),
_ => self
.state
.lock()
.unwrap()
.write_str(&format!("\x1b[{}D", n)),
_ => self.state.lock().unwrap().write_str(&format!("\x1b[{n}D")),
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ mod tests {
use super::*;
use crate::ProgressBar;

// https://github.com/rust-lang/rust-clippy/issues/10281
#[allow(clippy::uninlined_format_args)]
#[test]
fn test_time_per_step() {
let test_rate = |items_per_second| {
Expand Down
6 changes: 3 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ impl ProgressStyle {
}
"msg" => buf.push_str(state.message.expanded()),
"prefix" => buf.push_str(state.prefix.expanded()),
"pos" => buf.write_fmt(format_args!("{}", pos)).unwrap(),
"pos" => buf.write_fmt(format_args!("{pos}")).unwrap(),
"human_pos" => {
buf.write_fmt(format_args!("{}", HumanCount(pos))).unwrap();
}
"len" => buf.write_fmt(format_args!("{}", len)).unwrap(),
"len" => buf.write_fmt(format_args!("{len}")).unwrap(),
"human_len" => {
buf.write_fmt(format_args!("{}", HumanCount(len))).unwrap();
}
Expand Down Expand Up @@ -344,7 +344,7 @@ impl ProgressStyle {
Some(s) => cur
.write_fmt(format_args!("{}", s.apply_to(padded)))
.unwrap(),
None => cur.write_fmt(format_args!("{}", padded)).unwrap(),
None => cur.write_fmt(format_args!("{padded}")).unwrap(),
}
}
None => match style {
Expand Down
2 changes: 1 addition & 1 deletion tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ fn ticker_drop() {
let new_spinner = mp.add(
ProgressBar::new_spinner()
.with_finish(ProgressFinish::AndLeave)
.with_message(format!("doing stuff {}", i)),
.with_message(format!("doing stuff {i}")),
);
new_spinner.enable_steady_tick(Duration::from_millis(100));
spinner.replace(new_spinner);
Expand Down

0 comments on commit 1cbb573

Please sign in to comment.