Skip to content

Commit e6b9f31

Browse files
authored
Print --timestamps with script recipes (#3050)
1 parent 5732ee0 commit e6b9f31

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ impl Config {
675675
}
676676
}
677677

678+
pub(crate) fn timestamp(&self) -> Option<String> {
679+
self.timestamp.then(|| {
680+
chrono::Local::now()
681+
.format(&self.timestamp_format)
682+
.to_string()
683+
})
684+
}
685+
678686
pub(crate) fn from_matches(matches: &ArgMatches) -> ConfigResult<Self> {
679687
let mut overrides = BTreeMap::new();
680688
if let Some(mut values) = matches.get_many::<String>(arg::SET) {

src/recipe.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,8 @@ impl<'src, D> Recipe<'src, D> {
298298
}
299299
.stderr();
300300

301-
if config.timestamp {
302-
eprint!(
303-
"[{}] ",
304-
color.paint(
305-
&chrono::Local::now()
306-
.format(&config.timestamp_format)
307-
.to_string()
308-
),
309-
);
301+
if let Some(timestamp) = config.timestamp() {
302+
eprint!("[{}] ", color.paint(&timestamp));
310303
}
311304

312305
eprintln!("{}", color.paint(command));
@@ -387,6 +380,17 @@ impl<'src, D> Recipe<'src, D> {
387380
) -> RunResult<'src, ()> {
388381
let config = &context.config;
389382

383+
if let Some(timestamp) = config.timestamp() {
384+
let color = if config.highlight {
385+
config.color.command(config.command_color)
386+
} else {
387+
config.color
388+
}
389+
.stderr();
390+
391+
eprintln!("[{}] {}", color.paint(&timestamp), self.name);
392+
}
393+
390394
let mut evaluated_lines = Vec::new();
391395
for line in &self.body {
392396
evaluated_lines.push(evaluator.evaluate_line(line, false)?);

tests/timestamps.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22

33
#[test]
4-
fn print_timestamps() {
4+
fn linewise() {
55
Test::new()
66
.justfile(
77
"
@@ -16,7 +16,23 @@ fn print_timestamps() {
1616
}
1717

1818
#[test]
19-
fn print_timestamps_with_format_string() {
19+
fn script() {
20+
Test::new()
21+
.justfile(
22+
"
23+
recipe:
24+
#!/bin/sh
25+
echo 'one'
26+
",
27+
)
28+
.arg("--timestamp")
29+
.stderr_regex(concat!(r"\[\d\d:\d\d:\d\d\] recipe", "\n"))
30+
.stdout("one\n")
31+
.run();
32+
}
33+
34+
#[test]
35+
fn format_string() {
2036
Test::new()
2137
.justfile(
2238
"

0 commit comments

Comments
 (0)