forked from sunlei/zsh-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-ssh.zsh
207 lines (163 loc) · 4.47 KB
/
zsh-ssh.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env zsh
# Better completion for ssh in Zsh.
# https://github.com/sunlei/zsh-ssh
# v0.0.2
# Copyright (c) 2020 Sunlei <[email protected]>
_ssh-host-list() {
local ssh_config host_list
pushd "$HOME/.ssh" >/dev/null
ssh_config=$(command awk '
{
if (NF == 2 && tolower($1) == "include")
{
cmd = "sed -s '\''$G'\'' " $2 " 2> /dev/null"
# print cmd
while ( (cmd | getline line) > 0 ) {
print line
}
close($2)
}
else {
print
}
}
' config)
ssh_config=$(echo $ssh_config | command grep -v -E "^\s*#[^_]")
popd >/dev/null
host_list=$(echo $ssh_config | command awk '
function join(array, start, end, sep, result, i)
{
# https://www.gnu.org/software/gawk/manual/html_node/Join-Function.html
if (sep == "")
sep = " "
else if (sep == SUBSEP) # magic value
sep = ""
result = array[start]
for (i = start + 1; i <= end; i++)
result = result sep array[i]
return result
}
function parse_line(line)
{
n = split(line, line_array, " ")
key = line_array[1]
value = join(line_array, 2, n)
return key "#-#" value
}
BEGIN {
IGNORECASE = 1
FS="\n"
RS=""
host_list = ""
}
{
host_name = ""
alias = ""
desc = ""
desc_formated = ""
for (line_num = 1; line_num <= NF; ++line_num) {
line = parse_line($line_num)
split(line, tmp, "#-#")
key = tmp[1]
value = tmp[2]
if (key == "Host") { alias = value }
if (key == "Hostname") { host_name = value }
if (key == "#_Desc") { desc = value }
}
if (!host_name && alias ) {
host_name = alias
}
if (desc) {
desc_formated = sprintf("[\033[00;34m%s\033[0m]", desc)
}
if ((host_name && host_name != "*") || (alias && alias != "*")) {
host = sprintf("%s|->|%s|%s\n", alias, host_name, desc_formated)
host_list = host_list host
}
}
END {
print host_list
}
')
if [ -n "$1" ]; then
host_list=$(command grep -i "$1" <<< "$host_list")
fi
host_list=$(echo $host_list | command sort -u)
echo $host_list
}
_fzf-list-generator() {
local header host_list
if [ -n "$1" ]; then
host_list="$1"
else
host_list=$(_ssh-host-list)
fi
header="
Alias|->|Hostname|Desc
─────|──|────────|────
"
host_list="${header}\n${host_list}"
echo $host_list | command column -t -s '|'
}
_set-lbuffer() {
local result selected_host connect_cmd is_fzf_result
result="$1"
is_fzf_result="$2"
if [ "$is_fzf_result" = false ] ; then
result=$(cut -f 1 -d "|" <<< ${result})
fi
selected_host=$(cut -f 1 -d " " <<< ${result})
connect_cmd="ssh ${selected_host}"
LBUFFER="$connect_cmd"
}
fzf-complete-ssh() {
local tokens cmd result selected_host
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
tokens=(${(z)LBUFFER})
cmd=${tokens[1]}
if [[ "$LBUFFER" =~ "^ *ssh$" ]]; then
zle ${fzf_ssh_default_completion:-expand-or-complete}
elif [[ "$cmd" == "ssh" ]]; then
result=$(_ssh-host-list ${tokens[2, -1]})
if [ -z "$result" ]; then
zle ${fzf_ssh_default_completion:-expand-or-complete}
return
fi
if [ $(echo $result | wc -l) -eq 1 ]; then
_set-lbuffer $result false
zle reset-prompt
# zle redisplay
return
fi
result=$(_fzf-list-generator $result | fzf \
--height 40% \
--ansi \
--border \
--cycle \
--info=inline \
--header-lines=2 \
--reverse \
--prompt='SSH Remote > ' \
--no-separator \
--bind 'shift-tab:up,tab:down,bspace:backward-delete-char/eof' \
--preview 'ssh -T -G $(cut -f 1 -d " " <<< {}) | grep -i -E "^User |^HostName |^Port |^ControlMaster |^ForwardAgent |^LocalForward |^IdentityFile |^RemoteForward |^ProxyCommand |^ProxyJump " | column -t' \
--preview-window=right:40%
)
if [ -n "$result" ]; then
_set-lbuffer $result true
zle accept-line
fi
zle reset-prompt
# zle redisplay
# Fall back to default completion
else
zle ${fzf_ssh_default_completion:-expand-or-complete}
fi
}
[ -z "$fzf_ssh_default_completion" ] && {
binding=$(bindkey '^I')
[[ $binding =~ 'undefined-key' ]] || fzf_ssh_default_completion=$binding[(s: :w)2]
unset binding
}
zle -N fzf-complete-ssh
bindkey '^I' fzf-complete-ssh