2024-09-12 14:02:16 +00:00
|
|
|
# Prompt
|
|
|
|
NEWLINE=$'\n'
|
|
|
|
ARROW=$'\UE0B0'
|
|
|
|
setopt prompt_subst
|
|
|
|
function _git_symbols() {
|
|
|
|
# Symbols
|
|
|
|
local ahead='↑'
|
|
|
|
local behind='↓'
|
|
|
|
local diverged='↕'
|
|
|
|
local up_to_date='|'
|
|
|
|
local no_remote=''
|
|
|
|
local staged='+'
|
|
|
|
local untracked='?'
|
|
|
|
local modified='!'
|
|
|
|
local moved='>'
|
|
|
|
local deleted='x'
|
|
|
|
local stashed='$'
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
local output_symbols=''
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
local git_status_v
|
|
|
|
git_status_v="$(git status --porcelain=v2 --branch --show-stash 2>/dev/null)"
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
# Parse branch information
|
|
|
|
local ahead_count behind_count
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
# AHEAD, BEHIND, DIVERGED
|
|
|
|
if echo $git_status_v | grep -q "^# branch.ab " ; then
|
|
|
|
# One line of the git status output looks like this:
|
|
|
|
# # branch.ab +1 -2
|
|
|
|
# In the line below:
|
|
|
|
# - we grep for the line starting with # branch.ab
|
|
|
|
# - we grep for the numbers and output them on separate lines
|
|
|
|
# - we remove the + and - signs
|
|
|
|
# - we put the two numbers into variables, while telling read to use a newline as the delimiter for reading
|
|
|
|
read -d "\n" -r ahead_count behind_count <<< $(echo "$git_status_v" | grep "^# branch.ab" | grep -o -E '[+-][0-9]+' | sed 's/[-+]//')
|
|
|
|
# Show the ahead and behind symbols when relevant
|
|
|
|
[[ $ahead_count != 0 ]] && output_symbols+="$ahead"
|
|
|
|
[[ $behind_count != 0 ]] && output_symbols+="$behind"
|
|
|
|
# Replace the ahead symbol with the diverged symbol when both ahead and behind
|
|
|
|
output_symbols="${output_symbols//$ahead$behind/$diverged}"
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
# If the branch is up to date, show the up to date symbol
|
|
|
|
[[ $ahead_count == 0 && $behind_count == 0 ]] && output_symbols+="$up_to_date"
|
|
|
|
fi
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
# STASHED
|
|
|
|
echo $git_status_v | grep -q "^# stash " && output_symbols+="$stashed"
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
# STAGED
|
|
|
|
[[ $(git diff --name-only --cached) ]] && output_symbols+="$staged"
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
# For the rest of the symbols, we use the v1 format of git status because it's easier to parse.
|
|
|
|
local git_status
|
2024-05-23 16:39:59 +00:00
|
|
|
|
2024-09-12 14:02:16 +00:00
|
|
|
symbols="$(git status --porcelain=v1 | cut -c1-2 | sed 's/ //g')"
|
|
|
|
|
|
|
|
while IFS= read -r symbol; do
|
|
|
|
case $symbol in
|
|
|
|
??) output_symbols+="$untracked";;
|
|
|
|
M) output_symbols+="$modified";;
|
|
|
|
R) output_symbols+="$moved";;
|
|
|
|
D) output_symbols+="$deleted";;
|
|
|
|
esac
|
|
|
|
done <<< "$symbols"
|
|
|
|
|
|
|
|
# Remove duplicate symbols
|
|
|
|
output_symbols="$(echo -n "$output_symbols" | tr -s "$untracked$modified$moved$deleted")"
|
|
|
|
|
|
|
|
[[ -n $output_symbols ]] && echo -n " $output_symbols"
|
|
|
|
}
|
|
|
|
|
|
|
|
function _git_info() {
|
|
|
|
local git_info=''
|
|
|
|
local git_branch_name=''
|
|
|
|
|
|
|
|
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
|
|
git_branch_name="$(git symbolic-ref --short HEAD 2>/dev/null)"
|
|
|
|
if [[ -n "$git_branch_name" ]]; then
|
|
|
|
git_info+="%K{202}%F{52}${ARROW}%f"
|
|
|
|
git_info+="%F{black}%B "
|
|
|
|
git_info+="ש $git_branch_name"
|
|
|
|
fi
|
|
|
|
git_info+="$(_git_symbols)"
|
|
|
|
git_info+="%b%f"
|
|
|
|
git_info+="%k%F{202}${ARROW}%f"
|
|
|
|
echo "$git_info "
|
|
|
|
else
|
|
|
|
git_info+="%K{black}%F{52}${ARROW}%f%k"
|
|
|
|
echo "$git_info "
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set background and foreground for first element
|
|
|
|
PROMPT='%K{214}%F{black}'
|
|
|
|
# Print date and time
|
|
|
|
PROMPT+='${ARROW} %D %T '
|
|
|
|
# Close first element
|
|
|
|
PROMPT+='%f%k'
|
|
|
|
# Set background for second element
|
|
|
|
PROMPT+='%K{52}'
|
|
|
|
# Print next arrow
|
|
|
|
PROMPT+='%F{214}${ARROW}%f'
|
|
|
|
# Print current and parent directory
|
|
|
|
PROMPT+=' %2d '
|
|
|
|
# Close second element
|
|
|
|
PROMPT+='%k'
|
|
|
|
# Git Status
|
|
|
|
PROMPT+='$(_git_info)'
|
|
|
|
# Move to new line
|
|
|
|
PROMPT+='${NEWLINE}'
|
|
|
|
# Start second line
|
|
|
|
PROMPT+=' > '
|