elvish_config/lib/moi/completion/ssh.elv

107 lines
2.1 KiB
Plaintext

use re
use str
use moi/util/condition
use moi/util/file
use moi/util/ip
use moi/util/list
use moi/util/map
use moi/util/option
options-ssh = [ 1 2 4 6 A D f g I i L l m o v a b C c e F k N n p q R s T t X x ]
options-scp = [ 3 4 6 B C p q r v c F i l o P S ]
names = [
benjamin
luence
]
fn -kh {
cat ~/.ssh/known_hosts | peach [l]{
put [(str:split ' ' $l)]
} | peach [e]{
local:domains @_ = $@e
str:split ',' $domains
}
}
fn -port [cmd @argv]{
local:o = (condition:cset (eq $cmd 'ssh') '-p' '-P')
local:margs = (option:map $argv)
local:p = (map:value-of $margs $o &default=[])
if (list:empty $p) {
put 22
} else {
put $p[-1]
}
}
fn -complete-names []{ each [n]{ put $n'@' } $names }
fn -complete-domains [name hosts]{
each [h]{
put $name@$h
} $hosts
}
fn -complete-remote-dir [port address dir]{
try {
ssh -p $port $address 'ls '$dir | each [f]{
put $address':'$dir$f
}
} except e {
}
}
fn -complete-args [hosts cmd @argv]{
local:arg = $argv[-1]
local:i = (str:index $arg @)
if (< $i 0) {
-complete-names
all $hosts
if (eq $cmd scp) {
edit:complete-filename $cmd $@argv
}
return
}
local:n local:h = $arg[..$i] $arg[(+ $i 1)..]
if (eq $cmd scp) {
i = (str:index $h :)
if (>= $i 0) {
local:d = $h[(+ $i 1)..]
h = $h[..$i]
if (list:includes $h $hosts) {
local:p = (-port $cmd @argv)
local:e = [ (str:split / $d) ]
if (not-eq $e[-1] '') {
d = (str:join / $e[..-1])
if (eq $d '') {
d = /
}
}
-complete-remote-dir $p $n@$h $d
}
return
}
}
-complete-domains $n $hosts
}
fn complete [@argv]{
@hosts = (-kh)
local:cmd = $argv[0]
local:is-ssh = (eq $cmd ssh)
local:po = (condition:cset $is-ssh -p -P)
if (<= (count $argv) 2) {
all (condition:cset $is-ssh $options-ssh $options-scp)
-complete-args $hosts $@argv
} elif (eq $argv[-2] $po) {
put 22
} else {
-complete-args $hosts $@argv
}
}
edit:completion:arg-completer[scp] = $complete~
edit:completion:arg-completer[ssh] = $complete~