Skip to content
Myr

Add native Zsh completion

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Replace the auto-generated Zsh completion with a full completion script. This script improves completion support for options and arguments, which is hard to obtain from an auto-generated completion. - Full completion support for external commands and options when using `--exec`/`--exec-batch`. - The completion for `--type` and `--color` adds descriptions instead of just values. - Complete files when using `--ignore-file`. - Stop completing files for the pattern argument. - Improve completion for path arguments. Only directories are completed. - Single-use options are not offered again. - Updated exclusions lists for mutual-exclusive options. - Add support for hidden options (as defined in `app.rs`). They are not offered for completion, but if present are considered (and their value may be completed). Full support for exclusion lists depends of options groups, which was added in Zsh 5.4. Older Zsh versions, as commented in the completion script, will just offer most options. The format of the completion script was taken from ripgrep completion script. Command completion for `--exec`/`--exec-batch` was taken from Zsh's `_find` completion.

authored by Sebastián Mancilla · committed by David Peter · parent a38da63 · Browse files · 3 files+267 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+267 added−3 removed

  • other:+267 added−3 removed

3 files+267 added−3 removed

modified:ci/before_deploy.bash+2 added-2 removed
added:contrib/completion/_fd+265 added
1#compdef fd23##4# zsh completion function for fd5#6# Based on ripgrep completion function.7# Originally based on code from the zsh-users project — see copyright notice8# below.910autoload -U is-at-least1112_fd() {13 local curcontext="$curcontext" no='!' ret=114 local -a context line state state_descr _arguments_options fd_types fd_args15 local -A opt_args1617 if is-at-least 5.2; then18 _arguments_options=( -s -S )19 else20 _arguments_options=( -s )21 fi2223 fd_types=(24 {f,file}'\:"regular files"'25 {d,directory}'\:"directories"'26 {l,symlink}'\:"symbolic links"'27 {e,empty}'\:"empty files or directories"'28 {x,executable}'\:"executable (files)"'29 {s,socket}'\:"sockets"'30 {p,pipe}'\:"named pipes (FIFOs)"'31 )3233 # Do not complete rare options unless either the current prefix34 # matches one of those options or the user has the `complete-all`35 # style set. Note that this prefix check has to be updated manually to account36 # for all of the potential negation options listed below!37 if38 # (--[bpsu]* => match all options marked with '$no')39 [[ $PREFIX$SUFFIX == --[bopsu]* ]] ||40 zstyle -t ":complete:$curcontext:*" complete-all41 then42 no=43 fi4445 # We make heavy use of argument groups here to prevent the option specs from46 # growing unwieldy. These aren't supported in zsh <5.4, though, so we'll strip47 # them out below if necessary. This makes the exclusions inaccurate on those48 # older versions, but oh well — it's not that big a deal49 fd_args=(50 + '(hidden)' # hidden files51 {-H,--hidden}'[search hidden files/directories]'5253 + '(no-ignore-full)' # all ignore files54 '(no-ignore-partial)'{-I,--no-ignore}"[don't respect .(git|fd)ignore and global ignore files]"55 $no'(no-ignore-partial)*'{-u,--unrestricted}'[alias for --no-ignore, when repeated also alias for --hidden]'5657 + no-ignore-partial # some ignore files58 "(no-ignore-full --no-ignore-vcs)--no-ignore-vcs[don't respect .gitignore files]"59 "!(no-ignore-full --no-global-ignore-file)--no-global-ignore-file[don't respect the global ignore file]"6061 + '(case)' # case-sensitivity62 {-s,--case-sensitive}'[perform a case-sensitive search]'63 {-i,--ignore-case}'[perform a case-insensitive search]'6465 + '(regex-pattern)' # regex-based search pattern66 '(no-regex-pattern)--regex[perform a regex-based search (default)]'6768 + '(no-regex-pattern)' # non-regex-based search pattern69 {-g,--glob}'[perform a glob-based search]'70 {-F,--fixed-strings}'[treat pattern as literal string instead of a regex]'7172 + '(match-full)' # match against full path73 {-p,--full-path}'[match the pattern against the full path instead of the basename]'7475 + '(follow)' # follow symlinks76 {-L,--follow}'[follow symbolic links to directories]'7778 + '(abs-path)' # show absolute paths79 '(long-listing)'{-a,--absolute-path}'[show absolute paths instead of relative paths]'8081 + '(null-sep)' # use null separator for output82 '(long-listing)'{-0,--print0}'[separate search results by the null character]'8384 + '(long-listing)' # long-listing output85 '(abs-path null-sep max-results exec-cmds)'{-l,--list-details}'[use a long listing format with file metadata]'8687 + '(max-results)' # max number of results88 '(long-listing exec-cmds)--max-results=[limit number of search results to given count and quit]:count'89 '(long-listing exec-cmds)-1[limit to a single search result and quit]'9091 + '(fs-errors)' # file-system errors92 $no'--show-errors[enable the display of filesystem errors]'9394 + '(fs-traversal)' # file-system traversal95 $no"--one-file-system[don't descend into directories on other file systems]"96 '!--mount'97 '!--xdev'9899 + dir-depth # directory depth100 '(--exact-depth -d --max-depth)'{-d+,--max-depth=}'[set max directory depth to descend when searching]:depth'101 '!(--exact-depth -d --max-depth)--maxdepth:depth'102 '(--exact-depth --min-depth)--min-depth=[set directory depth to descend before start searching]:depth'103 '(--exact-depth -d --max-depth --maxdepth --min-depth)--exact-depth=[only search at the exact given directory depth]:depth'104105 + filter-misc # filter search106 '*'{-t+,--type=}"[filter search by type]:type:(($fd_types))"107 '*'{-e+,--extension=}'[filter search by file extension]:extension'108 '*'{-E+,--exclude=}'[exclude files/directories that match the given glob pattern]:glob pattern'109 '*'{-S+,--size=}'[limit search by file size]:size limit:->size'110 '(-o --owner)'{-o+,--owner=}'[filter by owning user and/or group]:owner and/or group:->owner'111112 + ignore-file # extra ignore files113 '*--ignore-file=[add a custom, low-precedence ignore-file with .gitignore format]: :_files'114115 + '(filter-mtime-newer)' # filter by files modified after than116 '--changed-within=[limit search to files/directories modified within the given date/duration]:date or duration'117 '!--change-newer-than=:date/duration'118 '!--newer=:date/duration'119120 + '(filter-mtime-older)' # filter by files modified before than121 '--changed-before=[limit search to files/directories modified before the given date/duration]:date or duration'122 '!--change-older-than=:date/duration'123 '!--older=:date/duration'124125 + '(color)' # colorize output126 {-c+,--color=}'[declare when to colorize search results]:when to colorize:((127 auto\:"show colors if the output goes to an interactive console (default)"128 never\:"do not use colorized output"129 always\:"always use colorized output"130 ))'131132 + '(threads)'133 {-j+,--threads=}'[set the number of threads for searching and executing]:number of threads'134135 + '(exec-cmds)' # execute command136 '(long-listing max-results)'{-x+,--exec=}'[execute command for each search result]:command: _command_names -e:*\;::program arguments: _normal'137 '(long-listing max-results)'{-X+,--exec-batch=}'[execute command for all search results at once]:command: _command_names -e:*\;::program arguments: _normal'138139 + other140 '!(--max-buffer-time)--max-buffer-time=[set amount of time to buffer before showing output]:time (ms)'141142 + '(about)' # about flags143 '(: * -)'{-h,--help}'[display help message]'144 '(: * -)'{-v,--version}'[display version information]'145146 + path-sep # set path separator for output147 $no'(--path-separator)--path-separator=[set the path separator to use when printing file paths]:path separator'148149 + search-path150 $no'(--base-directory)--base-directory=[change the current working directory to the given path]:directory:_files -/'151 $no'(*)*--search-path=[set search path (instead of positional <path> arguments)]:directory:_files -/'152153 + args # positional arguments154 '1: :_guard "^-*" pattern'155 '(--search-path)*:directory:_files -/'156 )157158 # Strip out argument groups where unsupported (see above)159 is-at-least 5.4 ||160 fd_args=( ${(@)args:#(#i)(+|[a-z0-9][a-z0-9_-]#|\([a-z0-9][a-z0-9_-]#\))} )161162 _arguments $_arguments_options : $fd_args && ret=0163164 case ${state} in165 owner)166 compset -P '(\\|)\!'167 if compset -P '*:'; then168 _groups && ret=0169 else170 if171 compset -S ':*' ||172 # Do not add the colon suffix when completing "!user<TAB>173 # (with a starting double-quote) otherwise pressing tab again174 # after the inserted colon "!user:<TAB> will complete history modifiers175 [[ $IPREFIX == (\\|\!)* && ($QIPREFIX == \"* && -z $QISUFFIX) ]]176 then177 _users && ret=0178 else179 local q180 # Since quotes are needed when using the negation prefix !,181 # automatically remove the colon suffix also when closing the quote182 if [[ $QIPREFIX == [\'\"]* ]]; then183 q=${QIPREFIX:0:1}184 fi185 _users -r ": \t\n\-$q" -S : && ret=0186 fi187 fi188 ;;189190 size)191 if compset -P '[-+][0-9]##'; then192 local -a suff=(193 'B:bytes'194 'K:kilobytes (10^3 = 1000 bytes)'195 'M:megabytes (10^6 = 1000^2 bytes)'196 'G:gigabytes (10^9 = 1000^3 bytes)'197 'T:terabytes (10^12 = 1000^4 bytes)'198 'Ki:kibibytes ( 2^10 = 1024 bytes)'199 'Mi:mebibytes ( 2^20 = 1024^2 bytes)'200 'Gi:gigibytes ( 2^30 = 1024^3 bytes)'201 'Ti:tebibytes ( 2^40 = 1024^4 bytes)'202 )203 _describe -t units 'size limit units' suff -V 'units'204 elif compset -P '[-+]'; then205 _message -e 'size limit number (full format: <+-><number><unit>)'206 else207 _values 'size limit prefix (full format: <prefix><number><unit>)' \208 '\+[file size must be greater or equal to]'\209 '-[file size must be less than or equal to]' && ret=0210 fi211 ;;212 esac213214 return ret215}216217_fd "$@"218219# ------------------------------------------------------------------------------220# Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users221# All rights reserved.222#223# Redistribution and use in source and binary forms, with or without224# modification, are permitted provided that the following conditions are met:225# * Redistributions of source code must retain the above copyright226# notice, this list of conditions and the following disclaimer.227# * Redistributions in binary form must reproduce the above copyright228# notice, this list of conditions and the following disclaimer in the229# documentation and/or other materials provided with the distribution.230# * Neither the name of the zsh-users nor the231# names of its contributors may be used to endorse or promote products232# derived from this software without specific prior written permission.233#234# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND235# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED236# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE237# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY238# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES239# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;240# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND241# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT242# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS243# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.244# ------------------------------------------------------------------------------245# Description246# -----------247#248# Completion script for fd249#250# ------------------------------------------------------------------------------251# Authors252# -------253#254# * smancill (https://github.com/smancill)255#256# ------------------------------------------------------------------------------257258# Local Variables:259# mode: shell-script260# coding: utf-8-unix261# indent-tabs-mode: nil262# sh-indentation: 2263# sh-basic-offset: 2264# End:265# vim: ft=zsh sw=2 ts=2 et
modified:build.rs-1 removed
24 unmodified lines
2526 let mut app = build_app();27 app.gen_completions("fd", Shell::Bash, &outdir);28 app.gen_completions("fd", Shell::Fish, &outdir);29 app.gen_completions("fd", Shell::Zsh, &outdir);29 app.gen_completions("fd", Shell::PowerShell, &outdir);30}