Skip to content

Commit

Permalink
[Search git status] beautify header
Browse files Browse the repository at this point in the history
Put a border around the diff type header created using ASCII art to make it stand out more.
Pass --no-prefix to git diff to slightly shorten the diff header.
  • Loading branch information
PatrickF1 committed Aug 8, 2022
1 parent d9c8568 commit 2bb6f71
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions functions/_fzf_preview_changed_file.fish
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ function _fzf_preview_changed_file --description "Show the untracked, staged, an
_fzf_report_diff_type Untracked
_fzf_preview_file $path
else
# no-prefix because the file is always being compared to itself so is unecessary
set diff_opts --color=always --no-prefix

if test $index_status != ' '
_fzf_report_diff_type Staged
git diff --staged --color=always -- $path
git diff --staged $diff_opts -- $path
end

if test $working_tree_status != ' '
_fzf_report_diff_type Unstaged
git diff --color=always -- $path
git diff $diff_opts -- $path
end
end
end
16 changes: 13 additions & 3 deletions functions/_fzf_report_diff_type.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# helper for _fzf_preview_changed_file
function _fzf_report_diff_type --argument-names diff_type --description "Print a distinct colored header."
set_color --underline yellow
echo -e === $diff_type ===\n
# prints out something like
# +--------+
# | Staged |
# +--------+
function _fzf_report_diff_type --argument-names diff_type --description "Print a distinct colored header meant to preface a git patch."
# number of "-" to draw is the length of the string to box + 2 for padding
set repeat_count (math 2 + (string length $diff_type))
set horizontal_border +(string repeat --count $repeat_count -)+

set_color yellow
echo $horizontal_border
echo "| $diff_type |"
echo $horizontal_border
set_color normal
end
3 changes: 2 additions & 1 deletion tests/preview_changed_file/deleted_in_working.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mock git diff "echo \$argv"
set file dir/file.txt

set output (_fzf_preview_changed_file " D $file")
contains -- "--color=always -- $file" $output && not string match -- "*--staged*" $output

contains -- "| Unstaged |" $output && not contains "| Staged |" $output
@test "only shows unstaged changes if file was only deleted in working tree" $status -eq 0
2 changes: 1 addition & 1 deletion tests/preview_changed_file/modified_in_both.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ mock git diff "echo \$argv"
set file dir/file.txt
set output (_fzf_preview_changed_file "MM $file")

contains -- "--staged --color=always -- $file" $output && contains -- "--color=always -- $file" $output
contains -- "| Unstaged |" $output && contains "| Staged |" $output
@test "shows staged and unstaged changes if the file is modified in both places" $status -eq 0

0 comments on commit 2bb6f71

Please sign in to comment.