mirror of
https://github.com/wfxr/forgit.git
synced 2026-09-10 15:26:16 -04:00
Improve git color process logic. #15
This commit is contained in:
parent
621676feb2
commit
c14ce9312a
|
|
@ -1,62 +1,18 @@
|
||||||
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::color_to_grep_code() {
|
forgit::warn() { printf "%b[Warn]%b %s\n" '\e[0;33m' '\e[0m' "$@" >&2; }
|
||||||
case "$1" in
|
forgit::info() { printf "%b[Info]%b %s\n" '\e[0;32m' '\e[0m' "$@" >&2; }
|
||||||
black)
|
forgit::inside_work_tree() { git rev-parse --is-inside-work-tree >/dev/null; }
|
||||||
echo -E '\[30m'
|
|
||||||
;;
|
|
||||||
red)
|
|
||||||
echo -E '\[31m'
|
|
||||||
;;
|
|
||||||
green)
|
|
||||||
echo -E '\[32m'
|
|
||||||
;;
|
|
||||||
yellow)
|
|
||||||
echo -E '\[33m'
|
|
||||||
;;
|
|
||||||
blue)
|
|
||||||
echo -E '\[34m'
|
|
||||||
;;
|
|
||||||
magenta)
|
|
||||||
echo -E '\[35m'
|
|
||||||
;;
|
|
||||||
cyan)
|
|
||||||
echo -E '\[36m'
|
|
||||||
;;
|
|
||||||
white)
|
|
||||||
echo -E '\[97m'
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
|
# https://github.com/so-fancy/diff-so-fancy
|
||||||
# diff is fancy with diff-so-fancy!
|
|
||||||
hash diff-so-fancy &>/dev/null && forgit_fancy='|diff-so-fancy'
|
hash diff-so-fancy &>/dev/null && forgit_fancy='|diff-so-fancy'
|
||||||
hash emojify &>/dev/null && forgit_emojify='|emojify'
|
# https://github.com/wfxr/emoji-cli
|
||||||
|
hash emojify &>/dev/null && forgit_emojify='|emojify'
|
||||||
forgit::inside_work_tree() {
|
|
||||||
git rev-parse --is-inside-work-tree >/dev/null
|
|
||||||
}
|
|
||||||
|
|
||||||
# git commit viewer
|
# git commit viewer
|
||||||
forgit::log() {
|
forgit::log() {
|
||||||
forgit::inside_work_tree || return 1
|
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="echo {} |grep -o '[a-f0-9]\{7\}' |head -1 |xargs -I% git show --color=always % $* $forgit_emojify $forgit_fancy"
|
||||||
eval "git log --graph --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' $@ $forgit_emojify" |
|
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 \
|
forgit::fzf +s +m --tiebreak=index \
|
||||||
--bind="enter:execute($cmd |LESS='-R' less)" \
|
--bind="enter:execute($cmd |LESS='-R' less)" \
|
||||||
--bind="ctrl-y:execute-silent(echo {} |grep -o '[a-f0-9]\{7\}' |${FORGIT_COPY_CMD:-pbcopy})" \
|
--bind="ctrl-y:execute-silent(echo {} |grep -o '[a-f0-9]\{7\}' |${FORGIT_COPY_CMD:-pbcopy})" \
|
||||||
|
|
@ -66,8 +22,10 @@ forgit::log() {
|
||||||
# git diff viewer
|
# git diff viewer
|
||||||
forgit::diff() {
|
forgit::diff() {
|
||||||
forgit::inside_work_tree || return 1
|
forgit::inside_work_tree || return 1
|
||||||
local cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy"
|
local cmd files
|
||||||
[[ $# -eq 0 ]] && local files=$(git rev-parse --show-toplevel) || local files="$@"
|
cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy"
|
||||||
|
files="$*"
|
||||||
|
[[ $# -eq 0 ]] && files=$(git rev-parse --show-toplevel)
|
||||||
git ls-files --modified "$files"|
|
git ls-files --modified "$files"|
|
||||||
forgit::fzf +m -0 \
|
forgit::fzf +m -0 \
|
||||||
--bind="enter:execute($cmd |LESS='-R' less)" \
|
--bind="enter:execute($cmd |LESS='-R' less)" \
|
||||||
|
|
@ -77,14 +35,12 @@ forgit::diff() {
|
||||||
# git add selector
|
# git add selector
|
||||||
forgit::add() {
|
forgit::add() {
|
||||||
forgit::inside_work_tree || return 1
|
forgit::inside_work_tree || return 1
|
||||||
local added=${$(git config color.status.untracked):-red}
|
local changed unmerged untracked files
|
||||||
added=$(forgit::color_to_grep_code "$added")
|
changed=$(git config --get-color color.status.changed red)
|
||||||
local changed=${$(git config color.status.changed):-red}
|
unmerged=$(git config --get-color color.status.unmerged red)
|
||||||
changed=$(forgit::color_to_grep_code "$changed")
|
untracked=$(git config --get-color color.status.untracked red)
|
||||||
local unmerged=${$(git config color.status.unmerged):-red}
|
files=$(git -c color.status=always status --short |
|
||||||
unmerged=$(forgit::color_to_grep_code "$unmerged")
|
grep -F -e "$changed" -e "$unmerged" -e "$untracked"|
|
||||||
local files=$(git -c color.status=always status --short |
|
|
||||||
grep -e "$added" -e "$changed" -e "$unmerged" |
|
|
||||||
awk '{printf "[%10s] ", $1; $1=""; print $0}' |
|
awk '{printf "[%10s] ", $1; $1=""; print $0}' |
|
||||||
forgit::fzf -0 -m --nth 2..,.. \
|
forgit::fzf -0 -m --nth 2..,.. \
|
||||||
--preview="git diff --color=always -- {-1} $forgit_emojify $forgit_fancy" |
|
--preview="git diff --color=always -- {-1} $forgit_emojify $forgit_fancy" |
|
||||||
|
|
@ -97,9 +53,10 @@ forgit::add() {
|
||||||
# git checkout-restore selector
|
# git checkout-restore selector
|
||||||
forgit::restore() {
|
forgit::restore() {
|
||||||
forgit::inside_work_tree || return 1
|
forgit::inside_work_tree || return 1
|
||||||
local cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy"
|
local cmd files
|
||||||
local files=$(git ls-files --modified $(git rev-parse --show-toplevel)|
|
cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy"
|
||||||
forgit::fzf -m -0 --preview="$cmd")
|
files="$(git ls-files --modified "$(git rev-parse --show-toplevel)"|
|
||||||
|
forgit::fzf -m -0 --preview="$cmd")"
|
||||||
[[ -n "$files" ]] && echo "$files" |xargs -I{} git checkout {} && git status --short && return
|
[[ -n "$files" ]] && echo "$files" |xargs -I{} git checkout {} && git status --short && return
|
||||||
echo 'Nothing to restore.'
|
echo 'Nothing to restore.'
|
||||||
}
|
}
|
||||||
|
|
@ -117,8 +74,9 @@ forgit::stash::show() {
|
||||||
# git clean selector
|
# git clean selector
|
||||||
forgit::clean() {
|
forgit::clean() {
|
||||||
forgit::inside_work_tree || return 1
|
forgit::inside_work_tree || return 1
|
||||||
|
local files
|
||||||
# Note: Postfix '/' in directory path should be removed. Otherwise the directory itself will not be removed.
|
# Note: Postfix '/' in directory path should be removed. Otherwise the directory itself will not be removed.
|
||||||
local files=$(git clean -xdfn "$@"| awk '{print $3}'| forgit::fzf -m -0 |sed 's#/$##')
|
files=$(git clean -xdfn "$@"| awk '{print $3}'| forgit::fzf -m -0 |sed 's#/$##')
|
||||||
[[ -n "$files" ]] && echo "$files" |xargs -I{} git clean -xdf {}
|
[[ -n "$files" ]] && echo "$files" |xargs -I{} git clean -xdf {}
|
||||||
echo 'Nothing to clean.'
|
echo 'Nothing to clean.'
|
||||||
}
|
}
|
||||||
|
|
@ -128,16 +86,18 @@ export FORGIT_GI_REPO=~/.forgit/gi/repos/dvcs/gitignore # https://github.com/dvc
|
||||||
|
|
||||||
forgit:ignore() {
|
forgit:ignore() {
|
||||||
[ -d $FORGIT_GI_REPO ] || forgit::ignore::update
|
[ -d $FORGIT_GI_REPO ] || forgit::ignore::update
|
||||||
local preview="cat $FORGIT_GI_REPO/templates/{2}.gitignore 2>/dev/null"
|
local IFS preview args options opt
|
||||||
local IFS=$'\n'
|
IFS=$'\n'
|
||||||
[[ $# -gt 0 ]] && args=($@) ||
|
preview="cat $FORGIT_GI_REPO/templates/{2}.gitignore 2>/dev/null"
|
||||||
args=($(forgit::ignore::list | nl -nrn -w4 -s' ' |
|
# shellcheck disable=SC2206,2207
|
||||||
forgit::fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}'))
|
args=($@) && [[ $# -eq 0 ]] && args=($(forgit::ignore::list | nl -nrn -w4 -s' ' |
|
||||||
test -z "$args" && return 1
|
forgit::fzf -m --preview="$preview" --preview-window="right:70%" | awk '{print $2}'))
|
||||||
local options=('(1) Output to stdout'
|
[ ${#args[@]} -eq 0 ] && return 1
|
||||||
'(2) Append to .gitignore'
|
options=('(1) Output to stdout'
|
||||||
'(3) Overwrite .gitignore')
|
'(2) Append to .gitignore'
|
||||||
|
'(3) Overwrite .gitignore')
|
||||||
opt=$(printf '%s\n' "${options[@]}" |forgit::fzf +m |awk '{print $1}')
|
opt=$(printf '%s\n' "${options[@]}" |forgit::fzf +m |awk '{print $1}')
|
||||||
|
# shellcheck disable=SC2068
|
||||||
case "$opt" in
|
case "$opt" in
|
||||||
'(1)' )
|
'(1)' )
|
||||||
forgit::ignore::get ${args[@]}
|
forgit::ignore::get ${args[@]}
|
||||||
|
|
@ -170,25 +130,37 @@ forgit::ignore::get() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
forgit::ignore::list() {
|
forgit::ignore::list() {
|
||||||
(for item in $FORGIT_GI_REPO/templates/*.gitignore; do
|
find $FORGIT_GI_REPO/templates/*.gitignore -print |sed -e 's#.gitignore$##' -e 's#.*/##' | sort -fu
|
||||||
echo "${item##*/}"
|
|
||||||
done) | sed 's/.gitignore$//' | sort -f -u
|
|
||||||
}
|
}
|
||||||
forgit::ignore::clean() {
|
forgit::ignore::clean() {
|
||||||
setopt localoptions rmstarsilent
|
setopt localoptions rmstarsilent
|
||||||
[[ -d $FORGIT_GI_REPO ]] && rm -rf $FORGIT_GI_REPO
|
[[ -d $FORGIT_GI_REPO ]] && rm -rf $FORGIT_GI_REPO
|
||||||
}
|
}
|
||||||
|
|
||||||
forgit::warn() { printf "%b[Warn]%b %s\n" '\e[0;33m' '\e[0m' "$@" >&2; }
|
forgit::fzf() {
|
||||||
forgit::info() { printf "%b[Info]%b %s\n" '\e[0;32m' '\e[0m' "$@" >&2; }
|
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 "$@"
|
||||||
|
}
|
||||||
|
|
||||||
# add aliases
|
# register aliases
|
||||||
|
# shellcheck disable=SC2139
|
||||||
if [[ -z "$FORGIT_NO_ALIASES" ]]; then
|
if [[ -z "$FORGIT_NO_ALIASES" ]]; then
|
||||||
alias ${forgit_add:-ga}='forgit::add'
|
alias "${forgit_add:-ga}"='forgit::add'
|
||||||
alias ${forgit_log:-glo}='forgit::log'
|
alias "${forgit_log:-glo}"='forgit::log'
|
||||||
alias ${forgit_diff:-gd}='forgit::diff'
|
alias "${forgit_diff:-gd}"='forgit::diff'
|
||||||
alias ${forgit_ignore:-gi}='forgit:ignore'
|
alias "${forgit_ignore:-gi}"='forgit:ignore'
|
||||||
alias ${forgit_restore:-gcf}='forgit::restore'
|
alias "${forgit_restore:-gcf}"='forgit::restore'
|
||||||
alias ${forgit_clean:-gclean}='forgit::clean'
|
alias "${forgit_clean:-gclean}"='forgit::clean'
|
||||||
alias ${forgit_stash_show:-gss}='forgit::stash::show'
|
alias "${forgit_stash_show:-gss}"='forgit::stash::show'
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue