From fdb3fff2501d3b21e06171f7958a1b7237b43451 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sun, 8 Dec 2024 21:05:56 +0530 Subject: [PATCH] fish: Refactor git prompt to a separate function --- .../functions/fish_custom_git_prompt.fish | 43 +++++++++++++++++++ fish/.config/fish/functions/fish_prompt.fish | 42 +----------------- 2 files changed, 44 insertions(+), 41 deletions(-) create mode 100644 fish/.config/fish/functions/fish_custom_git_prompt.fish diff --git a/fish/.config/fish/functions/fish_custom_git_prompt.fish b/fish/.config/fish/functions/fish_custom_git_prompt.fish new file mode 100644 index 0000000..bd3b0fe --- /dev/null +++ b/fish/.config/fish/functions/fish_custom_git_prompt.fish @@ -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 diff --git a/fish/.config/fish/functions/fish_prompt.fish b/fish/.config/fish/functions/fish_prompt.fish index 9d1fb16..0f1459e 100644 --- a/fish/.config/fish/functions/fish_prompt.fish +++ b/fish/.config/fish/functions/fish_prompt.fish @@ -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