Files
Modular-Bashrc-Manager/installer/01_git-cli-highlitgh.sh

53 lines
2.0 KiB
Bash

parse_git_branch() {
if git rev-parse --is-inside-work-tree &>/dev/null; then
status_output=$(git status --porcelain --branch 2>/dev/null)
branch=$(echo "$status_output" | head -n1 | sed -E 's/^## ([^ .]+).*/\1/')
ahead=$(echo "$status_output" | grep -o 'ahead [0-9]*' | awk '{print $2}')
behind=$(echo "$status_output" | grep -o 'behind [0-9]*' | awk '{print $2}')
staged=$(echo "$status_output" | grep -E '^[A-Z]' | wc -l)
modified=$(echo "$status_output" | grep -E '^.[A-Z]' | wc -l)
untracked=$(echo "$status_output" | grep -E '^\?\?' | wc -l)
GREEN_BOLD="\[\033[1;32m\]"
PURPLE_BOLD="\[\033[1;35m\]"
YELLOW_BOLD="\[\033[1;33m\]"
RED_BOLD="\[\033[1;31m\]"
CYAN_BOLD="\[\033[1;36m\]"
RESET="\[\033[0m\]"
git_status=""
[ "$staged" -gt 0 ] && git_status="${git_status}${GREEN_BOLD}+${staged}${RESET} "
[ "$modified" -gt 0 ] && git_status="${git_status}${YELLOW_BOLD}M:${modified}${RESET} "
[ "$untracked" -gt 0 ] && git_status="${git_status}${RED_BOLD}N:${untracked}${RESET} "
# Remote State
remote_status=""
[ -n "$ahead" ] && remote_status="${remote_status}${CYAN_BOLD}${ahead}${RESET}"
[ -n "$behind" ] && remote_status="${remote_status}${CYAN_BOLD}${behind}${RESET}"
# Repo cleaning?
if [ "$staged" -eq 0 ] && [ "$modified" -eq 0 ] && [ "$untracked" -eq 0 ]; then
branch_display="${GREEN_BOLD}${branch}${RESET}"
else
branch_display="${PURPLE_BOLD}${branch}${RESET}"
fi
# Final Building
output="(${branch_display}"
[ -n "$remote_status" ] && output="${output} ${remote_status}"
[ -n "$git_status" ] && output="${output} ${git_status}"
output=$(echo "$output" | sed 's/ $//')
output="${output})"
printf "%s" "$output"
fi
}
PROMPT_COMMAND='PS1="\[\033[1;32m\]\u\[\033[0m\]@\[\033[1;34m\]\h\[\033[0m\]:\[\033[1;33m\]\w\[\033[0m\]$(parse_git_branch)\$ "'