From d2d1ab76c7986558be86f7a745a2529bdb9dbb37 Mon Sep 17 00:00:00 2001 From: Wenxuan Zhang Date: Wed, 29 May 2019 18:11:11 +0800 Subject: [PATCH] Support multiple lines fzf opts. Former-commit-id: 0d542fb3f48ddd4b0da3823cb5dc5d35faa5f3a6 --- forgit.plugin.zsh | 115 +++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 43 deletions(-) diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index e2b04f1..18825ce 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -12,40 +12,54 @@ hash emojify &>/dev/null && forgit_emojify='|emojify' # git commit viewer forgit::log() { forgit::inside_work_tree || return 1 - local cmd="echo {} |grep -o '[a-f0-9]\{7\}' |head -1 |xargs -I% git show --color=always % $* $forgit_emojify $forgit_fancy" + local cmd opts + cmd="echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % $* $forgit_emojify $forgit_fancy" + opts=" + $FORGIT_FZF_DEFAULT_OPTS + +s +m --tiebreak=index --preview=\"$cmd\" + --bind=\"enter:execute($cmd |LESS='-R' less)\" + --bind=\"ctrl-y:execute-silent(echo {} |grep -o '[a-f0-9]\{7\}' |${FORGIT_COPY_CMD:-pbcopy})\" + $FORGIT_LOG_FZF_OPTS + " eval "git log --graph --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' $* $forgit_emojify" | - forgit::fzf +s +m --tiebreak=index \ - --bind="enter:execute($cmd |LESS='-R' less)" \ - --bind="ctrl-y:execute-silent(echo {} |grep -o '[a-f0-9]\{7\}' |${FORGIT_COPY_CMD:-pbcopy})" \ - --preview="$cmd" $FORGIT_LOG_FZF_OPTS + FZF_DEFAULT_OPTS="$opts" fzf } # git diff viewer forgit::diff() { forgit::inside_work_tree || return 1 - local cmd files + local cmd files opts cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy" files="$*" [[ $# -eq 0 ]] && files=$(git rev-parse --show-toplevel) - git ls-files --modified "$files"| - forgit::fzf +m -0 \ - --bind="enter:execute($cmd |LESS='-R' less)" \ - --preview="$cmd" $FORGIT_DIFF_FZF_OPTS + + opts=" + $FORGIT_FZF_DEFAULT_OPTS + +m -0 --preview=\"$cmd\" --bind=\"enter:execute($cmd |LESS='-R' less)\" + $FORGIT_DIFF_FZF_OPTS + " + git ls-files --modified "$files" | + FZF_DEFAULT_OPTS="$opts" fzf } # git add selector forgit::add() { forgit::inside_work_tree || return 1 - local changed unmerged untracked files + local changed unmerged untracked files opts changed=$(git config --get-color color.status.changed red) unmerged=$(git config --get-color color.status.unmerged red) untracked=$(git config --get-color color.status.untracked red) + + opts=" + $FORGIT_FZF_DEFAULT_OPTS + -0 -m --nth 2..,.. + --preview=\"git diff --color=always -- {-1} $forgit_emojify $forgit_fancy\" + $FORGIT_ADD_FZF_OPTS + " files=$(git -c color.status=always status --short | - grep -F -e "$changed" -e "$unmerged" -e "$untracked"| + grep -F -e "$changed" -e "$unmerged" -e "$untracked" | awk '{printf "[%10s] ", $1; $1=""; print $0}' | - forgit::fzf -0 -m --nth 2..,.. \ - --preview="git diff --color=always -- {-1} $forgit_emojify $forgit_fancy" $FORGIT_ADD_FZF_OPTS | - cut -d] -f2 | + FZF_DEFAULT_OPTS="$opts" fzf | cut -d] -f2 | sed 's/.* -> //') # for rename case [[ -n "$files" ]] && echo "$files" |xargs -I{} git add {} && git status --short && return echo 'Nothing to add.' @@ -54,10 +68,14 @@ forgit::add() { # git checkout-restore selector forgit::restore() { forgit::inside_work_tree || return 1 - local cmd files + local cmd files opts cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy" - files="$(git ls-files --modified "$(git rev-parse --show-toplevel)"| - forgit::fzf -m -0 --preview="$cmd" $FORGIT_CHECKOUT_FZF_OPTS)" + opts=" + $FORGIT_FZF_DEFAULT_OPTS + -m -0 --preview=\"$cmd\" + $FORGIT_CHECKOUT_FZF_OPTS + " + files="$(git ls-files --modified "$(git rev-parse --show-toplevel)"| FZF_DEFAULT_OPTS="$opts" fzf)" [[ -n "$files" ]] && echo "$files" |xargs -I{} git checkout {} && git status --short && return echo 'Nothing to restore.' } @@ -65,20 +83,28 @@ forgit::restore() { # git stash viewer forgit::stash::show() { forgit::inside_work_tree || return 1 - local cmd="git stash show \$(echo {}| cut -d: -f1) --color=always --ext-diff $forgit_fancy" - git stash list | - forgit::fzf +s +m -0 --tiebreak=index \ - --bind="enter:execute($cmd |LESS='-R' less)" \ - --preview="$cmd" $FORGIT_STASH_FZF_OPTS + local cmd opts + cmd="git stash show \$(echo {}| cut -d: -f1) --color=always --ext-diff $forgit_fancy" + opts=" + $FORGIT_FZF_DEFAULT_OPTS + +s +m -0 --tiebreak=index --preview=\"$cmd\" --bind=\"enter:execute($cmd |LESS='-R' less)\" + $FORGIT_STASH_FZF_OPTS + " + git stash list | FZF_DEFAULT_OPTS="$opts" fzf } # git clean selector forgit::clean() { forgit::inside_work_tree || return 1 - local files + local files opts + opts=" + $FORGIT_FZF_DEFAULT_OPTS + -m -0 + $FORGIT_CLEAN_FZF_OPTS + " # Note: Postfix '/' in directory path should be removed. Otherwise the directory itself will not be removed. - files=$(git clean -xdfn "$@"| awk '{print $3}'| forgit::fzf -m -0 $FORGIT_CLEAN_FZF_OPTS |sed 's#/$##') - [[ -n "$files" ]] && echo "$files" |xargs -I{} git clean -xdf {} + files=$(git clean -xdfn "$@"| awk '{print $3}'| FZF_DEFAULT_OPTS="$opts" fzf |sed 's#/$##') + [[ -n "$files" ]] && echo "$files" |xargs -I% git clean -xdf % && return echo 'Nothing to clean.' } @@ -89,13 +115,18 @@ export FORGIT_GI_TEMPLATES=${FORGIT_GI_TEMPLATES:-$FORGIT_GI_REPO_LOCAL/template forgit::ignore() { [ -d "$FORGIT_GI_REPO_LOCAL" ] || forgit::ignore::update - local IFS cmd args cat + local IFS cmd args cat opts # https://github.com/sharkdp/bat.git hash bat &>/dev/null && cat='bat -l gitignore --color=always' || cat="cat" cmd="$cat $FORGIT_GI_TEMPLATES/{2}{,.gitignore} 2>/dev/null" + opts=" + $FORGIT_FZF_DEFAULT_OPTS + -m --preview=\"$cmd\" --preview-window='right:70%' + $FORGIT_IGNORE_FZF_OPTS + " # shellcheck disable=SC2206,2207 IFS=$'\n' args=($@) && [[ $# -eq 0 ]] && args=($(forgit::ignore::list | nl -nrn -w4 -s' ' | - forgit::fzf -m --preview="$cmd" --preview-window="right:70%" $FORGIT_IGNORE_FZF_OPTS |awk '{print $2}')) + FZF_DEFAULT_OPTS="$opts" fzf |awk '{print $2}')) [ ${#args[@]} -eq 0 ] && return 1 # shellcheck disable=SC2068 if hash bat &>/dev/null; then @@ -131,21 +162,19 @@ forgit::ignore::clean() { [[ -d "$FORGIT_GI_REPO_LOCAL" ]] && rm -rf "$FORGIT_GI_REPO_LOCAL" } -forgit::fzf() { - FZF_DEFAULT_OPTS=" - $FZF_DEFAULT_OPTS - --ansi - --height '80%' - --bind='alt-k:preview-up,alt-p:preview-up' - --bind='alt-j:preview-down,alt-n:preview-down' - --bind='ctrl-r:toggle-all' - --bind='ctrl-s:toggle-sort' - --bind='?:toggle-preview' - --preview-window='right:60%' - --bind='alt-w:toggle-preview-wrap' - $FORGIT_FZF_DEFAULT_OPTS - " fzf "$@" -} +FORGIT_FZF_DEFAULT_OPTS=" +$FZF_DEFAULT_OPTS +--ansi +--height='80%' +--bind='alt-k:preview-up,alt-p:preview-up' +--bind='alt-j:preview-down,alt-n:preview-down' +--bind='ctrl-r:toggle-all' +--bind='ctrl-s:toggle-sort' +--bind='?:toggle-preview' +--bind='alt-w:toggle-preview-wrap' +--preview-window='right:60%' +$FORGIT_FZF_DEFAULT_OPTS +" # register aliases # shellcheck disable=SC2139