diff --git a/.context.json b/.context.json index 70d451e..d528057 100644 --- a/.context.json +++ b/.context.json @@ -431,6 +431,7 @@ "On Linux, installer affects all users by writing to /etc/profile.d and sourcing from /etc/bash.bashrc.", "On macOS, installer is per-user and sources from .bashrc/.bash_profile.", "Seasonal palettes and contest pack are now documented directly in main.", - "README uses images/bash-pallete.png." + "README uses images/bash-pallete.png.", + "Git capsule prefers __git_ps1 and falls back to git symbolic-ref/short hash when git-prompt is unavailable." ] } diff --git a/dotfiles/ps1/ps1.sh b/dotfiles/ps1/ps1.sh index d26c798..d6e6bbb 100644 --- a/dotfiles/ps1/ps1.sh +++ b/dotfiles/ps1/ps1.sh @@ -158,39 +158,40 @@ __try_source_git_prompt() { __try_source_git_prompt >/dev/null 2>&1 __git_capsule() { - if ! declare -F __git_ps1 >/dev/null 2>&1; then - return 0 - fi - local info status="" local clean="✨" local dirty="🔨" local staged="📌" local untracked="🧷" local stashed="📦" + local has_ps1=0 + + if declare -F __git_ps1 >/dev/null 2>&1; then + has_ps1=1 + info="$(__git_ps1 "%s" 2>/dev/null)" + else + if command -v git >/dev/null 2>&1; then + info="$(git symbolic-ref --quiet --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)" + fi + fi - info="$(__git_ps1 "%s" 2>/dev/null)" [[ -z "$info" ]] && return 0 - if [[ "$info" == *"*"* ]]; then status+=" $dirty"; fi - if [[ "$info" == *"+"* ]]; then status+=" $staged"; fi - if [[ "$info" == *"%"* ]]; then status+=" $untracked"; fi - if [[ "$info" == *"$"* ]]; then status+=" $stashed"; fi - if [[ -z "$status" ]]; then status=" $clean"; fi + if (( has_ps1 )); then + if [[ "$info" == *"*"* ]]; then status+=" $dirty"; fi + if [[ "$info" == *"+"* ]]; then status+=" $staged"; fi + if [[ "$info" == *"%"* ]]; then status+=" $untracked"; fi + if [[ "$info" == *"$"* ]]; then status+=" $stashed"; fi + if [[ -z "$status" ]]; then status=" $clean"; fi - info="${info//\%/}" - info="${info//\*/}" - info="${info//\+/}" - info="${info//\$/}" - info="$(printf "%s" "$info" | xargs)" - - printf " ⟦%s⟧%s" "$info" "$status" -} - -__git_capsule() { - if declare -F __git_ps1 >/dev/null 2>&1; then - __git_ps1 " (%s)" 2>/dev/null + info="${info//\%/}" + info="${info//\*/}" + info="${info//\+/}" + info="${info//\$/}" fi + + info="$(printf "%s" "$info" | xargs)" + printf " ⟦%s⟧%s" "$info" "$status" } # Time-based emoji with seasonal accents (Europe/Oslo) @@ -637,6 +638,9 @@ ${G_Z2_BG}${G_Z2_FG} ${HOST_PART}${PATH_SEP}${G_PATH_FG}${PATH_PART}${G_END_PAD} local H_RIGHT_FG="\[\e[38;5;31m\]" local H_GIT="" if [[ "$__PS1_GIT" == "1" ]]; then + export GIT_PS1_SHOWDIRTYSTATE=1 + export GIT_PS1_SHOWSTASHSTATE=1 + export GIT_PS1_SHOWUNTRACKEDFILES=1 H_GIT="$(__git_capsule)" fi PS1="\