From e7817188aa7cf96045e62e2512b28dfb0ab5b08f Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Mon, 30 Apr 2018 10:48:09 +0800 Subject: [PATCH] Refactor Former-commit-id: a05721e8b96a4a26c73a3922dfed35fa4de678f8 --- README.md | 22 ++++++++++-------- forgit.plugin.zsh | 59 ++++++++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index ee6a029..09c769f 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Clone this repo somewhere and source the `forgit.plugin.zsh` at ~/.zshrc. ### ga -Interactive `git add` +Interactive `git add` selector ![screenshot](screenshot-ga.png) @@ -47,7 +47,7 @@ Interactive `git add` ### glo -Interactive `git log` +Interactive `git log` browser ![screenshot](screenshot-glo.png) @@ -61,7 +61,7 @@ Interactive `git log` ### gd -Interactive `git diff` +Interactive `git diff` browser | Keybind | Action | | ---------------- | --------------------- | @@ -77,15 +77,17 @@ Interactive `.gitignore` generator ![screenshot](screenshot-gi.png) -### Custom aliases +## Custom aliases -You can redefine any of the default aliases by redefining any of the variables below before sourcing the plugin. -The default values for the alias are shown. +You can change the default aliases by redefining these variables below. - forgit_log_alias=glo - forgit_diff_alias=gd - forgit_add_alias=ga - forgit_ignore_alias=gi +``` bash +# Put them before sourcing the plugin if you don't use any plugin manager. +forgit_log=glo +forgit_diff=gd +forgit_add=ga +forgit_ignore=gi +``` ### Tips diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index cc75867..c73722f 100644 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -1,4 +1,4 @@ -wfxr::fzf() { +forgit::fzf() { fzf --border \ --ansi \ --cycle \ @@ -18,61 +18,51 @@ wfxr::fzf() { # diff is fancy with diff-so-fancy! (( $+commands[diff-so-fancy] )) && fancy='|diff-so-fancy' -(( $+commands[emojify] )) && emojify='|emojify' +(( $+commands[emojify] )) && emojify='|emojify' -# Set default aliases if not defined -[ ! -z $forgit_log_alias ] || forgit_log_alias=glo -[ ! -z $forgit_diff_alias ] || forgit_diff_alias=gd -[ ! -z $forgit_add_alias ] || forgit_add_alias=ga -[ ! -z $forgit_ignore_alias ] || forgit_ignore_alias=gi - -unalias $forgit_log_alias $forgit_diff_alias $forgit_add_alias $forgit_ignore_alias &>/dev/null - -inside_work_tree() { +forgit::inside_work_tree() { git rev-parse --is-inside-work-tree >/dev/null } + # git commit browser -forgit_log() { - inside_work_tree || return 1 +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 % $emojify $fancy" eval "git log --graph --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' $@ $emojify" | - wfxr::fzf -e +s --tiebreak=index \ + forgit::fzf -e +s --tiebreak=index \ --bind="enter:execute($cmd |LESS='-R' less)" \ --preview="$cmd" } -alias $forgit_log_alias=forgit_log # git diff brower -forgit_diff() { - inside_work_tree || return 1 +forgit::diff() { + forgit::inside_work_tree || return 1 local cmd="git diff --color=always -- {} $emojify $fancy" git ls-files --modified $(git rev-parse --show-toplevel)| - wfxr::fzf -e -0 \ + forgit::fzf -e -0 \ --bind="enter:execute($cmd |LESS='-R' less)" \ --preview="$cmd" } -alias $forgit_diff_alias=forgit_diff # git add selector -forgit_add() { - inside_work_tree || return 1 +forgit::add() { + forgit::inside_work_tree || return 1 # '31m' matches red color to filter out added files which is all green local files=$(git -c color.status=always status --short | grep 31m | awk '{printf "[%10s] ", $1; $1=""; print $0}' | - wfxr::fzf -e -0 -m --nth 2..,.. \ + forgit::fzf -e -0 -m --nth 2..,.. \ --preview="git diff --color=always -- {-1} $emojify $fancy" | cut -d] -f2 | sed 's/.* -> //') # for rename case [[ -n $files ]] && echo $files |xargs -I{} git add {} && git status } -alias $forgit_add_alias=forgit_add # git ignore generator export giCache=~/.giCache export giIndex=$giCache/.list -forgit_ignore() { - [ -f $giIndex ] || gi-update-index +forgit:ignore() { + [ -f $giIndex ] || forgit::ignore::update local preview="echo {} |awk '{print \$2}' |xargs -I% bash -c 'cat $giCache/% 2>/dev/null || (curl -sL https://www.gitignore.io/api/% |tee $giCache/%)'" IFS=$'\n' [[ $# -gt 0 ]] && args=($@) || args=($(cat $giIndex |nl -nrn -w4 -s' ' |fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}')) @@ -84,26 +74,31 @@ forgit_ignore() { opt=$(printf '%s\n' "${options[@]}" |fzf |awk '{print $1}') case "$opt" in '(1)' ) - gi-get ${args[@]} + forgit::ignore::get ${args[@]} ;; '(2)' ) - gi-get ${args[@]} >> .gitignore + forgit::ignore::get ${args[@]} >> .gitignore ;; '(3)' ) - gi-get ${args[@]} > .gitignore + forgit::ignore::get ${args[@]} > .gitignore ;; esac } -gi-update-index() { +forgit::ignore::update() { mkdir -p $giCache curl -sL https://www.gitignore.io/api/list |tr ',' '\n' > $giIndex } -gi-get() { +forgit::ignore::get() { mkdir -p $giCache echo $@ |xargs -I{} bash -c "cat $giCache/{} 2>/dev/null || (curl -sL https://www.gitignore.io/api/{} |tee $giCache/{})" } -gi-clean() { +forgit::ignore::clean() { setopt localoptions rmstarsilent [[ -d $giCache ]] && rm -rf $giCache/* } -alias $forgit_ignore_alias=forgit_ignore + +# add aliases +alias ${forgit_log:-glo}='forgit::add' +alias ${forgit_log:-glo}='forgit::log' +alias ${forgit_diff:-gd}='forgit::diff' +alias ${forgit_ignore:-gi}='forgit:ignore'