_fd 11 KB

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