Skip to content

Commit

Permalink
Show durations in ms.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaander committed May 8, 2024
1 parent 0a428e6 commit 11c19fe
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/cli/submit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Args;
use console::style;
use indicatif::{HumanCount, HumanDuration};
use indicatif::HumanCount;
use log::{debug, info, trace, warn};
use signal_hook::consts::{SIGINT, SIGTERM};
use signal_hook::flag;
Expand All @@ -14,6 +14,7 @@ use std::time::Instant;
use wildmatch::WildMatch;

use crate::cli::GlobalOptions;
use row::format::HumanDuration;
use row::project::Project;
use row::workflow::{Action, ResourceCost};
use row::MultiProgressContainer;
Expand Down Expand Up @@ -226,7 +227,7 @@ pub fn submit<W: Write>(
.italic()
.to_string();
}
message += &format!(" ({}).", style(HumanDuration(instant.elapsed())).dim());
message += &format!(" ({:#}).", style(HumanDuration(instant.elapsed())).dim());
println!("{message}");

let result = scheduler.submit(
Expand Down
23 changes: 23 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fmt;
use std::time::Duration;

/// Extend `indicatif::HumanDuration` with milliseconds
#[derive(Debug)]
pub struct HumanDuration(pub Duration);

impl fmt::Display for HumanDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.as_secs_f64() > 1.0 {
indicatif::HumanDuration(self.0).fmt(f)
} else {
#[allow(clippy::cast_sign_loss)]
let t = (self.0.as_secs_f64() / 1e-3).round() as usize;

match (f.alternate(), t) {
(true, _) => write!(f, "{t}ms"),
(false, 1) => write!(f, "{t} millisecond"),
(false, _) => write!(f, "{t} milliseconds"),
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
pub(crate) mod builtin;
pub mod cluster;
mod expr;
pub mod format;
pub mod launcher;
pub mod progress_styles;
pub mod project;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![warn(clippy::pedantic)]

use clap::Parser;
use indicatif::{HumanDuration, MultiProgress, ProgressDrawTarget};
use indicatif::{MultiProgress, ProgressDrawTarget};
use indicatif_log_bridge::LogWrapper;
use log::{error, info};
use std::error::Error;
Expand All @@ -13,6 +13,7 @@ mod cli;
mod ui;

use cli::{ColorMode, Commands, Options, ShowCommands};
use row::format::HumanDuration;
use row::MultiProgressContainer;
use ui::MultiProgressWriter;

Expand Down
13 changes: 12 additions & 1 deletion src/progress_styles.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use indicatif::ProgressStyle;
use indicatif::{ProgressState, ProgressStyle};
use std::fmt::Write;

use crate::format::HumanDuration;

pub(crate) const STEADY_TICK: u64 = 110;

/// Format progress duration in milliseconds
fn elapsed(state: &ProgressState, w: &mut dyn Write) {
let _ = write!(w, "{:#}", HumanDuration(state.elapsed()));
}

/// Create a named spinner.
///
/// # Panics
Expand All @@ -10,6 +18,7 @@ pub(crate) const STEADY_TICK: u64 = 110;
pub fn uncounted_spinner() -> ProgressStyle {
ProgressStyle::with_template("{spinner:.green.bold} {msg:.bold}... ({elapsed:.dim})")
.expect("Valid template")
.with_key("elapsed", elapsed)
.tick_strings(&["◐", "◓", "◑", "◒", "⊙"])
}

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

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

0 comments on commit 11c19fe

Please sign in to comment.