fish: Refactor git prompt to a separate function

This commit is contained in:
Sanchayan Maity 2024-12-08 21:05:56 +05:30
parent ec79200972
commit fdb3fff250
Signed by: sanchayanmaity
GPG key ID: 6F6A0609C12038F3
2 changed files with 44 additions and 41 deletions

View file

@ -0,0 +1,43 @@
function fish_custom_git_prompt --description "Custom git prompt"
# Show some pretty git information if we're in a git repo
set -l is_git_worktree (git rev-parse --is-inside-work-tree 2>/dev/null)
if test "$is_git_worktree" = true
if git rev-parse '@{u}' >/dev/null 2>&1
# If an upstream is set, check how far ahead/behind the branch is
set -l git_commits_ahead (git rev-list '@{u}..HEAD' | wc -l | awk '{print $1}')
set -l git_commits_behind (git rev-list 'HEAD..@{u}' | wc -l | awk '{print $1}')
if [ \( "$git_commits_ahead" -eq 0 \) -a \( "$git_commits_behind" -eq 0 \) ]
set_color blue
echo -n '⦿'
else
if [ "$git_commits_behind" -gt 0 ]
set_color red
echo -n "$git_commits_behind"
end
if [ "$git_commits_ahead" -gt 0 ]
set_color brred
echo -n "$git_commits_ahead"
end
end
else
# Otherwise, indicate that an upstream is unknown
set_color brred
echo -n "$question_mark_in_circle"
end
set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
if test -n "$git_branch"
set_color blue
echo -n ' ['
# Color the branch name differently if the working tree is dirty
if [ (count (git status --porcelain)) -gt 0 ]
set_color brred
else
set_color yellow
end
echo -n "$git_branch"
set_color blue
echo -n '] '
end
end
end

View file

@ -31,47 +31,7 @@ function fish_prompt
end end
echo -n ' ' echo -n ' '
# Show some pretty git information if we're in a git repo fish_custom_git_prompt
set -l is_git_worktree (git rev-parse --is-inside-work-tree 2>/dev/null)
if test "$is_git_worktree" = true
if git rev-parse '@{u}' >/dev/null 2>&1
# If an upstream is set, check how far ahead/behind the branch is
set -l git_commits_ahead (git rev-list '@{u}..HEAD' | wc -l | awk '{print $1}')
set -l git_commits_behind (git rev-list 'HEAD..@{u}' | wc -l | awk '{print $1}')
if [ \( "$git_commits_ahead" -eq 0 \) -a \( "$git_commits_behind" -eq 0 \) ]
set_color blue
echo -n '⦿'
else
if [ "$git_commits_behind" -gt 0 ]
set_color red
echo -n "$git_commits_behind"
end
if [ "$git_commits_ahead" -gt 0 ]
set_color brred
echo -n "$git_commits_ahead"
end
end
else
# Otherwise, indicate that an upstream is unknown
set_color brred
echo -n "$question_mark_in_circle"
end
set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
if test -n "$git_branch"
set_color blue
echo -n ' ['
# Color the branch name differently if the working tree is dirty
if [ (count (git status --porcelain)) -gt 0 ]
set_color brred
else
set_color yellow
end
echo -n "$git_branch"
set_color blue
echo -n '] '
end
end
if [ "$command_status" -eq 0 ] if [ "$command_status" -eq 0 ]
set_color brcyan set_color brcyan