diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-05-22 13:53:19 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-05-22 13:53:27 +0200 |
| commit | e8a206ff279bee464b4ef96a5e0827f3bf39cb4e (patch) | |
| tree | b178a6ac2f5f2e599fc23c43415d062233214fee /.config/bash/prompt | |
| parent | c5661a3b80d0bb6cbee5f817b6ce636eb9deb208 (diff) | |
| download | dotfiles-e8a206ff279bee464b4ef96a5e0827f3bf39cb4e.tar.gz dotfiles-e8a206ff279bee464b4ef96a5e0827f3bf39cb4e.zip | |
misc(bash): show git branch in prompt
Diffstat (limited to '.config/bash/prompt')
| -rw-r--r-- | .config/bash/prompt | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/.config/bash/prompt b/.config/bash/prompt index 13316c3..82cdccf 100644 --- a/.config/bash/prompt +++ b/.config/bash/prompt @@ -25,16 +25,17 @@ function prompt_jobs() { ((job_count > 0)) && printf '%s:%s ' $'' "$job_count" } -# Diverging commits vs the upstream branch — empty when in sync, no upstream, or outside a repo +# Current branch (short SHA if detached) plus divergence vs upstream — empty outside a repo function prompt_git() { - local ahead behind - read -r ahead behind < <(git rev-list --left-right --count "HEAD...@{u}" 2>/dev/null) || return - ((ahead == 0 && behind == 0)) && return local green=$'\001\e[01;32m\002' red=$'\001\e[01;31m\002' reset=$'\001\e[00m\002' - local out= - ((ahead > 0)) && out+="${green}↑${ahead}${reset}" - ((behind > 0)) && out+="${red}↓${behind}${reset}" - printf '%s ' "$out" + local branch ahead behind diverge= + branch=$(git symbolic-ref --short HEAD 2>/dev/null) || branch=$(git rev-parse --short HEAD 2>/dev/null) || return + branch=$green'('$branch')'$reset + if read -r ahead behind < <(git rev-list --left-right --count "HEAD...@{u}" 2>/dev/null); then + ((ahead > 0)) && diverge+="${green}↑${ahead}${reset}" + ((behind > 0)) && diverge+="${red}↓${behind}${reset}" + fi + printf '%s%s ' "$branch" "${diverge:+ $diverge}" } PS1=${colors[bold_green]}'[\u@\h'${colors[bold_white]}' \W $(prompt_git)$(prompt_shlvl)$(prompt_jobs)${colors[bold_green]}]\$'${colors[reset]}' ' |
