completion.elv 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use str
  2. use math
  3. use path
  4. fn make-filecomp {|predicate|
  5. put {|command @rest|
  6. var target = $rest[-1]
  7. put $target* | each {|candidate|
  8. var is-dir = (path:is-dir &follow-symlink=$true $candidate)
  9. if ($predicate $candidate $is-dir) {
  10. if $is-dir {
  11. var display = (styled $candidate blue bold)/
  12. edit:complex-candidate $candidate &display=$display &code-suffix=/
  13. } else {
  14. put $candidate
  15. }
  16. }
  17. }
  18. }
  19. }
  20. var files~ = (make-filecomp {|candidate is-dir| not $is-dir})
  21. var dirs~ = (make-filecomp {|candidate is-dir| put $is-dir})
  22. var any~ = (make-filecomp {|@_| put $true})
  23. fn cobra {|command @rest|
  24. var result = [((external $command) __complete $@rest 2>/dev/null)]
  25. var code = (num $result[-1][1..])
  26. var error = (> (% $code 2) 0)
  27. set code = (math:floor (/ $code 2))
  28. var no-space = (> (% $code 2) 0)
  29. set code = (math:floor (/ $code 2))
  30. var no-filecomp = (> (% $code 2) 0)
  31. if $error {
  32. return
  33. }
  34. var candidate = {|line|
  35. var @result = (str:split &max=2 "\t" $line)
  36. var name = $result[0]
  37. var description = $name
  38. if (> (count $result) 1) {
  39. set description = $description' ('$result[1]')'
  40. }
  41. var suffix = ' '
  42. if $no-space {
  43. set suffix = ''
  44. }
  45. edit:complex-candidate $result[0] &display=$description &code-suffix=$suffix
  46. }
  47. if (> (count $result) 1) {
  48. each $candidate $result[..-1]
  49. } elif (not $no-filecomp) {
  50. any $command $@rest
  51. }
  52. }