From 9eb200e7f6d20adb0996f5c27f5a80e589c2be87 Mon Sep 17 00:00:00 2001 From: Elias Norrby Date: Thu, 1 Apr 2021 21:48:36 +0200 Subject: [PATCH] feat(zsh): allow multi selection in man_widget(complete) 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. --- shell/zsh/man_widget.zsh | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/shell/zsh/man_widget.zsh b/shell/zsh/man_widget.zsh index 699e453f..103e188c 100644 --- a/shell/zsh/man_widget.zsh +++ b/shell/zsh/man_widget.zsh @@ -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 @@ -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 @@ -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%' @@ -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 } @@ -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"