Skip to content

Instantly share code, notes, and snippets.

@ebdrummond
Created February 21, 2023 12:19
Show Gist options
  • Save ebdrummond/81d6ff7a5b294ecaf7d95a19c933d518 to your computer and use it in GitHub Desktop.
Save ebdrummond/81d6ff7a5b294ecaf7d95a19c933d518 to your computer and use it in GitHub Desktop.
Show git branch name and dirty state in terminal
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