From 4b006b55c4c3610812353bc36e18f1d2eb59f56d Mon Sep 17 00:00:00 2001 From: MountainMan <43313373+MountainMan1312@users.noreply.github.com> Date: Sun, 2 Apr 2023 21:49:37 -0400 Subject: Add git branch to `$PS1` in `dotfiles/home/.bashrc` It's nice to see which branch you're in at-a-glance. This change makes it so that, if you are in a git repo, the `$PS1` displays which branch is active. If not, it is reduced to a single space. I would like to find a way to conditionally add a space between (BRANCH) and DIR, omitting it if not in a git repo. This would make it look nicer outside of git repos. --- home/.bashrc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/home/.bashrc b/home/.bashrc index 6610b34..6f6711e 100644 --- a/home/.bashrc +++ b/home/.bashrc @@ -16,10 +16,10 @@ fi # conditionally based on what kind of user is logged in. # # Breakdown of PS1 components: -# [TTY#] [DATE TIME] [USER HOST] [DIR] > +# [TTY#] [DATE TIME] [USER HOST] [(BRANCH) DIR] > # # Example of what the prompt should look like: -# [1] [2023-03-28 16:02:55] [user host] [/etc/portage] > +# [1] [2023-03-28 16:02:55] [user host] [(stable) /etc/portage] > ######################################################################### # Set colors for easy reading PS1_COLOR_RESET="\[\e[0m\]" @@ -59,8 +59,13 @@ PS1+="$PS1_COLOR_GREY@$PS1_COLOR_RESET" # @ PS1+="$PS1_COLOR_BLUE\h$PS1_COLOR_RESET" # HOST PS1+="$PS1_COLOR_MAIN] $PS1_COLOR_RESET" # ] -# [DIR] > +# [(BRANCH) DIR] > +parse_git_branch() { + git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' \ + | awk '{$1=$1;print}' +} PS1+="$PS1_COLOR_MAIN[$PS1_COLOR_RESET" # [ +PS1+="\$(parse_git_branch) " # (BRANCH) PS1+="$PS1_COLOR_CYAN\w$PS1_COLOR_RESET" # DIR PS1+="$PS1_COLOR_MAIN] > $PS1_COLOR_RESET" # ] > ######################################################################### -- cgit v1.2.3