Skip to content

Commit

Permalink
feat(zsh): allow multi selection in man_widget(complete)
Browse files Browse the repository at this point in the history
When in complete mode, all selection of multiple flags. The flags will
be added to the command link in the order they were selected. If one or
more flags require an argument, one has to add that after the fact.
  • Loading branch information
eliasnorrby committed Apr 1, 2021
1 parent 64ae48c commit 9eb200e
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions shell/zsh/man_widget.zsh
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
PREVIEW_SCRIPT=preview_flag_docs
MODE=""

man-widget() {
local mode="complete"
MODE="complete"
local command=($(get-command-from-buffer | xargs))

if [ -z "$command" ]; then
command=($(select-command | xargs))
mode="lookup"
MODE="lookup"
fi

[ -z "$command" ] && return
Expand All @@ -19,18 +20,18 @@ man-widget() {

[ -z "$flag" ] && return

case $mode in
case $MODE in
lookup)
echo
# "${PREVIEW_SCRIPT}" "$flag" ${command[@]}
go-to-docs "$flag" ${command[@]}
;;
complete)
add-flag "$flag"
add-flags "$flag"
;;
esac

# zle reset-prompt
zle reset-prompt
}

zle -N man-widget
Expand Down Expand Up @@ -58,6 +59,7 @@ select-flag() {
local command=("$@")
local preview
local window
local multi
if command -v "${PREVIEW_SCRIPT}" >/dev/null 2>&1; then
preview="${PREVIEW_SCRIPT} {} ${command[*]}"
window='down:70%'
Expand All @@ -66,12 +68,18 @@ select-flag() {
window='down:10%'
fi

local fzf_cmd=("fzf")
fzf_cmd+=('--preview' "${preview}")
fzf_cmd+=('--preview-window' "${window}")

if [ "$MODE" = "complete" ]; then
fzf_cmd+=('--multi')
fi

man-or-help ${command[@]} \
| grep '^[[:blank:]]*-[^ ]' \
| sed -e 's/^[[:blank:]]*//' -e '/^---/d' \
| fzf \
--preview "${preview}" \
--preview-window "${window}" \
| "$fzf_cmd[@]" \
| strip-to-only-flag
}

Expand Down Expand Up @@ -109,6 +117,12 @@ strip-to-only-flag() {
cut -d ',' -f1 | cut -d ' ' -f1
}

add-flags() {
for flag in ${=1}; do
add-flag "$flag"
done
}

add-flag() {
if [ "${LBUFFER: -1}" = " " ]; then
LBUFFER="${LBUFFER}$1"
Expand Down

0 comments on commit 9eb200e

Please sign in to comment.