Skip to content

Commit 11c19fe

Browse files
committed
Show durations in ms.
1 parent 0a428e6 commit 11c19fe

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

src/cli/submit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::Args;
22
use console::style;
3-
use indicatif::{HumanCount, HumanDuration};
3+
use indicatif::HumanCount;
44
use log::{debug, info, trace, warn};
55
use signal_hook::consts::{SIGINT, SIGTERM};
66
use signal_hook::flag;
@@ -14,6 +14,7 @@ use std::time::Instant;
1414
use wildmatch::WildMatch;
1515

1616
use crate::cli::GlobalOptions;
17+
use row::format::HumanDuration;
1718
use row::project::Project;
1819
use row::workflow::{Action, ResourceCost};
1920
use row::MultiProgressContainer;
@@ -226,7 +227,7 @@ pub fn submit<W: Write>(
226227
.italic()
227228
.to_string();
228229
}
229-
message += &format!(" ({}).", style(HumanDuration(instant.elapsed())).dim());
230+
message += &format!(" ({:#}).", style(HumanDuration(instant.elapsed())).dim());
230231
println!("{message}");
231232

232233
let result = scheduler.submit(

src/format.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::fmt;
2+
use std::time::Duration;
3+
4+
/// Extend `indicatif::HumanDuration` with milliseconds
5+
#[derive(Debug)]
6+
pub struct HumanDuration(pub Duration);
7+
8+
impl fmt::Display for HumanDuration {
9+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10+
if self.0.as_secs_f64() > 1.0 {
11+
indicatif::HumanDuration(self.0).fmt(f)
12+
} else {
13+
#[allow(clippy::cast_sign_loss)]
14+
let t = (self.0.as_secs_f64() / 1e-3).round() as usize;
15+
16+
match (f.alternate(), t) {
17+
(true, _) => write!(f, "{t}ms"),
18+
(false, 1) => write!(f, "{t} millisecond"),
19+
(false, _) => write!(f, "{t} milliseconds"),
20+
}
21+
}
22+
}
23+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
pub(crate) mod builtin;
99
pub mod cluster;
1010
mod expr;
11+
pub mod format;
1112
pub mod launcher;
1213
pub mod progress_styles;
1314
pub mod project;

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::pedantic)]
22

33
use clap::Parser;
4-
use indicatif::{HumanDuration, MultiProgress, ProgressDrawTarget};
4+
use indicatif::{MultiProgress, ProgressDrawTarget};
55
use indicatif_log_bridge::LogWrapper;
66
use log::{error, info};
77
use std::error::Error;
@@ -13,6 +13,7 @@ mod cli;
1313
mod ui;
1414

1515
use cli::{ColorMode, Commands, Options, ShowCommands};
16+
use row::format::HumanDuration;
1617
use row::MultiProgressContainer;
1718
use ui::MultiProgressWriter;
1819

src/progress_styles.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
use indicatif::ProgressStyle;
1+
use indicatif::{ProgressState, ProgressStyle};
2+
use std::fmt::Write;
3+
4+
use crate::format::HumanDuration;
25

36
pub(crate) const STEADY_TICK: u64 = 110;
47

8+
/// Format progress duration in milliseconds
9+
fn elapsed(state: &ProgressState, w: &mut dyn Write) {
10+
let _ = write!(w, "{:#}", HumanDuration(state.elapsed()));
11+
}
12+
513
/// Create a named spinner.
614
///
715
/// # Panics
@@ -10,6 +18,7 @@ pub(crate) const STEADY_TICK: u64 = 110;
1018
pub fn uncounted_spinner() -> ProgressStyle {
1119
ProgressStyle::with_template("{spinner:.green.bold} {msg:.bold}... ({elapsed:.dim})")
1220
.expect("Valid template")
21+
.with_key("elapsed", elapsed)
1322
.tick_strings(&["◐", "◓", "◑", "◒", "⊙"])
1423
}
1524

@@ -21,6 +30,7 @@ pub fn uncounted_spinner() -> ProgressStyle {
2130
pub fn counted_spinner() -> ProgressStyle {
2231
ProgressStyle::with_template("{spinner:.green.bold} {msg:.bold}: {human_pos} ({elapsed:.dim})")
2332
.expect("Valid template")
33+
.with_key("elapsed", elapsed)
2434
.tick_strings(&["◐", "◓", "◑", "◒", "⊙"])
2535
}
2636

@@ -34,5 +44,6 @@ pub fn counted_bar() -> ProgressStyle {
3444
"|{bar:32.green}| {msg:.bold}: {human_pos}/{human_len} ({elapsed:.dim})",
3545
)
3646
.expect("Valid template")
47+
.with_key("elapsed", elapsed)
3748
.progress_chars("█▉▊▋▌▍▎▏ ")
3849
}

0 commit comments

Comments
 (0)