fish: Refactor git prompt to a separate function
This commit is contained in:
parent
ec79200972
commit
fdb3fff250
2 changed files with 44 additions and 41 deletions
43
fish/.config/fish/functions/fish_custom_git_prompt.fish
Normal file
43
fish/.config/fish/functions/fish_custom_git_prompt.fish
Normal 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
|
|
@ -31,47 +31,7 @@ function fish_prompt
|
|||
end
|
||||
echo -n ' '
|
||||
|
||||
# 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
|
||||
fish_custom_git_prompt
|
||||
|
||||
if [ "$command_status" -eq 0 ]
|
||||
set_color brcyan
|
||||
|
|
Loading…
Reference in a new issue