You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

completers.elv 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. use re
  2. use util
  3. use github.com/xiaq/edit.elv/compl/git
  4. git:apply
  5. fn overlap-at [a b]{
  6. for i [(range 1 (- (count $b) 1))] {
  7. if (has-value $a $b[$i]) { put $i; return }
  8. }
  9. put $false
  10. }
  11. fn prefix-completer [p a]{
  12. edit:completion:arg-completer[$p] = [@cmd]{
  13. if (eq (count $cmd) 2) {
  14. $a $@cmd
  15. } elif (has-key $edit:completion:arg-completer $cmd[1]) {
  16. $edit:completion:arg-completer[$cmd[1]] (explode $cmd[1:])
  17. } else {
  18. edit:complete-filename $cmd[-1]
  19. }
  20. }
  21. }
  22. fn complete-directory [a]{
  23. dir = (path-dir $a)/
  24. if (has-prefix $a $dir) {
  25. a = (replaces &max=1 $dir '' $a)
  26. } else {
  27. dir = ''
  28. }
  29. for x [(put $dir*[match-hidden][nomatch-ok]$a*[match-hidden][nomatch-ok])] {
  30. if (-is-dir $x) { edit:complex-candidate &code-suffix=/ &style='blue;bold' $x }
  31. }
  32. }
  33. edit:completion:arg-completer[cd] = [@cmd]{
  34. if (eq (count $cmd) 2) {
  35. complete-directory $cmd[1]
  36. }
  37. }
  38. edit:completion:arg-completer[kak] = [@cmd]{
  39. if (eq $cmd[-2] -c) {
  40. kak -l
  41. } else {
  42. edit:complete-filename $cmd[-1]
  43. }
  44. }
  45. edit:completion:arg-completer[waifu2x-converter-cpp] = [@cmd]{
  46. if (has-value [-i --input -o --output] $cmd[-2]) {
  47. edit:complete-filename $cmd[-1]
  48. } elif (has-value [-m --mode] $cmd[-2]) {
  49. put noise scale noise-scale
  50. } elif (eq $cmd[-2] --noise-level) {
  51. put 0 1 2 3
  52. } else {
  53. put --scale-ratio --noise-level --mode --jobs --png-compression \
  54. --image-quality --silent -i -o
  55. }
  56. }
  57. kitty-cmds = []
  58. kitty-kittens = []
  59. edit:completion:arg-completer[kitty] = [@cmd]{
  60. if (== (count $kitty-cmds) 0) {
  61. @kitty-cmds = (kitty @ --help | peach [x]{ if (re:match '^ \w' $x) { put $x[2:] } })
  62. @kitty-kittens = (pwd=/usr/lib/kitty/kittens fd main.py | peach [x]{ path-dir $x })
  63. }
  64. if (has-value [kitten '+kitten'] $cmd[-2]) {
  65. explode $kitty-kittens
  66. } elif (eq $cmd[-2] '@') {
  67. explode $kitty-cmds
  68. } else {
  69. edit:complete-filename $cmd[-1]
  70. }
  71. }
  72. edit:completion:arg-completer[sv] = [@cmd]{
  73. if (eq (count $cmd) 2) {
  74. put status up down once pause cont hup alarm interrupt quit kill term 1 2 \
  75. exit start try-restart check {force-,}{stop,reload,restart,shutdown}
  76. } else {
  77. pwd=/var/service put *
  78. }
  79. }
  80. edit:completion:arg-completer[man] = [@cmd]{
  81. pwd=/usr/share/man put man*/* | peach [a]{
  82. re:replace &literal=$true '\.\dp?$' '' (path-base $a)
  83. }
  84. }
  85. edit:completion:arg-completer[kill] = [@cmd]{
  86. ps -u (whoami) --no-headers -o pid,command |\
  87. eawk [_ p @c]{ edit:complex-candidate &display-suffix=(print ' '$@c) $p }
  88. }
  89. edit:completion:arg-completer[nimble] = [@cmd]{
  90. if (eq (count $cmd) 2) {
  91. put {un,}install develop check init publish build c cc js test doc{,2} \
  92. refresh search list tasks path dump
  93. if ?(isnimbleproject) {
  94. nimble tasks 2>/dev/null | eawk [_ a @_]{ put $a }
  95. }
  96. } elif (eq $cmd[-2] install) {
  97. for x (cat ~/.nimble/packages_official.json | from-json) { put $x[name] }
  98. } elif (eq $cmd[-2] uninstall) {
  99. pkgs = [&]
  100. nimble list -i | eawk [_ n @v]{
  101. @ver = $@v[:-1]
  102. ver[0] = $ver[0][1:]
  103. pkgs[$n] = $ver
  104. }
  105. if (has-suffix $cmd[-1] '@') {
  106. for v $pkgs[$cmd[-1][:-1]] {
  107. put $cmd[-1]$v
  108. }
  109. } else { keys $pkgs }
  110. }
  111. }
  112. pijul-cmds = [add apply branches checkout clone credit delete-branch diff dist\
  113. generate-completions help init key log ls mv patch pull push\
  114. record remove revert rollback show-dependencies sign status tag unrecord]
  115. edit:completion:arg-completer[pijul] = [@cmd]{
  116. if (eq (count $cmd) 2) {
  117. explode $pijul-cmds
  118. }
  119. }
  120. neofetch-img = [ascii caca iterm2 jp2a kitty pixterm sixel termpix tycat w3m off]
  121. neofetch-opts = []
  122. edit:completion:arg-completer[neofetch] = [@cmd]{
  123. if (== (count $neofetch-opts) 0) {
  124. neofetch-opts = [(_ = ?(neofetch --help | each [x]{
  125. if (has-prefix $x ' --') {
  126. put $x | eawk [_ a @_]{ put $a }
  127. }
  128. })) --logo -L -v -vv]
  129. }
  130. explode $neofetch-opts
  131. }
  132. edit:completion:arg-completer[bspc] = [@cmd]{
  133. if (eq (count $cmd) 2) {
  134. put node desktop monitor query wm rule config subscribe quit
  135. } elif (eq $cmd[1] subscribe) {
  136. put all report monitor desktop node pointer
  137. } elif (eq $cmd[1] rule) {
  138. put --add --remove --list
  139. } elif (eq $cmd[1] wm) {
  140. put --dump-state --load-state --add-monitor --adopt-orphans --record-history --get-status
  141. } elif (eq $cmd[1] query) {
  142. put --nodes --desktops --monitors --tree
  143. }
  144. }
  145. edit:completion:arg-completer[ntr] = [@cmd]{
  146. if (not (has-prefix $cmd[-1] '-')) {
  147. pwd=$E:XDG_CONFIG_HOME/ntr/contexts put **
  148. }
  149. }
  150. edit:completion:arg-completer[update] = [@cmd]{
  151. update | each [x]{ if (has-prefix $x " ") { put $x[4:] } }
  152. }
  153. edit:completion:arg-completer[xr] = [@cmd]{
  154. xpkg -m
  155. xpkg -O | peach [x]{ edit:complex-candidate &style='red;inverse' $x }
  156. }
  157. edit:completion:arg-completer[xi] = [@cmd]{
  158. pwd=$E:XBPS_DISTDIR/srcpkgs put *
  159. }
  160. xbps-src-cmds = []
  161. xbps-src-arch = []
  162. edit:completion:arg-completer[xbps-src] = [@cmd]{
  163. if (== (count $xbps-src-cmds) 0) {
  164. @xbps-src-cmds = (xbps-src -h | take 129 | drop 4 | each [x]{ put (re:find &max=1 '^\w+(\-\w+)*' $x)[text] })
  165. @xbps-src-arch = (xbps-src -h | take 162 | drop 136)[1:]
  166. }
  167. if (eq $cmd[-2] '-a') {
  168. explode $xbps-src-arch
  169. } else {
  170. if (not (overlap-at $xbps-src-cmds $cmd)) {
  171. explode $xbps-src-cmds
  172. } else {
  173. pwd=$E:XBPS_DISTDIR/srcpkgs put *
  174. }
  175. }
  176. }
  177. edit:completion:arg-completer[strat] = [@cmd]{
  178. @strata = (brl list)
  179. has-strat = (overlap-at $strata $cmd)
  180. if (not $has-strat) {
  181. explode $strata
  182. } else {
  183. # edit:complete-sudo (explode $cmd[(+ $has-strat 1):])
  184. }
  185. }
  186. brl-cmds = []
  187. edit:completion:arg-completer[brl] = [@cmd]{
  188. if (== (count $brl-cmds) 0) {
  189. @brl-cmds = (brl -h | take 35 | drop 5 | each [x]{ put (re:find &max=1 '^ \w+' $x)[text][2:] })
  190. }
  191. len = (count $cmd)
  192. if (== $len 2) {
  193. explode $brl-cmds
  194. } else {
  195. c = $cmd[1]
  196. if (has-value [status enable disable hide show] $c) {
  197. brl list
  198. } elif (eq $c fetch) {
  199. brl fetch -L
  200. }
  201. }
  202. }
  203. edit:completion:arg-completer[promotescript] = [@cmd]{
  204. pwd=~/.local/bin fd -t f
  205. }
  206. edit:completion:arg-completer[edit-script] = $edit:complete-sudo~
  207. edit:completion:arg-completer[whereis] = $edit:complete-sudo~
  208. edit:completion:arg-completer[which] = $edit:complete-sudo~
  209. edit:completion:arg-completer[xq] = $edit:completion:arg-completer[xi]
  210. edit:completion:arg-completer[xqt] = $edit:completion:arg-completer[xi]
  211. edit:completion:arg-completer[xbps-install] = $edit:completion:arg-completer[xi]
  212. prefixes = [
  213. &sudo=$edit:complete-sudo~
  214. &strace=$edit:complete-sudo~
  215. &time=$edit:complete-sudo~
  216. &torify=$edit:complete-sudo~
  217. ]
  218. for k [(keys $prefixes)] { prefix-completer $k $prefixes[$k] }