Created
February 21, 2023 12:19
-
-
Save ebdrummond/81d6ff7a5b294ecaf7d95a19c933d518 to your computer and use it in GitHub Desktop.
Show git branch name and dirty state in terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parse_git_branch() { | |
git_status="$(git status 2> /dev/null)" | |
pattern="On branch ([^[:space:]]*)" | |
if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then | |
state="*" | |
fi | |
if [[ ${git_status} =~ ${pattern} ]]; then | |
branch=${match[1]} | |
branch_cut=${branch:0:35} | |
if (( ${#branch} > ${#branch_cut} )); then | |
echo "(${branch_cut}...${state})" | |
else | |
echo "(${branch}${state})" | |
fi | |
fi | |
} | |
setopt PROMPT_SUBST | |
PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$ ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment