From d37634fa7664317b612f75f86b07281b1d9fc142 Mon Sep 17 00:00:00 2001 From: lengyijun Date: Fri, 11 Apr 2025 07:33:24 +0800 Subject: [PATCH] Add option: zoxide query -t : print last_accessed Closes #1037 Co-authored-by: Azalea Colburn <62953415+azaleacolburn@users.noreply.github.com> --- contrib/completions/_zoxide | 2 ++ contrib/completions/_zoxide.ps1 | 2 ++ contrib/completions/zoxide.bash | 2 +- contrib/completions/zoxide.elv | 2 ++ contrib/completions/zoxide.fish | 1 + contrib/completions/zoxide.nu | 1 + contrib/completions/zoxide.ts | 4 ++++ src/cmd/cmd.rs | 4 ++++ src/cmd/query.rs | 8 +++++++- src/db/dir.rs | 12 +++++++++++- 10 files changed, 35 insertions(+), 3 deletions(-) diff --git a/contrib/completions/_zoxide b/contrib/completions/_zoxide index 97e654f..7405042 100644 --- a/contrib/completions/_zoxide +++ b/contrib/completions/_zoxide @@ -129,6 +129,8 @@ _arguments "${_arguments_options[@]}" : \ '(-i --interactive)--list[List all matching directories]' \ '-s[Print score with results]' \ '--score[Print score with results]' \ +'-t[Print \`last_accessed\` with results, no effect without \`--list\` option]' \ +'--time[Print \`last_accessed\` with results, no effect without \`--list\` option]' \ '-h[Print help]' \ '--help[Print help]' \ '-V[Print version]' \ diff --git a/contrib/completions/_zoxide.ps1 b/contrib/completions/_zoxide.ps1 index bb47d3a..138c051 100644 --- a/contrib/completions/_zoxide.ps1 +++ b/contrib/completions/_zoxide.ps1 @@ -111,6 +111,8 @@ Register-ArgumentCompleter -Native -CommandName 'zoxide' -ScriptBlock { [CompletionResult]::new('--list', '--list', [CompletionResultType]::ParameterName, 'List all matching directories') [CompletionResult]::new('-s', '-s', [CompletionResultType]::ParameterName, 'Print score with results') [CompletionResult]::new('--score', '--score', [CompletionResultType]::ParameterName, 'Print score with results') + [CompletionResult]::new('-t', '-t', [CompletionResultType]::ParameterName, 'Print `last_accessed` with results, no effect without `--list` option') + [CompletionResult]::new('--time', '--time', [CompletionResultType]::ParameterName, 'Print `last_accessed` with results, no effect without `--list` option') [CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help') [CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help') [CompletionResult]::new('-V', '-V ', [CompletionResultType]::ParameterName, 'Print version') diff --git a/contrib/completions/zoxide.bash b/contrib/completions/zoxide.bash index 82b174e..c94bd65 100644 --- a/contrib/completions/zoxide.bash +++ b/contrib/completions/zoxide.bash @@ -199,7 +199,7 @@ _zoxide() { return 0 ;; zoxide__query) - opts="-a -i -l -s -h -V --all --interactive --list --score --exclude --base-dir --help --version [KEYWORDS]..." + opts="-a -i -l -s -t -h -V --all --interactive --list --score --time --exclude --base-dir --help --version [KEYWORDS]..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/contrib/completions/zoxide.elv b/contrib/completions/zoxide.elv index 93c57af..2c2ff2d 100644 --- a/contrib/completions/zoxide.elv +++ b/contrib/completions/zoxide.elv @@ -99,6 +99,8 @@ set edit:completion:arg-completer[zoxide] = {|@words| cand --list 'List all matching directories' cand -s 'Print score with results' cand --score 'Print score with results' + cand -t 'Print `last_accessed` with results, no effect without `--list` option' + cand --time 'Print `last_accessed` with results, no effect without `--list` option' cand -h 'Print help' cand --help 'Print help' cand -V 'Print version' diff --git a/contrib/completions/zoxide.fish b/contrib/completions/zoxide.fish index 3a0bfe7..1b7e3f3 100644 --- a/contrib/completions/zoxide.fish +++ b/contrib/completions/zoxide.fish @@ -67,6 +67,7 @@ complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s a -l all -d 'Sho complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s i -l interactive -d 'Use interactive selection' complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s l -l list -d 'List all matching directories' complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s s -l score -d 'Print score with results' +complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s t -l time -d 'Print `last_accessed` with results, no effect without `--list` option' complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s h -l help -d 'Print help' complete -c zoxide -n "__fish_zoxide_using_subcommand query" -s V -l version -d 'Print version' complete -c zoxide -n "__fish_zoxide_using_subcommand remove" -s h -l help -d 'Print help' diff --git a/contrib/completions/zoxide.nu b/contrib/completions/zoxide.nu index 642908e..10f09b3 100644 --- a/contrib/completions/zoxide.nu +++ b/contrib/completions/zoxide.nu @@ -81,6 +81,7 @@ module completions { --interactive(-i) # Use interactive selection --list(-l) # List all matching directories --score(-s) # Print score with results + --time(-t) # Print `last_accessed` with results, no effect without `--list` option --exclude: path # Exclude the current directory --base-dir: path # Only search within this directory --help(-h) # Print help diff --git a/contrib/completions/zoxide.ts b/contrib/completions/zoxide.ts index 1e0d404..8c60fbe 100644 --- a/contrib/completions/zoxide.ts +++ b/contrib/completions/zoxide.ts @@ -248,6 +248,10 @@ const completion: Fig.Spec = { name: ["-s", "--score"], description: "Print score with results", }, + { + name: ["-t", "--time"], + description: "Print `last_accessed` with results, no effect without `--list` option", + }, { name: ["-h", "--help"], description: "Print help", diff --git a/src/cmd/cmd.rs b/src/cmd/cmd.rs index 7359786..bf7ed01 100644 --- a/src/cmd/cmd.rs +++ b/src/cmd/cmd.rs @@ -183,6 +183,10 @@ pub struct Query { #[clap(long, short)] pub score: bool, + /// Print `last_accessed` with results, no effect without `--list` option + #[clap(long, short)] + pub time: bool, + /// Exclude the current directory #[clap(long, value_hint = ValueHint::DirPath, value_name = "path")] pub exclude: Option, diff --git a/src/cmd/query.rs b/src/cmd/query.rs index 6539c2e..3e59e02 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -58,7 +58,13 @@ impl Query { if Some(dir.path.as_ref()) == self.exclude.as_deref() { continue; } - let dir = if self.score { dir.display().with_score(now) } else { dir.display() }; + let dir = match (self.score, self.time) { + (true, true) => dir.display().with_score(now).with_last_assessed(), + (true, false) => dir.display().with_score(now), + (false, true) => dir.display().with_last_assessed(), + (false, false) => dir.display(), + }; + writeln!(handle, "{dir}").pipe_exit("stdout")?; } Ok(()) diff --git a/src/db/dir.rs b/src/db/dir.rs index 5d6d62c..a6549de 100644 --- a/src/db/dir.rs +++ b/src/db/dir.rs @@ -37,11 +37,12 @@ pub struct DirDisplay<'a> { dir: &'a Dir<'a>, now: Option, separator: char, + show_last_accessed: bool, } impl<'a> DirDisplay<'a> { fn new(dir: &'a Dir) -> Self { - Self { dir, separator: ' ', now: None } + Self { dir, separator: ' ', now: None, show_last_accessed: false } } pub fn with_score(mut self, now: Epoch) -> Self { @@ -49,6 +50,11 @@ impl<'a> DirDisplay<'a> { self } + pub fn with_last_assessed(mut self) -> Self { + self.show_last_accessed = true; + self + } + pub fn with_separator(mut self, separator: char) -> Self { self.separator = separator; self @@ -61,6 +67,10 @@ impl Display for DirDisplay<'_> { let score = self.dir.score(now).clamp(0.0, 9999.0); write!(f, "{score:>6.1}{}", self.separator)?; } + if self.show_last_accessed { + let last_accessed = self.dir.last_accessed; + write!(f, "{last_accessed:>6.1}{}", self.separator)?; + } write!(f, "{}", self.dir.path) } }