forked from sunlei/zsh-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-ssh.zsh
238 lines (188 loc) · 5.8 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env zsh
# Better completion for ssh in Zsh.
# https://github.com/sunlei/zsh-ssh
# v0.0.6
# Copyright (c) 2020 Sunlei <[email protected]>
SSH_CONFIG_FILE="${SSH_CONFIG_FILE:-$HOME/.ssh/config}"
# Parse the file and handle the include directive.
_parse_config_file() {
# Enable PCRE matching
setopt localoptions rematchpcre
local config_file_path=$(realpath "$1")
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ $line =~ ^[Ii]nclude[[:space:]]+(.*) ]] && (( $#match > 0 )); then
local include_path="${match[1]}"
# Replace the first occurrence of "~" in the string with the value of the environment variable HOME.
local expanded_include_path=${include_path/#\~/$HOME}
# `~` used to force the expansion of wildcards in variables
for include_file_path in $~expanded_include_path; do
if [[ -f "$include_file_path" ]]; then
# Insert a blank line between the included files
echo ""
_parse_config_file "$include_file_path"
fi
done
else
echo "$line"
fi
done < "$config_file_path"
}
_ssh-host-list() {
local ssh_config host_list
ssh_config=$(_parse_config_file $SSH_CONFIG_FILE)
ssh_config=$(echo $ssh_config | command grep -v -E "^\s*#[^_]")
# Make sure each Host line is preceded by an empty line
ssh_config=$(echo $ssh_config | command sed 's/[[:space:]]*Host\b/\nHost /g')
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) {
# Manualy remove leading spaces for split with regex
line = gensub(/^\s+/, "", "g", line)
n = split(line, line_array, " +|=")
key = line_array[1]
value = join(line_array, 2, n, " ")
return key "#-#" value
}
function contains_star(str) {
return index(str, "*") > 0
}
function starts_or_ends_with_star(str) {
start_char = substr(str, 1, 1)
end_char = substr(str, length(str), 1)
return start_char == "*" || end_char == "*"
}
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") { aliases = value }
if (key == "Hostname") { host_name = value }
if (key == "#_Desc") { desc = value }
}
split(aliases, alias_list, " ")
for (i in alias_list) {
alias = alias_list[i]
if (!host_name && alias ) {
host_name = alias
}
if (desc) {
desc_formated = sprintf("[\033[00;34m%s\033[0m]", desc)
}
if ((host_name && !starts_or_ends_with_star(host_name)) && (alias && !starts_or_ends_with_star(alias))) {
host = sprintf("%s|->|%s|%s\n", alias, host_name, desc_formated)
host_list = host_list host
}
}
}
END {
print host_list
}
')
for arg in "$@"; do
case $arg in
-*) shift;;
*) break;;
esac
done
host_list=$(command grep -i "$1" <<< "$host_list")
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 > ' \
--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
# vim: set ft=zsh sw=2 ts=2 et