Rebuild terlar git prompt as a configuration for fish_git_prompt

This removes the awkward secondary logic.

Note that we still ship a function called `__terlar_git_prompt`
because people who picked the prompt will still be calling it - we
don't update the prompt.
This commit is contained in:
Fabian Homborg 2021-04-05 10:58:40 +02:00 committed by Fabian Boehm
parent 3552a11afa
commit 8af2f96b4f
2 changed files with 69 additions and 96 deletions

View file

@ -1,98 +1,38 @@
set -g fish_color_git_clean green
set -g fish_color_git_staged yellow
set -g fish_color_git_dirty red
set -g fish_color_git_added green
set -g fish_color_git_modified blue
set -g fish_color_git_renamed magenta
set -g fish_color_git_copied magenta
set -g fish_color_git_deleted red
set -g fish_color_git_untracked yellow
set -g fish_color_git_unmerged red
set -g fish_prompt_git_status_added '✚'
set -g fish_prompt_git_status_modified '*'
set -g fish_prompt_git_status_renamed '➜'
set -g fish_prompt_git_status_copied '⇒'
set -g fish_prompt_git_status_deleted '✖'
set -g fish_prompt_git_status_untracked '?'
set -g fish_prompt_git_status_unmerged !
set -g fish_prompt_git_status_order added modified renamed copied deleted untracked unmerged
function __terlar_git_prompt --description 'Write out the git prompt'
# If git isn't installed, there's nothing we can do
# Return 1 so the calling prompt can deal with it
if not command -sq git
return 1
end
set -l branch (git rev-parse --abbrev-ref HEAD 2>/dev/null)
if test -z $branch
return
end
echo -n '|'
# Ignore untracked files unless we're explicitly asked.
# This is dog slow.
set -l untr -uno
set -q __fish_git_prompt_showdirtystate
or set -g __fish_git_prompt_showdirtystate 1
set -q __fish_git_prompt_showuntrackedfiles
and set untr -unormal
or set -g __fish_git_prompt_showuntrackedfiles 1
set -q __fish_git_prompt_showcolorhints
or set -g __fish_git_prompt_showcolorhints 1
set -q __fish_git_prompt_color_untrackedfiles
or set -g __fish_git_prompt_color_untrackedfiles yellow
set -q __fish_git_prompt_char_untrackedfiles
or set -g __fish_git_prompt_char_untrackedfiles '?'
set -q __fish_git_prompt_color_invalidstate
or set -g __fish_git_prompt_color_invalidstate red
set -q __fish_git_prompt_char_invalidstate
or set -g __fish_git_prompt_char_invalidstate '!'
set -q __fish_git_prompt_color_dirtystate
or set -g __fish_git_prompt_color_dirtystate blue
set -q __fish_git_prompt_char_dirtystate
or set -g __fish_git_prompt_char_dirtystate '*'
set -q __fish_git_prompt_char_stagedstate
or set -g __fish_git_prompt_char_stagedstate '✚'
set -q __fish_git_prompt_color_cleanstate
or set -g __fish_git_prompt_color_cleanstate green
set -q __fish_git_prompt_char_cleanstate
or set -g __fish_git_prompt_char_cleanstate '✓'
set -q __fish_git_prompt_color_stagedstate
or set -g __fish_git_prompt_color_stagedstate yellow
set -q __fish_git_prompt_color_branch_dirty
or set -g __fish_git_prompt_color_branch_dirty red
set -q __fish_git_prompt_color_branch_staged
or set -g __fish_git_prompt_color_branch_staged yellow
set -q __fish_git_prompt_color_branch
or set -g __fish_git_prompt_color_branch green
set -q __fish_git_prompt_char_stateseparator
or set -g __fish_git_prompt_char_stateseparator '⚡'
set -l index (git -c core.fsmonitor= status --porcelain 2>/dev/null|cut -c 1-2|sort -u)
if test -z "$index"
set_color $fish_color_git_clean
echo -n $branch'✓'
set_color normal
return
end
set -l gs
set -l staged
for i in $index
if string match -rq '^[AMRCD]' -- $i
set staged 1
end
# HACK: To allow matching a literal `??` both with and without `?` globs.
set -l dq '??'
switch $i
case 'A '
set -a gs added
case 'M ' ' M'
set -a gs modified
case 'R '
set -a gs renamed
case 'C '
set -a gs copied
case 'D ' ' D'
set -a gs deleted
case "$dq"
set -a gs untracked
case 'U*' '*U' DD AA
set -a gs unmerged
end
end
if set -q staged[1]
set_color $fish_color_git_staged
else
set_color $fish_color_git_dirty
end
echo -n $branch'⚡'
for i in $fish_prompt_git_status_order
if contains $i in $gs
set -l color_name fish_color_git_$i
set -l status_name fish_prompt_git_status_$i
set_color $$color_name
echo -n $$status_name
end
end
set_color normal
fish_git_prompt $argv
end

View file

@ -13,8 +13,41 @@ function fish_prompt --description 'Write out the prompt'
echo -n (prompt_pwd)
set_color normal
__terlar_git_prompt
fish_hg_prompt
set -q __fish_git_prompt_showdirtystate
or set -g __fish_git_prompt_showdirtystate 1
set -q __fish_git_prompt_showuntrackedfiles
or set -g __fish_git_prompt_showuntrackedfiles 1
set -q __fish_git_prompt_showcolorhints
or set -g __fish_git_prompt_showcolorhints 1
set -q __fish_git_prompt_color_untrackedfiles
or set -g __fish_git_prompt_color_untrackedfiles yellow
set -q __fish_git_prompt_char_untrackedfiles
or set -g __fish_git_prompt_char_untrackedfiles '?'
set -q __fish_git_prompt_color_invalidstate
or set -g __fish_git_prompt_color_invalidstate red
set -q __fish_git_prompt_char_invalidstate
or set -g __fish_git_prompt_char_invalidstate '!'
set -q __fish_git_prompt_color_dirtystate
or set -g __fish_git_prompt_color_dirtystate blue
set -q __fish_git_prompt_char_dirtystate
or set -g __fish_git_prompt_char_dirtystate '*'
set -q __fish_git_prompt_char_stagedstate
or set -g __fish_git_prompt_char_stagedstate '✚'
set -q __fish_git_prompt_color_cleanstate
or set -g __fish_git_prompt_color_cleanstate green
set -q __fish_git_prompt_char_cleanstate
or set -g __fish_git_prompt_char_cleanstate '✓'
set -q __fish_git_prompt_color_stagedstate
or set -g __fish_git_prompt_color_stagedstate yellow
set -q __fish_git_prompt_color_branch_dirty
or set -g __fish_git_prompt_color_branch_dirty red
set -q __fish_git_prompt_color_branch_staged
or set -g __fish_git_prompt_color_branch_staged yellow
set -q __fish_git_prompt_color_branch
or set -g __fish_git_prompt_color_branch green
set -q __fish_git_prompt_char_stateseparator
or set -g __fish_git_prompt_char_stateseparator '⚡'
fish_vcs_prompt
echo
if not test $last_status -eq 0