Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: cli completions #200

Merged
merged 1 commit into from
May 3, 2021
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ assert_cmd = "1.0.1"
once_cell = "1.4.1"
seq-macro = "0.2.1"

[build-dependencies]
clap = "3.0.0-beta.2"
clap_generate = "3.0.0-beta.2"

[features]
default = []
# Adds tests for code generated by `zoxide init`.
Expand Down
18 changes: 18 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
use clap::IntoApp;
use clap_generate::{generate_to, generators::*};

use std::env;
use std::process::Command;

include!("src/app.rs");

fn completions() {
let mut app = Cli::into_app();
let bin_name = env!("CARGO_PKG_NAME");
let out_dir = "contrib/completions";

generate_to::<Bash, _, _>(&mut app, bin_name, out_dir);
generate_to::<Elvish, _, _>(&mut app, bin_name, out_dir);
generate_to::<Fish, _, _>(&mut app, bin_name, out_dir);
generate_to::<PowerShell, _, _>(&mut app, bin_name, out_dir);
generate_to::<Zsh, _, _>(&mut app, bin_name, out_dir);
}

fn git_version() -> Option<String> {
// Packages releases of zoxide almost always use the source tarball
// provided by GitHub, which does not include the `.git` folder. Since this
Expand All @@ -21,4 +38,5 @@ fn crate_version() -> String {
fn main() {
let version = git_version().unwrap_or_else(crate_version);
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version);
completions();
}
132 changes: 132 additions & 0 deletions contrib/completions/_zoxide
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#compdef zoxide

autoload -U is-at-least

_zoxide() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1

if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
":: :_zoxide_commands" \
"*::: :->zoxide" \
&& ret=0
case $state in
(zoxide)
words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:zoxide-command-$line[1]:"
case $line[1] in
(add)
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
':path:' \
&& ret=0
;;
(import)
_arguments "${_arguments_options[@]}" \
'--from=[Application to import from]: :(autojump z)' \
'--merge[Merge into existing database]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
':path:' \
&& ret=0
;;
(init)
_arguments "${_arguments_options[@]}" \
'--cmd=[Renames the '\''z'\'' command and corresponding aliases]' \
'--hook=[Chooses event upon which an entry is added to the database]: :(none prompt pwd)' \
'--no-aliases[Prevents zoxide from defining any commands]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
':shell:(bash elvish fish nushell posix powershell xonsh zsh)' \
&& ret=0
;;
(query)
_arguments "${_arguments_options[@]}" \
'--exclude=[Exclude a path from results]' \
'(-l --list)-i[Use interactive selection]' \
'(-l --list)--interactive[Use interactive selection]' \
'(-i --interactive)-l[List all matching directories]' \
'(-i --interactive)--list[List all matching directories]' \
'-s[Print score with results]' \
'--score[Print score with results]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'*::keywords:' \
&& ret=0
;;
(remove)
_arguments "${_arguments_options[@]}" \
'()*-i+[]' \
'()*--interactive=[]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'::path:' \
&& ret=0
;;
esac
;;
esac
}

(( $+functions[_zoxide_commands] )) ||
_zoxide_commands() {
local commands; commands=(
"add:Add a new directory or increment its rank" \
"import:Import entries from another application" \
"init:Generate shell configuration" \
"query:Search for a directory in the database" \
"remove:Remove a directory from the database" \
)
_describe -t commands 'zoxide commands' commands "$@"
}
(( $+functions[_zoxide__add_commands] )) ||
_zoxide__add_commands() {
local commands; commands=(

)
_describe -t commands 'zoxide add commands' commands "$@"
}
(( $+functions[_zoxide__import_commands] )) ||
_zoxide__import_commands() {
local commands; commands=(

)
_describe -t commands 'zoxide import commands' commands "$@"
}
(( $+functions[_zoxide__init_commands] )) ||
_zoxide__init_commands() {
local commands; commands=(

)
_describe -t commands 'zoxide init commands' commands "$@"
}
(( $+functions[_zoxide__query_commands] )) ||
_zoxide__query_commands() {
local commands; commands=(

)
_describe -t commands 'zoxide query commands' commands "$@"
}
(( $+functions[_zoxide__remove_commands] )) ||
_zoxide__remove_commands() {
local commands; commands=(

)
_describe -t commands 'zoxide remove commands' commands "$@"
}

_zoxide "$@"
77 changes: 77 additions & 0 deletions contrib/completions/_zoxide.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'zoxide' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'zoxide'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-')) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'zoxide' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('add', 'add', [CompletionResultType]::ParameterValue, 'Add a new directory or increment its rank')
[CompletionResult]::new('import', 'import', [CompletionResultType]::ParameterValue, 'Import entries from another application')
[CompletionResult]::new('init', 'init', [CompletionResultType]::ParameterValue, 'Generate shell configuration')
[CompletionResult]::new('query', 'query', [CompletionResultType]::ParameterValue, 'Search for a directory in the database')
[CompletionResult]::new('remove', 'remove', [CompletionResultType]::ParameterValue, 'Remove a directory from the database')
break
}
'zoxide;add' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
break
}
'zoxide;import' {
[CompletionResult]::new('--from', 'from', [CompletionResultType]::ParameterName, 'Application to import from')
[CompletionResult]::new('--merge', 'merge', [CompletionResultType]::ParameterName, 'Merge into existing database')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
break
}
'zoxide;init' {
[CompletionResult]::new('--cmd', 'cmd', [CompletionResultType]::ParameterName, 'Renames the ''z'' command and corresponding aliases')
[CompletionResult]::new('--hook', 'hook', [CompletionResultType]::ParameterName, 'Chooses event upon which an entry is added to the database')
[CompletionResult]::new('--no-aliases', 'no-aliases', [CompletionResultType]::ParameterName, 'Prevents zoxide from defining any commands')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
break
}
'zoxide;query' {
[CompletionResult]::new('--exclude', 'exclude', [CompletionResultType]::ParameterName, 'Exclude a path from results')
[CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'Use interactive selection')
[CompletionResult]::new('--interactive', 'interactive', [CompletionResultType]::ParameterName, 'Use interactive selection')
[CompletionResult]::new('-l', 'l', [CompletionResultType]::ParameterName, 'List all matching directories')
[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('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
break
}
'zoxide;remove' {
[CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'i')
[CompletionResult]::new('--interactive', 'interactive', [CompletionResultType]::ParameterName, 'interactive')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
Loading