Add more fields to atuin search -f formatting (#824)

- Add `{exit}` which returns the exit code
- Add `{relativetime}` which gives a relative time, e.g. "5h"
This commit is contained in:
Tom Cammann 2023-03-28 22:06:24 +01:00 committed by GitHub
parent 3514ff2401
commit 0f139044b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -20,6 +20,7 @@ use atuin_client::{
use atuin_client::sync; use atuin_client::sync;
use log::debug; use log::debug;
use super::search::format_duration;
use super::search::format_duration_into; use super::search::format_duration_into;
#[derive(Subcommand)] #[derive(Subcommand)]
@ -115,11 +116,17 @@ impl FormatKey for FmtHistory<'_> {
match key { match key {
"command" => f.write_str(self.0.command.trim())?, "command" => f.write_str(self.0.command.trim())?,
"directory" => f.write_str(self.0.cwd.trim())?, "directory" => f.write_str(self.0.cwd.trim())?,
"exit" => f.write_str(&self.0.exit.to_string())?,
"duration" => { "duration" => {
let dur = Duration::from_nanos(std::cmp::max(self.0.duration, 0) as u64); let dur = Duration::from_nanos(std::cmp::max(self.0.duration, 0) as u64);
format_duration_into(dur, f)?; format_duration_into(dur, f)?;
} }
"time" => self.0.timestamp.format("%Y-%m-%d %H:%M:%S").fmt(f)?, "time" => self.0.timestamp.format("%Y-%m-%d %H:%M:%S").fmt(f)?,
"relativetime" => {
let since = chrono::Utc::now() - self.0.timestamp;
let time = format_duration(since.to_std().unwrap_or_default());
f.write_str(&time)?;
}
"host" => f.write_str( "host" => f.write_str(
self.0 self.0
.hostname .hostname

View file

@ -79,7 +79,8 @@ pub struct Cmd {
#[arg(long)] #[arg(long)]
delete: bool, delete: bool,
/// Available variables: {command}, {directory}, {duration}, {user}, {host} and {time}. /// Available variables: {command}, {directory}, {duration}, {user}, {host}, {time}, {exit} and
/// {relativetime}.
/// Example: --format "{time} - [{duration}] - {directory}$\t{command}" /// Example: --format "{time} - [{duration}] - {directory}$\t{command}"
#[arg(long, short)] #[arg(long, short)]
format: Option<String>, format: Option<String>,