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

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