Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4299,6 +4299,20 @@ $ just foo
hello
```

Which may include a format string:

```just
[timestamp('%H:%M:%S%.3f')]
foo:
echo hello
```

```
$ just foo
[07:28:46.487] echo hello
hello
```

### Signal Handling

[Signals](https://en.wikipedia.org/wiki/Signal_(IPC)) are messages sent to
Expand Down Expand Up @@ -4758,6 +4772,7 @@ change their behavior.
| `[script(COMMAND)]`<sup>1.32.0</sup> | recipe | Execute recipe as a script interpreted by `COMMAND`. See [script recipes](#script-recipes) for more details. |
| `[script]`<sup>1.33.0</sup> | recipe | Execute recipe as script. See [script recipes](#script-recipes) for more details. |
| `[shell]`<sup>1.52.0</sup> | recipe | Execute recipe as a shell recipe, overriding `set default-script`. |
| `[timestamp(FORMAT)]`<sup>master</sup> | recipe | Print command timestamps with format `FORMAT`. `FORMAT` may be an expression. |
| `[timestamp]`<sup>master</sup> | recipe | Print command timestamps. |
| `[unix]`<sup>1.8.0</sup> | any<sup>1.56.0</sup> | Enable item on unixes. (Includes macOS). |
| `[windows]`<sup>1.8.0</sup> | any<sup>1.56.0</sup> | Enable item on Windows. |
Expand Down
15 changes: 9 additions & 6 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(crate) enum Attribute<'src> {
Private,
Script(Option<Interpreter<StringLiteral<'src>>>),
Shell,
Timestamp,
Timestamp(Option<Expression<'src>>),
Unix,
Windows,
WorkingDirectory(Expression<'src>),
Expand All @@ -75,7 +75,7 @@ impl AttributeKind {
fn accepts_expressions(self) -> bool {
matches!(
self,
Self::Confirm | Self::Doc | Self::Env | Self::WorkingDirectory
Self::Confirm | Self::Doc | Self::Env | Self::Timestamp | Self::WorkingDirectory
)
}

Expand Down Expand Up @@ -117,10 +117,9 @@ impl AttributeKind {
| Self::PositionalArguments
| Self::Private
| Self::Shell
| Self::Timestamp
| Self::Unix
| Self::Windows => 0..=0,
Self::Confirm | Self::Doc => 0..=1,
Self::Confirm | Self::Doc | Self::Timestamp => 0..=1,
Self::Continue | Self::Script => 0..=usize::MAX,
Self::Arg | Self::Extension | Self::Group | Self::WorkingDirectory => 1..=1,
Self::Env => 2..=2,
Expand Down Expand Up @@ -203,6 +202,9 @@ impl<'src> Attribute<'src> {
let (_, value) = arguments.next().unwrap();
Ok(Self::Env(key, value))
}
AttributeKind::Timestamp => Ok(Self::Timestamp(
arguments.into_iter().next().map(|(_, expr)| expr),
)),
AttributeKind::WorkingDirectory => Ok(Self::WorkingDirectory(
arguments.into_iter().next().map(|(_, expr)| expr).unwrap(),
)),
Expand Down Expand Up @@ -248,6 +250,7 @@ impl<'src> Attribute<'src> {
AttributeKind::Confirm
| AttributeKind::Doc
| AttributeKind::Env
| AttributeKind::Timestamp
| AttributeKind::WorkingDirectory => {
unreachable!()
}
Expand Down Expand Up @@ -276,7 +279,6 @@ impl<'src> Attribute<'src> {
})
}),
AttributeKind::Shell => Self::Shell,
AttributeKind::Timestamp => Self::Timestamp,
AttributeKind::Unix => Self::Unix,
AttributeKind::Windows => Self::Windows,
};
Expand Down Expand Up @@ -596,7 +598,7 @@ impl Display for Attribute<'_> {
| Self::Private
| Self::Script(None)
| Self::Shell
| Self::Timestamp
| Self::Timestamp(None)
| Self::Unix
| Self::Windows => {}
Self::Cache {
Expand All @@ -620,6 +622,7 @@ impl Display for Attribute<'_> {
}
Self::Confirm(Some(argument))
| Self::Doc(Some(argument))
| Self::Timestamp(Some(argument))
| Self::WorkingDirectory(argument) => {
write!(f, "({argument})")?;
}
Expand Down
45 changes: 36 additions & 9 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,32 @@ impl<'src> Recipe<'src> {
self.attributes.contains(AttributeKind::NoQuiet)
}

fn timestamp(&self, config: &Config) -> RunResult<'static, Option<String>> {
(config.timestamp || self.attributes.contains(AttributeKind::Timestamp))
.then(|| {
datetime_format(chrono::Local::now(), &config.timestamp_format)
.map_err(Error::DatetimeFormat)
})
.transpose()
fn timestamp_format(
&self,
config: &Config,
evaluator: &mut Evaluator<'src, '_>,
) -> RunResult<'src, Option<String>> {
if let Some(attribute) = self.attributes.get(AttributeKind::Timestamp) {
let Attribute::Timestamp(format) = attribute else {
unreachable!();
};
Ok(Some(
format
.as_ref()
.map(|expression| {
evaluator.evaluate_string(
expression,
StringContext::TimestampAttribute(self.attributes.name(attribute)),
)
})
.transpose()?
.unwrap_or_else(|| config.timestamp_format.clone()),
))
} else if config.timestamp {
Ok(Some(config.timestamp_format.clone()))
} else {
Ok(None)
}
}

pub(crate) fn run<'run>(
Expand Down Expand Up @@ -286,6 +305,8 @@ impl<'src> Recipe<'src> {

let working_directory = self.working_directory(context, &mut evaluator)?;

let timestamp_format = self.timestamp_format(config, &mut evaluator)?;

loop {
let Some(line) = lines.peek() else {
return Ok(());
Expand Down Expand Up @@ -330,7 +351,10 @@ impl<'src> Recipe<'src> {
let infallible = sigils.contains(&Sigil::Infallible);
let quiet = sigils.contains(&Sigil::Quiet);

let timestamp = self.timestamp(config)?;
let timestamp = timestamp_format
.as_deref()
.map(|format| datetime_format(chrono::Local::now(), format).map_err(Error::DatetimeFormat))
.transpose()?;

if config.dry_run
|| config.verbosity.loquacious()
Expand Down Expand Up @@ -448,7 +472,10 @@ impl<'src> Recipe<'src> {
) -> RunResult<'src> {
let config = &context.config;

if let Some(timestamp) = self.timestamp(config)? {
if let Some(format) = self.timestamp_format(config, &mut evaluator)? {
let timestamp =
datetime_format(chrono::Local::now(), &format).map_err(Error::DatetimeFormat)?;

let color = if config.highlight {
config.color.command(config.command_color)
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/string_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(crate) enum StringContext<'src> {
EnvKey(Name<'src>),
Function(Name<'src>),
Setting(Name<'src>),
TimestampAttribute(Name<'src>),
WorkingDirectoryAttribute(Name<'src>),
}

Expand All @@ -14,6 +15,7 @@ impl<'src> StringContext<'src> {
Self::EnvKey(name)
| Self::Function(name)
| Self::Setting(name)
| Self::TimestampAttribute(name)
| Self::WorkingDirectoryAttribute(name) => name.token,
}
}
Expand All @@ -25,6 +27,9 @@ impl Display for StringContext<'_> {
Self::EnvKey(_) => write!(f, "used as `env` attribute name"),
Self::Function(name) => write!(f, "passed to `{name}()`"),
Self::Setting(name) => write!(f, "assigned to `{name}` setting"),
Self::TimestampAttribute(_) => {
write!(f, "used as a `[timestamp]` attribute")
}
Self::WorkingDirectoryAttribute(_) => {
write!(f, "used as a `[working-directory]` attribute")
}
Expand Down
6 changes: 4 additions & 2 deletions src/unresolved_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ impl<'src> UnresolvedRecipe<'src> {
)?;
}
}
Attribute::Confirm(Some(expression)) | Attribute::WorkingDirectory(expression) => {
Attribute::Confirm(Some(expression))
| Attribute::Timestamp(Some(expression))
| Attribute::WorkingDirectory(expression) => {
variable_resolver.resolve_expression(
expression,
&parameters,
Expand Down Expand Up @@ -134,7 +136,7 @@ impl<'src> UnresolvedRecipe<'src> {
| Attribute::Private
| Attribute::Script(_)
| Attribute::Shell
| Attribute::Timestamp
| Attribute::Timestamp(None)
| Attribute::Unix
| Attribute::Windows => {}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/timestamps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ fn attribute() {
.success();
}

#[test]
fn attribute_format() {
Test::new()
.justfile(
"
[timestamp('%H:%M:%S%.3f')]
recipe:
echo foo
",
)
.stderr_regex(concat!(r"\[\d\d:\d\d:\d\d\.\d\d\d\] echo foo", "\n"))
.stdout("foo\n")
.success();
}

#[test]
fn attribute_format_expression() {
Test::new()
.justfile(
"
format := '%H:%M:%S' + '%.3f'

[timestamp(format)]
recipe:
echo foo
",
)
.stderr_regex(concat!(r"\[\d\d:\d\d:\d\d\.\d\d\d\] echo foo", "\n"))
.stdout("foo\n")
.success();
}

#[test]
fn attribute_script() {
Test::new()
Expand Down