Skip to content

Commit

Permalink
scripts: bump fzf scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyinzuo committed Mar 30, 2024
1 parent 35585e0 commit 940b4b1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
1 change: 1 addition & 0 deletions install/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fzf通过vim插件安装最新版
2 changes: 1 addition & 1 deletion install/setup-ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set -v
# Leaderf needs python3-dev and python3-distutils
# wamerican: American English字典文件,安装后位于/usr/share/dict/american-english, 用于vim dictionary
# wordnet: nvim cmp dictionary 可以用wordnet解释单词
apt-get install -y libgtk-3-dev libxt-dev vim-gtk3 ripgrep fzf fd-find tree bat nodejs npm git sqlformat vifm python3-dev python3-distutils wamerican wordnet
apt-get install -y libgtk-3-dev libxt-dev vim-gtk3 ripgrep fd-find tree bat nodejs npm git sqlformat vifm python3-dev python3-distutils wamerican wordnet

# ripgrep-all(master分支)
# See: https://github.com/phiresky/ripgrep-all/issues/113
Expand Down
91 changes: 68 additions & 23 deletions root/fzf/fzf-git.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)
#
# Copyright (c) 2022 Junegunn Choi
# Copyright (c) 2024 Junegunn Choi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +20,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# shellcheck disable=SC2039
[[ $0 = - ]] && return

if [[ $# -eq 1 ]]; then
branches() {
git branch "$@" --sort=-committerdate --sort=-HEAD --format=$'%(HEAD) %(color:yellow)%(refname:short) %(color:green)(%(committerdate:relative))\t%(color:blue)%(subject)%(color:reset)' --color=always | column -ts$'\t'
Expand All @@ -30,6 +33,9 @@ if [[ $# -eq 1 ]]; then
sed 's#^refs/remotes/#\x1b[95mremote-branch\t\x1b[33m#; s#^refs/heads/#\x1b[92mbranch\t\x1b[33m#; s#^refs/tags/#\x1b[96mtag\t\x1b[33m#; s#refs/stash#\x1b[91mstash\t\x1b[33mrefs/stash#' |
column -ts$'\t'
}
hashes() {
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always "$@"
}
case "$1" in
branches)
echo $'CTRL-O (open in browser) ╱ ALT-A (show all branches)\n'
Expand All @@ -39,6 +45,14 @@ if [[ $# -eq 1 ]]; then
echo $'CTRL-O (open in browser)\n'
branches -a
;;
hashes)
echo $'CTRL-O (open in browser) ╱ CTRL-D (diff)\nCTRL-S (toggle sort) ╱ ALT-A (show all hashes)\n'
hashes
;;
all-hashes)
echo $'CTRL-O (open in browser) ╱ CTRL-D (diff)\nCTRL-S (toggle sort)\n'
hashes --all
;;
refs)
echo $'CTRL-O (open in browser) ╱ ALT-E (examine in editor) ╱ ALT-A (show all refs)\n'
refs 'grep -v ^refs/remotes'
Expand Down Expand Up @@ -104,7 +118,8 @@ if [[ $- =~ i ]]; then
_fzf_git_fzf() {
fzf-tmux -p80%,60% -- \
--layout=reverse --multi --height=50% --min-height=20 --border \
--color='header:italic:underline' \
--border-label-pos=2 \
--color='header:italic:underline,label:blue' \
--preview-window='right,50%,border-left' \
--bind='ctrl-/:change-preview-window(down,50%,border-top|hidden|)' "$@"
}
Expand Down Expand Up @@ -132,13 +147,18 @@ fi

_fzf_git_files() {
_fzf_git_check || return
(git -c color.status=always status --short
git ls-files | grep -vxFf <(git status -s | grep '^[^?]' | cut -c4-; echo :) | sed 's/^/ /') |
local root query
root=$(git rev-parse --show-toplevel)
[[ $root != "$PWD" ]] && query='!../ '

(git -c color.status=always status --short --no-branch
git ls-files "$root" | grep -vxFf <(git status -s | grep '^[^?]' | cut -c4-; echo :) | sed 's/^/ /') |
_fzf_git_fzf -m --ansi --nth 2..,.. \
--prompt '📁 Files> ' \
--border-label '📁 Files' \
--header $'CTRL-O (open in browser) ╱ ALT-E (open in editor)\n\n' \
--bind "ctrl-o:execute-silent:bash $__fzf_git file {-1}" \
--bind "alt-e:execute:${EDITOR:-vim} {-1} > /dev/tty" \
--query "$query" \
--preview "git diff --no-ext-diff --color=always -- {-1} | sed 1,4d; $_fzf_git_cat {-1}" "$@" |
cut -c4- | sed 's/.* -> //'
}
Expand All @@ -147,37 +167,38 @@ _fzf_git_branches() {
_fzf_git_check || return
bash "$__fzf_git" branches |
_fzf_git_fzf --ansi \
--prompt '🌲 Branches> ' \
--border-label '🌲 Branches' \
--header-lines 2 \
--tiebreak begin \
--preview-window down,border-top,40% \
--color hl:underline,hl+:underline \
--no-hscroll \
--bind 'ctrl-/:change-preview-window(down,70%|hidden|)' \
--bind "ctrl-o:execute-silent:bash $__fzf_git branch {}" \
--bind "alt-a:change-prompt(🌳 All branches> )+reload:bash \"$__fzf_git\" all-branches" \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1)' "$@" |
--bind "alt-a:change-border-label(🌳 All branches)+reload:bash \"$__fzf_git\" all-branches" \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) --' "$@" |
sed 's/^..//' | cut -d' ' -f1
}

_fzf_git_tags() {
_fzf_git_check || return
git tag --sort -version:refname |
_fzf_git_fzf --preview-window right,70% \
--prompt '📛 Tags> ' \
--border-label '📛 Tags' \
--header $'CTRL-O (open in browser)\n\n' \
--bind "ctrl-o:execute-silent:bash $__fzf_git tag {}" \
--preview 'git show --color=always {}' "$@"
}

_fzf_git_hashes() {
_fzf_git_check || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
bash "$__fzf_git" hashes |
_fzf_git_fzf --ansi --no-sort --bind 'ctrl-s:toggle-sort' \
--prompt '🍡 Hashes> ' \
--header $'CTRL-O (open in browser) ╱ CTRL-D (diff) ╱ CTRL-S (toggle sort)\n\n' \
--border-label '🍡 Hashes' \
--header-lines 3 \
--bind "ctrl-o:execute-silent:bash $__fzf_git commit {}" \
--bind 'ctrl-d:execute:grep -o "[a-f0-9]\{7,\}" <<< {} | head -n 1 | xargs git diff > /dev/tty' \
--bind "alt-a:change-border-label(🍇 All hashes)+reload:bash \"$__fzf_git\" all-hashes" \
--color hl:underline,hl+:underline \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | head -n 1 | xargs git show --color=always' "$@" |
awk 'match($0, /[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]*/) { print substr($0, RSTART, RLENGTH) }'
Expand All @@ -187,42 +208,64 @@ _fzf_git_remotes() {
_fzf_git_check || return
git remote -v | awk '{print $1 "\t" $2}' | uniq |
_fzf_git_fzf --tac \
--prompt '📡 Remotes> ' \
--border-label '📡 Remotes' \
--header $'CTRL-O (open in browser)\n\n' \
--bind "ctrl-o:execute-silent:bash $__fzf_git remote {1}" \
--preview-window right,70% \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {1}/"$(git rev-parse --abbrev-ref HEAD)"' "$@" |
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {1}/"$(git rev-parse --abbrev-ref HEAD)" --' "$@" |
cut -d$'\t' -f1
}

_fzf_git_stashes() {
_fzf_git_check || return
git stash list | _fzf_git_fzf \
--prompt '🥡 Stashes> ' \
--border-label '🥡 Stashes' \
--header $'CTRL-X (drop stash)\n\n' \
--bind 'ctrl-x:execute-silent(git stash drop {1})+reload(git stash list)' \
--bind 'ctrl-x:reload(git stash drop -q {1}; git stash list)' \
-d: --preview 'git show --color=always {1}' "$@" |
cut -d: -f1
}

_fzf_git_lreflogs() {
_fzf_git_check || return
git reflog --color=always --format="%C(blue)%gD %C(yellow)%h%C(auto)%d %gs" | _fzf_git_fzf --ansi \
--border-label '📒 Reflogs' \
--preview 'git show --color=always {1}' "$@" |
awk '{print $1}'
}

_fzf_git_each_ref() {
_fzf_git_check || return
bash "$__fzf_git" refs | _fzf_git_fzf --ansi \
--nth 2,2.. \
--tiebreak begin \
--prompt '☘️ Each ref> ' \
--border-label '☘️ Each ref' \
--header-lines 2 \
--preview-window down,border-top,40% \
--color hl:underline,hl+:underline \
--no-hscroll \
--bind 'ctrl-/:change-preview-window(down,70%|hidden|)' \
--bind "ctrl-o:execute-silent:bash $__fzf_git {1} {2}" \
--bind "alt-e:execute:${EDITOR:-vim} <(git show {2}) > /dev/tty" \
--bind "alt-a:change-prompt(🍀 Every ref> )+reload:bash \"$__fzf_git\" all-refs" \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {2}' "$@" |
--bind "alt-a:change-border-label(🍀 Every ref)+reload:bash \"$__fzf_git\" all-refs" \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {2} --' "$@" |
awk '{print $2}'
}

_fzf_git_worktrees() {
_fzf_git_check || return
git worktree list | _fzf_git_fzf \
--border-label '🌴 Worktrees' \
--header $'CTRL-X (remove worktree)\n\n' \
--bind 'ctrl-x:reload(git worktree remove {1} > /dev/null; git worktree list)' \
--preview '
git -c color.status=always -C {1} status --short --branch
echo
git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {2} --
' "$@" |
awk '{print $1}'
}

if [[ -n "${BASH_VERSION:-}" ]]; then
__fzf_git_init() {
bind '"\er": redraw-current-line'
Expand All @@ -241,16 +284,18 @@ elif [[ -n "${ZSH_VERSION:-}" ]]; then
}

__fzf_git_init() {
local o
local m o
for o in "$@"; do
eval "fzf-git-$o-widget() { local result=\$(_fzf_git_$o | __fzf_git_join); zle reset-prompt; LBUFFER+=\$result }"
eval "zle -N fzf-git-$o-widget"
eval "bindkey '^g^${o[1]}' fzf-git-$o-widget"
eval "bindkey '^g${o[1]}' fzf-git-$o-widget"
for m in emacs vicmd viins; do
eval "bindkey -M $m '^g^${o[1]}' fzf-git-$o-widget"
eval "bindkey -M $m '^g${o[1]}' fzf-git-$o-widget"
done
done
}
fi
__fzf_git_init files branches tags remotes hashes stashes each_ref
__fzf_git_init files branches tags remotes hashes stashes lreflogs each_ref worktrees

# -----------------------------------------------------------------------------
fi
24 changes: 16 additions & 8 deletions root/fzf/fzf.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

export PATH=$PATH:$HOME/plugged/fzf/bin
# Extended-search-mode
#
# 'wild exact-match
Expand Down Expand Up @@ -62,25 +63,32 @@ _fzf_comprun() {
# alias "bat=batcat" in Ubuntu

# https://github.com/junegunn/fzf/blob/master/ADVANCED.md#using-fzf-as-interactive-ripgrep-launcher
# Require fzf 0.45.0+
rfv() {
# 1. Search for text in files using Ripgrep
# 2. Interactively restart Ripgrep with reload action
# 3. Open the file in Vim
# Switch between Ripgrep mode and fzf filtering mode (CTRL-T)
rm -f /tmp/rg-fzf-{r,f}
RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
IFS=: read -ra selected < <(
FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \
fzf --ansi \
--disabled --query "$INITIAL_QUERY" \
: | fzf --ansi --disabled --query "$INITIAL_QUERY" \
--bind "start:reload:$RG_PREFIX {q}" \
--bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
--bind 'ctrl-t:transform:[[ ! $FZF_PROMPT =~ ripgrep ]] &&
echo "rebind(change)+change-prompt(1. ripgrep> )+disable-search+transform-query:echo \{q} > /tmp/rg-fzf-f; cat /tmp/rg-fzf-r" ||
echo "unbind(change)+change-prompt(2. fzf> )+enable-search+transform-query:echo \{q} > /tmp/rg-fzf-r; cat /tmp/rg-fzf-f"' \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--prompt '1. ripgrep> ' \
--delimiter : \
--preview 'batcat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)
[ -n "${selected[0]}" ] && vim "${selected[0]}" "+${selected[1]}"
--header 'CTRL-T: Switch between ripgrep/fzf' \
--preview 'bat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
--bind 'enter:become(vim {1} +{2})'
}

rffv() {
# TODO: update it
# search file contains <file path>:<line number>:
# which can be generated by
# 1) rg --vimgrep
Expand Down

0 comments on commit 940b4b1

Please sign in to comment.