12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- use str
- use math
- use path
- fn make-filecomp {|predicate|
- put {|command @rest|
- var target = $rest[-1]
- put $target* | each {|candidate|
- var is-dir = (path:is-dir &follow-symlink=$true $candidate)
- if ($predicate $candidate $is-dir) {
- if $is-dir {
- var display = (styled $candidate blue bold)/
- edit:complex-candidate $candidate &display=$display &code-suffix=/
- } else {
- put $candidate
- }
- }
- }
- }
- }
- var files~ = (make-filecomp {|candidate is-dir| not $is-dir})
- var dirs~ = (make-filecomp {|candidate is-dir| put $is-dir})
- var any~ = (make-filecomp {|@_| put $true})
- fn cobra {|command @rest|
- var result = [((external $command) __complete $@rest 2>/dev/null)]
- var code = (num $result[-1][1..])
- var error = (> (% $code 2) 0)
- set code = (math:floor (/ $code 2))
- var no-space = (> (% $code 2) 0)
- set code = (math:floor (/ $code 2))
- var no-filecomp = (> (% $code 2) 0)
- if $error {
- return
- }
- var candidate = {|line|
- var @result = (str:split &max=2 "\t" $line)
- var name = $result[0]
- var description = $name
- if (> (count $result) 1) {
- set description = $description' ('$result[1]')'
- }
- var suffix = ' '
- if $no-space {
- set suffix = ''
- }
- edit:complex-candidate $result[0] &display=$description &code-suffix=$suffix
- }
- if (> (count $result) 1) {
- each $candidate $result[..-1]
- } elif (not $no-filecomp) {
- any $command $@rest
- }
- }
|