Nushell: add CLI completions

This commit is contained in:
Ajeet D'Souza 2025-05-27 02:12:30 +05:30
parent ee8bbe57d3
commit b318a2a190
11 changed files with 375 additions and 408 deletions

View file

@ -14,10 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support for Tcsh.
- Added `--score` flag to `zoxide add`.
- POSIX: add doctor to diagnose common issues.
- Nushell: add CLI completions.
### Changed
- Bash: zoxide will now rewrite the prompt when using Space-Tab completions.
- Bash: zoxide will now automatically `cd` when selecting Space-Tab completions.
### Fixed
@ -27,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Nushell: stop ignoring symlinks when `cd`-ing into a directory.
- Fzf: updated minimum supported version to v0.51.0.
- PowerShell: avoid setting `$error` when defining `__zoxide_hooked`.
- PowerShell: handle special characters in file paths when `cd`-ing into them.
- Database corruption issue when the filesystem is 100% full.
## [0.9.7] - 2025-02-10

618
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -17,37 +17,41 @@ maintenance = { status = "actively-developed" }
[dependencies]
anyhow = "1.0.32"
askama = { version = "0.14.0", default-features = false, features = [
"derive",
"std",
] }
bincode = "1.3.1"
clap = { version = "4.3.0", features = ["derive"] }
color-print = "0.3.4"
dirs = "5.0.0"
dirs = "6.0.0"
dunce = "1.0.1"
fastrand = "2.0.0"
glob = "0.3.0"
ouroboros = "0.18.3"
rinja = { version = "0.3.2", default-features = false }
serde = { version = "1.0.116", features = ["derive"] }
[target.'cfg(unix)'.dependencies]
nix = { version = "0.29.0", default-features = false, features = [
nix = { version = "0.30.1", default-features = false, features = [
"fs",
"user",
] }
[target.'cfg(windows)'.dependencies]
which = "6.0.0"
which = "7.0.3"
[build-dependencies]
clap = { version = "4.3.0", features = ["derive"] }
clap_complete = "4.3.0"
clap_complete_fig = "4.3.0"
clap_complete = "4.5.50"
clap_complete_fig = "4.5.2"
clap_complete_nushell = "4.5.5"
color-print = "0.3.4"
[dev-dependencies]
assert_cmd = "2.0.0"
rstest = { version = "0.23.0", default-features = false }
rstest = { version = "0.25.0", default-features = false }
rstest_reuse = "0.7.0"
tempfile = "3.1.0"
tempfile = "=3.15.0"
[features]
default = []

View file

@ -6,6 +6,7 @@ use std::{env, io};
use clap::CommandFactory as _;
use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_complete_fig::Fig;
use clap_complete_nushell::Nushell;
use cmd::Cmd;
fn main() -> io::Result<()> {
@ -27,6 +28,7 @@ fn generate_completions() -> io::Result<()> {
clap_complete::generate_to(Elvish, cmd, BIN_NAME, OUT_DIR)?;
clap_complete::generate_to(Fig, cmd, BIN_NAME, OUT_DIR)?;
clap_complete::generate_to(Fish, cmd, BIN_NAME, OUT_DIR)?;
clap_complete::generate_to(Nushell, cmd, BIN_NAME, OUT_DIR)?;
clap_complete::generate_to(PowerShell, cmd, BIN_NAME, OUT_DIR)?;
clap_complete::generate_to(Zsh, cmd, BIN_NAME, OUT_DIR)?;

View file

@ -30,8 +30,8 @@ _zoxide() {
case $line[1] in
(add)
_arguments "${_arguments_options[@]}" : \
'-s+[The rank to increment the entry if it exists or initialize it with if it doesn'\''t]:SCORE: ' \
'--score=[The rank to increment the entry if it exists or initialize it with if it doesn'\''t]:SCORE: ' \
'-s+[The rank to increment the entry if it exists or initialize it with if it doesn'\''t]:SCORE:_default' \
'--score=[The rank to increment the entry if it exists or initialize it with if it doesn'\''t]:SCORE:_default' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
@ -61,7 +61,7 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':path:' \
':path:_default' \
&& ret=0
;;
(delete)
@ -70,7 +70,7 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':path:' \
':path:_default' \
&& ret=0
;;
(increment)
@ -79,7 +79,7 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':path:' \
':path:_default' \
&& ret=0
;;
(reload)
@ -107,7 +107,7 @@ _arguments "${_arguments_options[@]}" : \
;;
(init)
_arguments "${_arguments_options[@]}" : \
'--cmd=[Changes the prefix of the \`z\` and \`zi\` commands]:CMD: ' \
'--cmd=[Changes the prefix of the \`z\` and \`zi\` commands]:CMD:_default' \
'--hook=[Changes how often zoxide increments a directory'\''s score]:HOOK:(none prompt pwd)' \
'--no-cmd[Prevents zoxide from defining the \`z\` and \`zi\` commands]' \
'-h[Print help]' \
@ -132,7 +132,7 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*::keywords:' \
'*::keywords:_default' \
&& ret=0
;;
(remove)

View file

@ -1,12 +1,16 @@
_zoxide() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
cur="$2"
else
cur="${COMP_WORDS[COMP_CWORD]}"
fi
prev="$3"
cmd=""
opts=""
for i in ${COMP_WORDS[@]}
for i in "${COMP_WORDS[@]:0:COMP_CWORD}"
do
case "${cmd},${i}" in
",$1")

View file

@ -49,12 +49,15 @@ complete -c zoxide -n "__fish_zoxide_using_subcommand edit; and __fish_seen_subc
complete -c zoxide -n "__fish_zoxide_using_subcommand edit; and __fish_seen_subcommand_from increment" -s V -l version -d 'Print version'
complete -c zoxide -n "__fish_zoxide_using_subcommand edit; and __fish_seen_subcommand_from reload" -s h -l help -d 'Print help'
complete -c zoxide -n "__fish_zoxide_using_subcommand edit; and __fish_seen_subcommand_from reload" -s V -l version -d 'Print version'
complete -c zoxide -n "__fish_zoxide_using_subcommand import" -l from -d 'Application to import from' -r -f -a "{autojump\t'',z\t''}"
complete -c zoxide -n "__fish_zoxide_using_subcommand import" -l from -d 'Application to import from' -r -f -a "autojump\t''
z\t''"
complete -c zoxide -n "__fish_zoxide_using_subcommand import" -l merge -d 'Merge into existing database'
complete -c zoxide -n "__fish_zoxide_using_subcommand import" -s h -l help -d 'Print help'
complete -c zoxide -n "__fish_zoxide_using_subcommand import" -s V -l version -d 'Print version'
complete -c zoxide -n "__fish_zoxide_using_subcommand init" -l cmd -d 'Changes the prefix of the `z` and `zi` commands' -r
complete -c zoxide -n "__fish_zoxide_using_subcommand init" -l hook -d 'Changes how often zoxide increments a directory\'s score' -r -f -a "{none\t'',prompt\t'',pwd\t''}"
complete -c zoxide -n "__fish_zoxide_using_subcommand init" -l hook -d 'Changes how often zoxide increments a directory\'s score' -r -f -a "none\t''
prompt\t''
pwd\t''"
complete -c zoxide -n "__fish_zoxide_using_subcommand init" -l no-cmd -d 'Prevents zoxide from defining the `z` and `zi` commands'
complete -c zoxide -n "__fish_zoxide_using_subcommand init" -s h -l help -d 'Print help'
complete -c zoxide -n "__fish_zoxide_using_subcommand init" -s V -l version -d 'Print version'

98
contrib/completions/zoxide.nu generated Normal file
View file

@ -0,0 +1,98 @@
module completions {
# A smarter cd command for your terminal
export extern zoxide [
--help(-h) # Print help
--version(-V) # Print version
]
# Add a new directory or increment its rank
export extern "zoxide add" [
...paths: path
--score(-s): string # The rank to increment the entry if it exists or initialize it with if it doesn't
--help(-h) # Print help
--version(-V) # Print version
]
# Edit the database
export extern "zoxide edit" [
--help(-h) # Print help
--version(-V) # Print version
]
export extern "zoxide edit decrement" [
path: string
--help(-h) # Print help
--version(-V) # Print version
]
export extern "zoxide edit delete" [
path: string
--help(-h) # Print help
--version(-V) # Print version
]
export extern "zoxide edit increment" [
path: string
--help(-h) # Print help
--version(-V) # Print version
]
export extern "zoxide edit reload" [
--help(-h) # Print help
--version(-V) # Print version
]
def "nu-complete zoxide import from" [] {
[ "autojump" "z" ]
}
# Import entries from another application
export extern "zoxide import" [
path: path
--from: string@"nu-complete zoxide import from" # Application to import from
--merge # Merge into existing database
--help(-h) # Print help
--version(-V) # Print version
]
def "nu-complete zoxide init shell" [] {
[ "bash" "elvish" "fish" "nushell" "posix" "powershell" "tcsh" "xonsh" "zsh" ]
}
def "nu-complete zoxide init hook" [] {
[ "none" "prompt" "pwd" ]
}
# Generate shell configuration
export extern "zoxide init" [
shell: string@"nu-complete zoxide init shell"
--no-cmd # Prevents zoxide from defining the `z` and `zi` commands
--cmd: string # Changes the prefix of the `z` and `zi` commands
--hook: string@"nu-complete zoxide init hook" # Changes how often zoxide increments a directory's score
--help(-h) # Print help
--version(-V) # Print version
]
# Search for a directory in the database
export extern "zoxide query" [
...keywords: string
--all(-a) # Show unavailable directories
--interactive(-i) # Use interactive selection
--list(-l) # List all matching directories
--score(-s) # Print score with results
--exclude: path # Exclude the current directory
--help(-h) # Print help
--version(-V) # Print version
]
# Remove a directory from the database
export extern "zoxide remove" [
...paths: path
--help(-h) # Print help
--version(-V) # Print version
]
}
export use completions *

View file

@ -1,7 +1,7 @@
use std::io::{self, Write};
use anyhow::{Context, Result};
use rinja::Template;
use askama::Template;
use crate::cmd::{Init, InitShell, Run};
use crate::config;

View file

@ -10,7 +10,7 @@ pub struct Opts<'a> {
macro_rules! make_template {
($name:ident, $path:expr) => {
#[derive(::std::fmt::Debug, ::rinja::Template)]
#[derive(::std::fmt::Debug, ::askama::Template)]
#[template(path = $path)]
pub struct $name<'a>(pub &'a self::Opts<'a>);
@ -36,8 +36,8 @@ make_template!(Zsh, "zsh.txt");
#[cfg(feature = "nix-dev")]
#[cfg(test)]
mod tests {
use askama::Template;
use assert_cmd::Command;
use rinja::Template;
use rstest::rstest;
use rstest_reuse::{apply, template};

View file

@ -169,13 +169,12 @@ pub fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
#[cfg(unix)]
if let Ok(metadata) = path.metadata() {
use std::os::unix::fs::MetadataExt;
use std::os::unix::io::AsRawFd;
use nix::unistd::{self, Gid, Uid};
let uid = Uid::from_raw(metadata.uid());
let gid = Gid::from_raw(metadata.gid());
_ = unistd::fchown(tmp_file.as_raw_fd(), Some(uid), Some(gid));
_ = unistd::fchown(&tmp_file, Some(uid), Some(gid));
}
// Close and rename the tmpfile.