-
Notifications
You must be signed in to change notification settings - Fork 0
/
popman.plugin.zsh
executable file
·55 lines (48 loc) · 1.16 KB
/
popman.plugin.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
#!/bin/env zsh
extract_commands() {
local cmd_line=$1
echo "$cmd_line" \
| sed "s/'[^']*'//g; s/-\S*//g" \
| xargs \
| awk -F'\\$\\(|)|\\|' '{
for (i=1; i<=NF; i++) {
split($i, a, " ");
if (a[1] == "sudo" || a[1] == "xargs" && a[2] != "") {
print a[1]
print a[2]
}
else if (a[1] != "") {
print a[1]
}
}
}' \
| uniq \
| tac
}
popman() {
local curr_buffer=$BUFFER
local cmds=$(extract_commands "$curr_buffer")
local cmd_count=$(echo "$cmds" | wc -l)
if [ "$cmd_count" -eq 0 ]; then
return;
fi
local choice
if [ "$cmd_count" -eq 1 ]; then
choice=$(echo "$cmds" | head -n 1)
else
# TODO: This should happen in the tmux popup instead of direcly in the buffer
choice=$(echo "$cmds" | fzf --height 5 --layout=reverse --prompt="Select the tool you need help with: " --print-query | tr -d '\n')
fi
if [ "${TMUX}" ]; then
tmux popup -EE -h 90% -w 90% man "$choice"
else
BUFFER=""
zle redisplay
man "$choice"
fi
BUFFER=$curr_buffer
CURSOR=$#BUFFER
zle redisplay
}
zle -N popman
bindkey '^K' popman