diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-10 14:56:32 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-10 14:56:32 +0200 |
| commit | 9a5790ec7bc284e77dfb0a7c76c43509be1dae9a (patch) | |
| tree | 359bd6c32ad9f4d94b29e1568fafdd5f8155b1d1 | |
| parent | 00b945397e59cb32e0f8ea4bd71ce35b6dc689e8 (diff) | |
| download | dotfiles-9a5790ec7bc284e77dfb0a7c76c43509be1dae9a.tar.gz dotfiles-9a5790ec7bc284e77dfb0a7c76c43509be1dae9a.zip | |
fix(bash): fix and refactor prompt
| -rw-r--r-- | .config/bash/prompt | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/.config/bash/prompt b/.config/bash/prompt index 5c1e762..84cf84d 100644 --- a/.config/bash/prompt +++ b/.config/bash/prompt @@ -2,41 +2,40 @@ # # bash prompt configuration -declare -A colors=( - [bold_black]='\[\e[01;30m\]' - [bold_red]='\[\e[01;31m\]' - [bold_green]='\[\e[01;32m\]' - [bold_yellow]='\[\e[01;33m\]' - [bold_blue]='\[\e[01;34m\]' - [bold_magenta]='\[\e[01;35m\]' - [bold_cyan]='\[\e[01;36m\]' - [bold_white]='\[\e[01;37m\]' - [reset]='\[\e[00m\]' +# Declared global so that functions that run in PS1 can access +declare -gA _PROMPT_COLORS=( + [bold_black]=$'\001\e[01;30m\002' + [bold_red]=$'\001\e[01;31m\002' + [bold_green]=$'\001\e[01;32m\002' + [bold_yellow]=$'\001\e[01;33m\002' + [bold_blue]=$'\001\e[01;34m\002' + [bold_magenta]=$'\001\e[01;35m\002' + [bold_cyan]=$'\001\e[01;36m\002' + [bold_white]=$'\001\e[01;37m\002' + [reset]=$'\001\e[00m\002' ) -function prompt_shlvl() { - ((SHLVL > 1)) && printf '%s:%s ' $'' "$SHLVL" -} +_prompt_shlvl() { ((SHLVL > 1)) && printf '%s:%s ' $'' "$SHLVL"; } -function prompt_jobs() { +_prompt_jobs() { local job_count job_count=$(jobs | wc --lines) ((job_count > 0)) && printf '%s:%s ' $'' "$job_count" } -# Current branch (short SHA if detached) plus divergence vs upstream — empty outside a repo -function prompt_git() { - local green=$'\001\e[01;32m\002' red=$'\001\e[01;31m\002' reset=$'\001\e[00m\002' +_prompt_git() { local branch ahead behind diverge= + # HEAD name (commit SHA in detached HEAD) branch=$(git symbolic-ref --short HEAD 2>/dev/null) || branch=$(git rev-parse --short HEAD 2>/dev/null) || return - branch=$green'('$branch')'$reset + branch="${_PROMPT_COLORS[bold_green]}($branch)${_PROMPT_COLORS[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}" + ((ahead > 0)) && diverge+="${_PROMPT_COLORS[bold_green]}↑${ahead}${_PROMPT_COLORS[reset]}" + ((behind > 0)) && diverge+="${_PROMPT_COLORS[bold_red]}↓${behind}${_PROMPT_COLORS[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[reset]}' ' +# The quoted parts are expanded on each prompt, the unquoted parts immediately +PS1=${_PROMPT_COLORS[bold_green]}'[\u@\h'${_PROMPT_COLORS[bold_white]}' \W $(_prompt_git)$(_prompt_shlvl)$(_prompt_jobs)]\$'${_PROMPT_COLORS[reset]}' ' # vim: ft=bash |
