Skip to content

Instantly share code, notes, and snippets.

@pwiesner
Last active April 6, 2023 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwiesner/ff06399062a9e211088dae135fc165ab to your computer and use it in GitHub Desktop.
Save pwiesner/ff06399062a9e211088dae135fc165ab to your computer and use it in GitHub Desktop.
# don't forget chmod +x ~/.zsh/git-prompt.zsh
suppress_errors() {
# Syntactic sugar to ignore any errors. I created it because I can never
# remember that 2> represents stderr.
eval "$1" 2>/dev/null
}
git_prompt() {
# Colors lifted from https://www.nordtheme.com/docs/colors-and-palettes
local BRANCH_STYLE="%F{172}" # Aurora Yellow (nord13)
local BEHIND_STYLE="%F{196}" # Aurora Red (nord11)
local AHEAD_STYLE="%F{84}" # Aurora Green (nord14)
local UNSTAGED_STYLE="%F{196}" # Aurora Red (nord11)
local STAGED_STYLE="%F{38}" # Aurora Blue (nord9)
local UNTRACKED_STYLE="%F{141}" # Aurora Purple (nord15)
BRANCH=$(suppress_errors "git symbolic-ref --short HEAD")
# if the length of $BRANCH is not (!) zero (-z) then...
if [ ! -z $BRANCH ]; then
# Exclude any untracked files from status
BEHIND=$(git status --untracked-files=no | grep 'Your branch is behind')
AHEAD=$(git status --untracked-files=no | grep 'Your branch is ahead')
# See https://git-scm.com/docs/git-status#_short_format for more details.
# I think, big question mark on whether or not I am rignt, the following is
# true.
#
# - A leading space indicates that a change has been made, but not yet staged
# - A trailing space indicates that changes has been staged
#
# There is some weirdness with merge conflicts i'm not accounting for, but in
# general we can expect the following from tracked files.
#
# - M (Modified)
# - D (Deleted)
# - R (Renamed)
# - C (Copied)
# - ?? (Untracked)
# Find unstaged changes - note the leading space
UNSTAGED_CHANGES=$(git status --short | grep '^\( M\| D\| R\| C\)')
# Find staged changes - note the trailing space
STAGED_CHANGES=$(git status --short | grep '^\(M \|D \|R \|C \)')
# Find untracked files
UNTRACKED_CHANGES=$(git status --short | grep '^??')
echo -n "${BRANCH_STYLE} [$BRANCH]"
if [ ! -z "$BEHIND" ]; then
echo -n " ${BEHIND_STYLE}↓"
fi
if [ ! -z "$AHEAD" ]; then
echo -n " ${AHEAD_STYLE}↑"
fi
if [ ! -z "$UNSTAGED_CHANGES" ]; then
echo -n " ${UNSTAGED_STYLE}✖"
fi
if [ ! -z "$STAGED_CHANGES" ]; then
echo -n " ${STAGED_STYLE}●"
fi
if [ ! -z "$UNTRACKED_CHANGES" ]; then
echo -n " ${UNTRACKED_STYLE}?"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment