diff --git a/.config/zsh/autoload/gstash b/.config/zsh/autoload/gstash new file mode 100755 index 0000000..0c2248e --- /dev/null +++ b/.config/zsh/autoload/gstash @@ -0,0 +1,123 @@ +## Author: druckdev +## Created: 2021-05-14 +## +## A TUI for git-stash using fzf. +## Displays git-stash list and git-show as preview command for each stash. + +# extendedglob is necessary for the expansion of the binds array +emulate -L zsh -o extendedglob + +# Return if fzf is not available +if ! command -v fzf &>/dev/null; then + printf "command not found: fzf" >&2 + return 1 +fi + +# Return if not in git repo +git rev-parse || return + +# One line format for fzf list view +# abbreviated commit hash (yellow), title and ref names +local formatshort='--pretty=format:%C(yellow)%h %Creset%s%C(auto)%d' +# Verbose format for the preview window on the right +# This array is stitched together with newlines later +local -a format=( + '--pretty=format:%C(yellow)' # newline created by this eaten by %- + '%-Commit: %H%C(auto)' # yellow commit hash + ' %D%Cblue' # auto colored ref names (if any) + 'Author: %aN %aE%Cred' # blue author mail + 'AuthorDate: %ad%Cblue' # red author date + 'Commit: %cN %cE%Cred' # blue commiter mail + 'CommitDate: %cd%Cblue' # red commit date + 'Signer: %GS%Cgreen' # signer name + 'Fingerprint: %GF' # pgp fingerprint + '%Creset%C(bold)' # empty line + ' %s%Creset' # bold white subject + '' # newline + '%-b' # body + '' # newline +) + +# Ignore the graph part at the beginning, then capture the commit hash and throw +# away the rest of the line. +local commit_hash='s/^[^a-f0-9]*([a-f0-9]*).*$/\1/' + +local dateshort='--date=format:%F' # year +local date="$dateshort %T %z" # year time timezone + +# Put the commit hash into the clipboard +# (If no known clipboard tool is available, just print it) +local fzf_copy_command="echo -nE {} | sed -E '$commit_hash'" +if [[ $OSTYPE =~ darwin ]] && command -v pbcopy &>/dev/null; then + fzf_copy_command+=" | pbcopy" +elif command -v xclip &>/dev/null; then + fzf_copy_command+=" | xclip -selection c" +fi + +local -A fzf_preview +read -r -d '' </dev/null; then + fzf_preview[patch]+=" | diff-so-fancy --color=always" +fi +fzf_preview[patch]+=" }" + +read -r -d '' < $(tput lines) )); then + fzf_args+=(--preview-window=right) +else + fzf_args+=(--preview-window=down) +fi + +# Display an ascii graph of the commits in the above format and pipe that into +# fzf. +git stash list "$formatshort" --color=always "$@" \ +| fzf "${fzf_args[@]}" +return 0 + +# vim: ft=zsh