Skip to content

Commit 4914779

Browse files
authored
Add --time to print recipe execution time (#3099)
1 parent 1b9ed7d commit 4914779

5 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/arguments.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ pub struct Arguments {
283283
value_name = "TEMPDIR"
284284
)]
285285
pub(crate) tempdir: Option<PathBuf>,
286+
#[arg(env = "JUST_TIME", help = "Print recipe execution time", long)]
287+
pub(crate) time: bool,
286288
#[arg(env = "JUST_TIMESTAMP", help = "Print recipe command timestamps", long)]
287289
pub(crate) timestamp: bool,
288290
#[arg(

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub(crate) struct Config {
3232
pub(crate) shell_command: bool,
3333
pub(crate) subcommand: Subcommand,
3434
pub(crate) tempdir: Option<PathBuf>,
35+
pub(crate) time: bool,
3536
pub(crate) timestamp: bool,
3637
pub(crate) timestamp_format: String,
3738
pub(crate) unsorted: bool,
@@ -281,6 +282,7 @@ impl Config {
281282
shell_command: arguments.shell_command,
282283
subcommand,
283284
tempdir: arguments.tempdir,
285+
time: arguments.time,
284286
timestamp: arguments.timestamp,
285287
timestamp_format: arguments.timestamp_format,
286288
unsorted: arguments.unsorted,

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ pub(crate) use {
147147
slice,
148148
str::{self, Chars, FromStr},
149149
sync::{Arc, LazyLock, Mutex, MutexGuard},
150-
thread, vec,
150+
thread,
151+
time::Instant,
152+
vec,
151153
},
152154
strum::{Display, EnumDiscriminants, EnumIter, EnumString, IntoStaticStr},
153155
tempfile::TempDir,

src/recipe.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,33 @@ impl<'src> Recipe<'src> {
221221

222222
let evaluator = Evaluator::new(context, BTreeMap::new(), is_dependency, scope);
223223

224-
if self.is_script() {
224+
let start = Instant::now();
225+
let result = if self.is_script() {
225226
self.run_script(context, scope, positional, evaluator)
226227
} else {
227228
self.run_linewise(context, scope, positional, evaluator)
229+
};
230+
let elapsed = start.elapsed();
231+
232+
if context.config.time {
233+
let color = if context.config.highlight {
234+
context.config.color.command(context.config.command_color)
235+
} else {
236+
context.config.color
237+
}
238+
.stderr();
239+
240+
let prefix = color.prefix();
241+
let suffix = color.suffix();
242+
let recipe_name = self.name.lexeme();
243+
244+
eprintln!(
245+
"{prefix}---> {recipe_name} completed in {:.3}s{suffix}",
246+
elapsed.as_secs_f64(),
247+
);
228248
}
249+
250+
result
229251
}
230252

231253
fn run_linewise<'run>(

tests/run.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ fn one_flag_only_allows_one_invocation() {
4141
.stderr("error: Expected 1 command-line recipe invocation but found 2.\n")
4242
.failure();
4343
}
44+
45+
#[test]
46+
fn time_reports_time_when_specified() {
47+
Test::new()
48+
.justfile(
49+
"
50+
foo:
51+
@echo FOO
52+
",
53+
)
54+
.arg("--time")
55+
.stdout("FOO\n")
56+
.stderr_regex(r"---> foo completed in \d+\.\d+s\n")
57+
.success();
58+
}

0 commit comments

Comments
 (0)