From fb10da6857a79b53ba5220db12e987e699f40af3 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Mon, 16 Oct 2023 20:42:11 +0800 Subject: [PATCH] feat(completions): generate shell completion scripts --- config/completions/_yazi | 39 ++++++++++++++++++++++++++++++ config/completions/_yazi.ps1 | 37 +++++++++++++++++++++++++++++ config/completions/yazi.bash | 46 ++++++++++++++++++++++++++++++++++++ config/completions/yazi.elv | 31 ++++++++++++++++++++++++ config/completions/yazi.fish | 5 ++++ config/completions/yazi.nu | 14 +++++++++++ config/completions/yazi.ts | 45 +++++++++++++++++++++++++++++++++++ 7 files changed, 217 insertions(+) create mode 100644 config/completions/_yazi create mode 100644 config/completions/_yazi.ps1 create mode 100644 config/completions/yazi.bash create mode 100644 config/completions/yazi.elv create mode 100644 config/completions/yazi.fish create mode 100644 config/completions/yazi.nu create mode 100644 config/completions/yazi.ts diff --git a/config/completions/_yazi b/config/completions/_yazi new file mode 100644 index 00000000..52ccf2e0 --- /dev/null +++ b/config/completions/_yazi @@ -0,0 +1,39 @@ +#compdef yazi + +autoload -U is-at-least + +_yazi() { + 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[@]}" \ +'--cwd-file=[Write the cwd on exit to this file]:CWD_FILE:_files' \ +'--chooser-file=[Write the selected files on open emitted by the chooser mode]:CHOOSER_FILE:_files' \ +'--clear-cache[Clear the cache directory]' \ +'-h[Print help]' \ +'--help[Print help]' \ +'-V[Print version]' \ +'--version[Print version]' \ +'::cwd -- Set the current working directory:_files' \ +&& ret=0 +} + +(( $+functions[_yazi_commands] )) || +_yazi_commands() { + local commands; commands=() + _describe -t commands 'yazi commands' commands "$@" +} + +if [ "$funcstack[1]" = "_yazi" ]; then + _yazi "$@" +else + compdef _yazi yazi +fi diff --git a/config/completions/_yazi.ps1 b/config/completions/_yazi.ps1 new file mode 100644 index 00000000..22d198b2 --- /dev/null +++ b/config/completions/_yazi.ps1 @@ -0,0 +1,37 @@ + +using namespace System.Management.Automation +using namespace System.Management.Automation.Language + +Register-ArgumentCompleter -Native -CommandName 'yazi' -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $commandElements = $commandAst.CommandElements + $command = @( + 'yazi' + 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('-') -or + $element.Value -eq $wordToComplete) { + break + } + $element.Value + }) -join ';' + + $completions = @(switch ($command) { + 'yazi' { + [CompletionResult]::new('--cwd-file', 'cwd-file', [CompletionResultType]::ParameterName, 'Write the cwd on exit to this file') + [CompletionResult]::new('--chooser-file', 'chooser-file', [CompletionResultType]::ParameterName, 'Write the selected files on open emitted by the chooser mode') + [CompletionResult]::new('--clear-cache', 'clear-cache', [CompletionResultType]::ParameterName, 'Clear the cache directory') + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') + [CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') + break + } + }) + + $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | + Sort-Object -Property ListItemText +} diff --git a/config/completions/yazi.bash b/config/completions/yazi.bash new file mode 100644 index 00000000..f137efef --- /dev/null +++ b/config/completions/yazi.bash @@ -0,0 +1,46 @@ +_yazi() { + local i cur prev opts cmd + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + cmd="" + opts="" + + for i in ${COMP_WORDS[@]} + do + case "${cmd},${i}" in + ",$1") + cmd="yazi" + ;; + *) + ;; + esac + done + + case "${cmd}" in + yazi) + opts="-h -V --cwd-file --chooser-file --clear-cache --help --version [CWD]" + if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + --cwd-file) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --chooser-file) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; + esac +} + +complete -F _yazi -o nosort -o bashdefault -o default yazi diff --git a/config/completions/yazi.elv b/config/completions/yazi.elv new file mode 100644 index 00000000..637939c2 --- /dev/null +++ b/config/completions/yazi.elv @@ -0,0 +1,31 @@ + +use builtin; +use str; + +set edit:completion:arg-completer[yazi] = {|@words| + fn spaces {|n| + builtin:repeat $n ' ' | str:join '' + } + fn cand {|text desc| + edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc + } + var command = 'yazi' + for word $words[1..-1] { + if (str:has-prefix $word '-') { + break + } + set command = $command';'$word + } + var completions = [ + &'yazi'= { + cand --cwd-file 'Write the cwd on exit to this file' + cand --chooser-file 'Write the selected files on open emitted by the chooser mode' + cand --clear-cache 'Clear the cache directory' + cand -h 'Print help' + cand --help 'Print help' + cand -V 'Print version' + cand --version 'Print version' + } + ] + $completions[$command] +} diff --git a/config/completions/yazi.fish b/config/completions/yazi.fish new file mode 100644 index 00000000..badbc534 --- /dev/null +++ b/config/completions/yazi.fish @@ -0,0 +1,5 @@ +complete -c yazi -l cwd-file -d 'Write the cwd on exit to this file' -r -F +complete -c yazi -l chooser-file -d 'Write the selected files on open emitted by the chooser mode' -r -F +complete -c yazi -l clear-cache -d 'Clear the cache directory' +complete -c yazi -s h -l help -d 'Print help' +complete -c yazi -s V -l version -d 'Print version' diff --git a/config/completions/yazi.nu b/config/completions/yazi.nu new file mode 100644 index 00000000..4a0649a4 --- /dev/null +++ b/config/completions/yazi.nu @@ -0,0 +1,14 @@ +module completions { + + export extern yazi [ + cwd?: string # Set the current working directory + --cwd-file: string # Write the cwd on exit to this file + --chooser-file: string # Write the selected files on open emitted by the chooser mode + --clear-cache # Clear the cache directory + --help(-h) # Print help + --version(-V) # Print version + ] + +} + +export use completions * diff --git a/config/completions/yazi.ts b/config/completions/yazi.ts new file mode 100644 index 00000000..f2915edd --- /dev/null +++ b/config/completions/yazi.ts @@ -0,0 +1,45 @@ +const completion: Fig.Spec = { + name: "yazi", + description: "", + options: [ + { + name: "--cwd-file", + description: "Write the cwd on exit to this file", + isRepeatable: true, + args: { + name: "cwd_file", + isOptional: true, + template: "filepaths", + }, + }, + { + name: "--chooser-file", + description: "Write the selected files on open emitted by the chooser mode", + isRepeatable: true, + args: { + name: "chooser_file", + isOptional: true, + template: "filepaths", + }, + }, + { + name: "--clear-cache", + description: "Clear the cache directory", + }, + { + name: ["-h", "--help"], + description: "Print help", + }, + { + name: ["-V", "--version"], + description: "Print version", + }, + ], + args: { + name: "cwd", + isOptional: true, + template: "filepaths", + }, +}; + +export default completion;