_fd 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #compdef fd
  2. ##
  3. # zsh completion function for fd
  4. #
  5. # Based on ripgrep completion function.
  6. # Originally based on code from the zsh-users project — see copyright notice
  7. # below.
  8. autoload -U is-at-least
  9. _fd() {
  10. local curcontext="$curcontext" no='!' ret=1
  11. local -a context line state state_descr _arguments_options fd_types fd_args
  12. local -A opt_args
  13. if is-at-least 5.2; then
  14. _arguments_options=( -s -S )
  15. else
  16. _arguments_options=( -s )
  17. fi
  18. fd_types=(
  19. {f,file}'\:"regular files"'
  20. {d,directory}'\:"directories"'
  21. {l,symlink}'\:"symbolic links"'
  22. {e,empty}'\:"empty files or directories"'
  23. {x,executable}'\:"executable (files)"'
  24. {s,socket}'\:"sockets"'
  25. {p,pipe}'\:"named pipes (FIFOs)"'
  26. )
  27. # Do not complete rare options unless either the current prefix
  28. # matches one of those options or the user has the `complete-all`
  29. # style set. Note that this prefix check has to be updated manually to account
  30. # for all of the potential negation options listed below!
  31. if
  32. # (--[bpsu]* => match all options marked with '$no')
  33. [[ $PREFIX$SUFFIX == --[bopsu]* ]] ||
  34. zstyle -t ":complete:$curcontext:*" complete-all
  35. then
  36. no=
  37. fi
  38. # We make heavy use of argument groups here to prevent the option specs from
  39. # growing unwieldy. These aren't supported in zsh <5.4, though, so we'll strip
  40. # them out below if necessary. This makes the exclusions inaccurate on those
  41. # older versions, but oh well — it's not that big a deal
  42. fd_args=(
  43. + '(hidden)' # hidden files
  44. {-H,--hidden}'[search hidden files/directories]'
  45. + '(no-ignore-full)' # all ignore files
  46. '(no-ignore-partial)'{-I,--no-ignore}"[don't respect .(git|fd)ignore and global ignore files]"
  47. $no'(no-ignore-partial)*'{-u,--unrestricted}'[alias for --no-ignore, when repeated also alias for --hidden]'
  48. + no-ignore-partial # some ignore files
  49. "(no-ignore-full --no-ignore-vcs)--no-ignore-vcs[don't respect .gitignore files]"
  50. "!(no-ignore-full --no-global-ignore-file)--no-global-ignore-file[don't respect the global ignore file]"
  51. $no'(no-ignore-full --no-ignore-parent)--no-ignore-parent[]'
  52. + '(case)' # case-sensitivity
  53. {-s,--case-sensitive}'[perform a case-sensitive search]'
  54. {-i,--ignore-case}'[perform a case-insensitive search]'
  55. + '(regex-pattern)' # regex-based search pattern
  56. '(no-regex-pattern)--regex[perform a regex-based search (default)]'
  57. + '(no-regex-pattern)' # non-regex-based search pattern
  58. {-g,--glob}'[perform a glob-based search]'
  59. {-F,--fixed-strings}'[treat pattern as literal string instead of a regex]'
  60. + '(match-full)' # match against full path
  61. {-p,--full-path}'[match the pattern against the full path instead of the basename]'
  62. + '(follow)' # follow symlinks
  63. {-L,--follow}'[follow symbolic links to directories]'
  64. + '(abs-path)' # show absolute paths
  65. '(long-listing)'{-a,--absolute-path}'[show absolute paths instead of relative paths]'
  66. + '(null-sep)' # use null separator for output
  67. '(long-listing)'{-0,--print0}'[separate search results by the null character]'
  68. + '(long-listing)' # long-listing output
  69. '(abs-path null-sep max-results exec-cmds)'{-l,--list-details}'[use a long listing format with file metadata]'
  70. + '(max-results)' # max number of results
  71. '(long-listing exec-cmds)--max-results=[limit number of search results to given count and quit]:count'
  72. '(long-listing exec-cmds)-1[limit to a single search result and quit]'
  73. + '(fs-errors)' # file-system errors
  74. $no'--show-errors[enable the display of filesystem errors]'
  75. + '(fs-traversal)' # file-system traversal
  76. $no"--one-file-system[don't descend into directories on other file systems]"
  77. '!--mount'
  78. '!--xdev'
  79. + dir-depth # directory depth
  80. '(--exact-depth -d --max-depth)'{-d+,--max-depth=}'[set max directory depth to descend when searching]:depth'
  81. '!(--exact-depth -d --max-depth)--maxdepth:depth'
  82. '(--exact-depth --min-depth)--min-depth=[set directory depth to descend before start searching]:depth'
  83. '(--exact-depth -d --max-depth --maxdepth --min-depth)--exact-depth=[only search at the exact given directory depth]:depth'
  84. + prune # pruning
  85. "--prune[don't traverse into matching directories]"
  86. + filter-misc # filter search
  87. '*'{-t+,--type=}"[filter search by type]:type:(($fd_types))"
  88. '*'{-e+,--extension=}'[filter search by file extension]:extension'
  89. '*'{-E+,--exclude=}'[exclude files/directories that match the given glob pattern]:glob pattern'
  90. '*'{-S+,--size=}'[limit search by file size]:size limit:->size'
  91. '(-o --owner)'{-o+,--owner=}'[filter by owning user and/or group]:owner and/or group:->owner'
  92. + ignore-file # extra ignore files
  93. '*--ignore-file=[add a custom, low-precedence ignore-file with .gitignore format]: :_files'
  94. + '(filter-mtime-newer)' # filter by files modified after than
  95. '--changed-within=[limit search to files/directories modified within the given date/duration]:date or duration'
  96. '!--change-newer-than=:date/duration'
  97. '!--newer=:date/duration'
  98. + '(filter-mtime-older)' # filter by files modified before than
  99. '--changed-before=[limit search to files/directories modified before the given date/duration]:date or duration'
  100. '!--change-older-than=:date/duration'
  101. '!--older=:date/duration'
  102. + '(color)' # colorize output
  103. {-c+,--color=}'[declare when to colorize search results]:when to colorize:((
  104. auto\:"show colors if the output goes to an interactive console (default)"
  105. never\:"do not use colorized output"
  106. always\:"always use colorized output"
  107. ))'
  108. + '(threads)'
  109. {-j+,--threads=}'[set the number of threads for searching and executing]:number of threads'
  110. + '(exec-cmds)' # execute command
  111. '(long-listing max-results)'{-x+,--exec=}'[execute command for each search result]:command: _command_names -e:*\;::program arguments: _normal'
  112. '(long-listing max-results)'{-X+,--exec-batch=}'[execute command for all search results at once]:command: _command_names -e:*\;::program arguments: _normal'
  113. '(long-listing max-results)--batch-size=[max number of args for each -X call]:size'
  114. + other
  115. '!(--max-buffer-time)--max-buffer-time=[set amount of time to buffer before showing output]:time (ms)'
  116. + '(about)' # about flags
  117. '(: * -)'{-h,--help}'[display help message]'
  118. '(: * -)'{-v,--version}'[display version information]'
  119. + path-sep # set path separator for output
  120. $no'(--path-separator)--path-separator=[set the path separator to use when printing file paths]:path separator'
  121. + search-path
  122. $no'(--base-directory)--base-directory=[change the current working directory to the given path]:directory:_files -/'
  123. $no'(*)*--search-path=[set search path (instead of positional <path> arguments)]:directory:_files -/'
  124. + strip-cwd-prefix
  125. $no'(strip-cwd-prefix exec-cmds)--strip-cwd-prefix[Strip ./ prefix when output is redirected]'
  126. + args # positional arguments
  127. '1: :_guard "^-*" pattern'
  128. '(--search-path)*:directory:_files -/'
  129. )
  130. # Strip out argument groups where unsupported (see above)
  131. is-at-least 5.4 ||
  132. fd_args=( ${(@)args:#(#i)(+|[a-z0-9][a-z0-9_-]#|\([a-z0-9][a-z0-9_-]#\))} )
  133. _arguments $_arguments_options : $fd_args && ret=0
  134. case ${state} in
  135. owner)
  136. compset -P '(\\|)\!'
  137. if compset -P '*:'; then
  138. _groups && ret=0
  139. else
  140. if
  141. compset -S ':*' ||
  142. # Do not add the colon suffix when completing "!user<TAB>
  143. # (with a starting double-quote) otherwise pressing tab again
  144. # after the inserted colon "!user:<TAB> will complete history modifiers
  145. [[ $IPREFIX == (\\|\!)* && ($QIPREFIX == \"* && -z $QISUFFIX) ]]
  146. then
  147. _users && ret=0
  148. else
  149. local q
  150. # Since quotes are needed when using the negation prefix !,
  151. # automatically remove the colon suffix also when closing the quote
  152. if [[ $QIPREFIX == [\'\"]* ]]; then
  153. q=${QIPREFIX:0:1}
  154. fi
  155. _users -r ": \t\n\-$q" -S : && ret=0
  156. fi
  157. fi
  158. ;;
  159. size)
  160. if compset -P '[-+][0-9]##'; then
  161. local -a suff=(
  162. 'B:bytes'
  163. 'K:kilobytes (10^3 = 1000 bytes)'
  164. 'M:megabytes (10^6 = 1000^2 bytes)'
  165. 'G:gigabytes (10^9 = 1000^3 bytes)'
  166. 'T:terabytes (10^12 = 1000^4 bytes)'
  167. 'Ki:kibibytes ( 2^10 = 1024 bytes)'
  168. 'Mi:mebibytes ( 2^20 = 1024^2 bytes)'
  169. 'Gi:gigibytes ( 2^30 = 1024^3 bytes)'
  170. 'Ti:tebibytes ( 2^40 = 1024^4 bytes)'
  171. )
  172. _describe -t units 'size limit units' suff -V 'units'
  173. elif compset -P '[-+]'; then
  174. _message -e 'size limit number (full format: <+-><number><unit>)'
  175. else
  176. _values 'size limit prefix (full format: <prefix><number><unit>)' \
  177. '\+[file size must be greater or equal to]'\
  178. '-[file size must be less than or equal to]' && ret=0
  179. fi
  180. ;;
  181. esac
  182. return ret
  183. }
  184. _fd "$@"
  185. # ------------------------------------------------------------------------------
  186. # Copyright (c) 2011 GitHub zsh-users - http://github.com/zsh-users
  187. # All rights reserved.
  188. #
  189. # Redistribution and use in source and binary forms, with or without
  190. # modification, are permitted provided that the following conditions are met:
  191. # * Redistributions of source code must retain the above copyright
  192. # notice, this list of conditions and the following disclaimer.
  193. # * Redistributions in binary form must reproduce the above copyright
  194. # notice, this list of conditions and the following disclaimer in the
  195. # documentation and/or other materials provided with the distribution.
  196. # * Neither the name of the zsh-users nor the
  197. # names of its contributors may be used to endorse or promote products
  198. # derived from this software without specific prior written permission.
  199. #
  200. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  201. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  202. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  203. # DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
  204. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  205. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  206. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  207. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  208. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  209. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  210. # ------------------------------------------------------------------------------
  211. # Description
  212. # -----------
  213. #
  214. # Completion script for fd
  215. #
  216. # ------------------------------------------------------------------------------
  217. # Authors
  218. # -------
  219. #
  220. # * smancill (https://github.com/smancill)
  221. #
  222. # ------------------------------------------------------------------------------
  223. # Local Variables:
  224. # mode: shell-script
  225. # coding: utf-8-unix
  226. # indent-tabs-mode: nil
  227. # sh-indentation: 2
  228. # sh-basic-offset: 2
  229. # End:
  230. # vim: ft=zsh sw=2 ts=2 et