Support user custom fzf options for forgit (#4)

Former-commit-id: 2dc83917ec
This commit is contained in:
Wenxuan 2018-06-02 23:52:01 +08:00
parent 7978646f71
commit df8e4eab0d
2 changed files with 36 additions and 21 deletions

View file

@ -77,7 +77,7 @@ Interactive `.gitignore` generator
![screenshot](screenshot-gi.png)
## Custom aliases
## Custom options
You can change the default aliases by defining these variables below.
@ -89,10 +89,24 @@ forgit_add=ga
forgit_ignore=gi
```
You can add custom fzf options for `forgit`, including keybinds, layout, etc.
(No need to repeat the options already defined in `FZF_DEFAULT_OPTS`)
``` bash
FORGIT_FZF_DEFAULT_OPTS="
--exact
--border
--cycle
--reverse
--height '80%'
"
```
### Tips
- Hit `q` to Quit from full screen preview any time.
- Install [`diff-so-fancy`](https://github.com/so-fancy/diff-so-fancy) to have better `diff` output.
- Call `glo` with arguments to get logs only related to these files or directories(eg, `glo main.go test.go`).
- Call `gi` with arguments to get wanted `.gitignore` contents directly(eg, `gi c++`).
## [License](LICENSE.txt)

View file

@ -1,19 +1,20 @@
forgit::fzf() {
fzf --border \
--ansi \
--cycle \
--reverse \
--height '80%' \
--bind='alt-v:page-up' \
--bind='ctrl-v:page-down' \
--bind='alt-k:preview-up,alt-p:preview-up' \
--bind='alt-j:preview-down,alt-n:preview-down' \
--bind='alt-a:select-all' \
--bind='ctrl-r:toggle-all' \
--bind='ctrl-s:toggle-sort' \
--bind='?:toggle-preview' \
--preview-window="right:60%" \
--bind='alt-w:toggle-preview-wrap' "$@"
FZF_DEFAULT_OPTS="
$FZF_DEFAULT_OPTS
--ansi
--height '80%'
--bind='alt-v:page-up'
--bind='ctrl-v:page-down'
--bind='alt-k:preview-up,alt-p:preview-up'
--bind='alt-j:preview-down,alt-n:preview-down'
--bind='alt-a:select-all'
--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() {
@ -59,7 +60,7 @@ 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"
eval "git log --graph --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' $@ $forgit_emojify" |
forgit::fzf -e +s --tiebreak=index \
forgit::fzf +s +m --tiebreak=index \
--bind="enter:execute($cmd |LESS='-R' less)" \
--preview="$cmd"
}
@ -69,7 +70,7 @@ forgit::diff() {
forgit::inside_work_tree || return 1
local cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy"
git ls-files --modified $(git rev-parse --show-toplevel)|
forgit::fzf -e -0 \
forgit::fzf +m -0 \
--bind="enter:execute($cmd |LESS='-R' less)" \
--preview="$cmd"
}
@ -86,7 +87,7 @@ forgit::add() {
local files=$(git -c color.status=always status --short |
grep -e "$added" -e "$changed" -e "$unmerged" |
awk '{printf "[%10s] ", $1; $1=""; print $0}' |
forgit::fzf -e -0 -m --nth 2..,.. \
forgit::fzf -0 -m --nth 2..,.. \
--preview="git diff --color=always -- {-1} $forgit_emojify $forgit_fancy" |
cut -d] -f2 |
sed 's/.* -> //') # for rename case
@ -100,13 +101,13 @@ forgit:ignore() {
[ -f $FORGIT_GI_INDEX ] || forgit::ignore::update
local preview="echo {} |awk '{print \$2}' |xargs -I% bash -c 'cat $FORGIT_GI_CACHE/% 2>/dev/null || (curl -sL https://www.gitignore.io/api/% |tee $FORGIT_GI_CACHE/%)'"
IFS=$'\n'
[[ $# -gt 0 ]] && args=($@) || args=($(cat $FORGIT_GI_INDEX |nl -nrn -w4 -s' ' |fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}'))
[[ $# -gt 0 ]] && args=($@) || args=($(cat $FORGIT_GI_INDEX |nl -nrn -w4 -s' ' |forgit::fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}'))
test -z "$args" && return 1
local options=(
'(1) Output to stdout'
'(2) Append to .gitignore'
'(3) Overwrite .gitignore')
opt=$(printf '%s\n' "${options[@]}" |fzf |awk '{print $1}')
opt=$(printf '%s\n' "${options[@]}" |forgit::fzf +m |awk '{print $1}')
case "$opt" in
'(1)' )
forgit::ignore::get ${args[@]}