home-config/fish/functions/_fzf_search_git_status.fish
ca201bd9f1ce2bc1e6b5eb5107bf3ffc764417b4
· 1.8 KB · 41 lines
raw
| 1 | function _fzf_search_git_status --description "Search the output of git status. Replace the current token with the selected file paths." |
| 2 | if not git rev-parse --git-dir >/dev/null 2>&1 |
| 3 | echo '_fzf_search_git_status: Not in a git repository.' >&2 |
| 4 | else |
| 5 | set -f preview_cmd '_fzf_preview_changed_file {}' |
| 6 | if set --query fzf_diff_highlighter |
| 7 | set preview_cmd "$preview_cmd | $fzf_diff_highlighter" |
| 8 | end |
| 9 | |
| 10 | set -f selected_paths ( |
| 11 | # Pass configuration color.status=always to force status to use colors even though output is sent to a pipe |
| 12 | git -c color.status=always status --short | |
| 13 | _fzf_wrapper --ansi \ |
| 14 | --multi \ |
| 15 | --prompt="Git Status> " \ |
| 16 | --query=(commandline --current-token) \ |
| 17 | --preview=$preview_cmd \ |
| 18 | --nth="2.." \ |
| 19 | $fzf_git_status_opts |
| 20 | ) |
| 21 | if test $status -eq 0 |
| 22 | # git status --short automatically escapes the paths of most files for us so not going to bother trying to handle |
| 23 | # the few edges cases of weird file names that should be extremely rare (e.g. "this;needs;escaping") |
| 24 | set -f cleaned_paths |
| 25 | |
| 26 | for path in $selected_paths |
| 27 | if test (string sub --length 1 $path) = R |
| 28 | # path has been renamed and looks like "R LICENSE -> LICENSE.md" |
| 29 | # extract the path to use from after the arrow |
| 30 | set --append cleaned_paths (string split -- "-> " $path)[-1] |
| 31 | else |
| 32 | set --append cleaned_paths (string sub --start=4 $path) |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | commandline --current-token --replace -- (string join ' ' $cleaned_paths) |
| 37 | end |
| 38 | end |
| 39 | |
| 40 | commandline --function repaint |
| 41 | end |