Last active
January 26, 2025 13:09
-
-
Save jdharmon/7650afac92d70dd8facdf1ffda3f71a5 to your computer and use it in GitHub Desktop.
Oh My Zsh theme that mimics the behavior of posh-git
This file contains hidden or 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
# Colors | |
local RED="%F{red}" | |
local GREEN="%F{green}" | |
local BLUE="%F{blue}" | |
local YELLOW="%F{yellow}" | |
local MAGENTA="%F{magenta}" | |
local CYAN="%F{cyan}" | |
local RESET="%f" | |
# Symbols | |
local GIT_CLEAN="≡" | |
local GIT_BEHIND="↓" | |
local GIT_AHEAD="↑" | |
# Function to get the Git status | |
git_prompt_info() { | |
# Check if we're in a Git repository | |
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then | |
# Get the current branch name | |
local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
[[ "$branch" == "HEAD" ]] && branch="(detached)" | |
# Get the status of the working directory | |
local git_status=$(git status --short --branch 2>/dev/null) | |
if [[ "$git_status" == *...* ]]; then | |
remoteStatus=" " | |
if [[ "$git_status" =~ "ahead ([0-9]+)" ]]; then | |
remoteStatus+="${GIT_AHEAD}${match[1]}" | |
fi | |
if [[ "$git_status" =~ "behind ([0-9]+)" ]]; then | |
remoteStatus+="${GIT_BEHIND}${match[1]}" | |
fi | |
if [[ "$remoteStatus" = " " ]]; then | |
remoteStatus+="${GIT_CLEAN}" | |
fi | |
else | |
remoteStatus="" | |
fi | |
# Set the color of the branch name based on the status | |
if [[ "$remoteStatus" == *"${GIT_AHEAD}"* ]] && [[ "$remoteStatus" == *"${GIT_BEHIND}"* ]]; then | |
branch="${YELLOW}${branch}" | |
elif [[ "$remoteStatus" == *"${GIT_AHEAD}"* ]]; then | |
branch="${GREEN}${branch}" | |
elif [[ "$remoteStatus" == *"${GIT_BEHIND}"* ]]; then | |
branch="${RED}${branch}" | |
else | |
branch="${CYAN}${branch}" | |
fi | |
# Check for file changes and add symbols | |
local stagedChanges=$(echo "$git_status" | grep -E '^M' | wc -l | tr -d ' ') | |
local unstagedChanges=$(echo "$git_status" | grep -E '^ M' | wc -l | tr -d ' ') | |
local stagedAdds=$(echo "$git_status" | grep -E '^A' | wc -l | tr -d ' ') | |
local unstagedAdds=$(echo "$git_status" | grep -E '^\?\?' | wc -l | tr -d ' ') | |
local stagedDeletes=$(echo "$git_status" | grep -E '^D' | wc -l | tr -d ' ') | |
local unstagedDeletes=$(echo "$git_status" | grep -E '^ D' | wc -l | tr -d ' ') | |
if [[ "$stagedChanges" -ne 0 || "$stagedAdds" -ne 0 || "$stagedDeletes" -ne 0 ]]; then | |
staged=" ${GREEN}~${stagedChanges} +${stagedAdds} -${stagedDeletes}" | |
fi | |
if [[ "$unstagedChanges" -ne 0 || "$unstagedAdds" -ne 0 || "$unstagedDeletes" -ne 0 ]]; then | |
unstaged=" ${RED}~${unstagedChanges} +${unstagedAdds} -${unstagedDeletes}" | |
fi | |
if [[ -n "$staged" && -n "$unstaged" ]]; then | |
separator="${YELLOW} |" | |
fi | |
echo -n " ${YELLOW}[${branch}${remoteStatus}${staged}${separator}${unstaged}${YELLOW}]" | |
fi | |
} | |
# Prompt definition | |
PROMPT='${GREEN}%n@%m${RESET}:${BLUE}%~$(git_prompt_info)${RESET}%# ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment