Skip to content

Commit

Permalink
[Search git status] preview changed files (#237)
Browse files Browse the repository at this point in the history
Show a preview of the unstaged and staged changes of the hovered-over file. This will make it easier to identify which files to select and perhaps offer a faster-to-navigate alternative to git diff.
  • Loading branch information
NextAlone authored Jun 13, 2022
1 parent 4ebf002 commit 6d8e962
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Use `fzf.fish` to interactively find and insert the shell entities listed below

- **Search input:** the current repository's `git status`
- **Key binding and mnemonic:** <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>S</kbd> (`S` for status)
- **Preview window:** the unstaged and staged changes in the file

### Commit hashes

Expand Down
19 changes: 19 additions & 0 deletions functions/_fzf_preview_changed_file.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# helper for _fzf_search_git_status
# arg should be a line from git status --short, something like...
# MM functions/_fzf_preview_changed_file.fish
function _fzf_preview_changed_file
set -l path (string split ' ' $argv)[-1]
if string match -r '^\?\?' $argv --quiet
echo -e (set_color --underline)=== Untracked ===\n
_fzf_preview_file $path
else
if string match -r '\S\s\S' $argv --quiet
echo -e (set_color --underline red)=== Unstaged ===\n
git diff --color=always -- $path
end
if string match -r '^\S' $argv --quiet
echo -e (set_color --underline green)=== Staged ===\n
git diff --color=always --staged -- $path
end
end
end
2 changes: 1 addition & 1 deletion functions/_fzf_preview_file.fish
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# helper function for _fzf_search_directory
# helper function for _fzf_search_directory and _fzf_search_git_status
function _fzf_preview_file --description "Print a preview for the given file based on its file type."
# because there's no way to guarantee that _fzf_search_directory passes the path to _fzf_preview_file
# as one argument, we collect all the arguments into one single variable and treat that as the path
Expand Down
1 change: 1 addition & 0 deletions functions/_fzf_search_git_status.fish
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function _fzf_search_git_status --description "Search the output of git status.
_fzf_wrapper --ansi \
--multi \
--query=(commandline --current-token) \
--preview='_fzf_preview_changed_file {}' \
$fzf_git_status_opts
)
if test $status -eq 0
Expand Down

0 comments on commit 6d8e962

Please sign in to comment.