tweaked performance
This commit is contained in:
parent
0f35e9998f
commit
b76120c308
@ -5,7 +5,7 @@
|
|||||||
# Default to theme 1 if not set
|
# Default to theme 1 if not set
|
||||||
GIT_PROMPT_THEME="${GIT_PROMPT_THEME:-1}"
|
GIT_PROMPT_THEME="${GIT_PROMPT_THEME:-1}"
|
||||||
|
|
||||||
# Define all 6 themes
|
# Define theme icons
|
||||||
set_git_prompt_theme_icons() {
|
set_git_prompt_theme_icons() {
|
||||||
case "$GIT_PROMPT_THEME" in
|
case "$GIT_PROMPT_THEME" in
|
||||||
2|3)
|
2|3)
|
||||||
@ -62,40 +62,39 @@ set_git_prompt_theme_icons() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
# Initialize icons
|
|
||||||
set_git_prompt_theme_icons
|
set_git_prompt_theme_icons
|
||||||
|
|
||||||
# Git prompt logic
|
# Caching mechanism
|
||||||
git_prompt_info() {
|
GIT_CACHE_FILE="/tmp/git_prompt_cache.$$"
|
||||||
|
GIT_CACHE_EXPIRY=2 # seconds
|
||||||
|
|
||||||
|
actual_git_prompt_info_logic() {
|
||||||
git rev-parse --is-inside-work-tree &>/dev/null || return
|
git rev-parse --is-inside-work-tree &>/dev/null || return
|
||||||
|
|
||||||
local branch
|
local branch
|
||||||
branch=$(git symbolic-ref --short HEAD 2>/dev/null || echo "(detached)")
|
branch=$(git symbolic-ref --short HEAD 2>/dev/null || echo "(detached)")
|
||||||
|
|
||||||
local status
|
local status
|
||||||
status=$(git status --porcelain=2 --branch 2>/dev/null) || return
|
status=$(GIT_OPTIONAL_LOCKS=0 git status --porcelain --branch 2>/dev/null) || return
|
||||||
|
|
||||||
local staged conflicts changed untracked stashed is_clean has_remote
|
local staged conflicts changed untracked stashed is_clean has_remote
|
||||||
staged=$(grep -cE '^1 [ADMRT]' <<<"$status")
|
staged=$(grep -cE '^[AMDR] ' <<<"$status")
|
||||||
conflicts=$(grep -cE '^u ' <<<"$status")
|
conflicts=$(grep -cE '^UU ' <<<"$status")
|
||||||
changed=$(grep -cE '^1 .[MD]' <<<"$status")
|
changed=$(grep -cE '^.[MD] ' <<<"$status")
|
||||||
untracked=$(grep -cE '^\? ' <<<"$status")
|
untracked=$(grep -cE '^\?\? ' <<<"$status")
|
||||||
stashed=$(git stash list 2>/dev/null | wc -l | tr -d ' ')
|
stashed=$(GIT_OPTIONAL_LOCKS=0 git stash list 2>/dev/null | grep -c .)
|
||||||
|
|
||||||
local upstream
|
local upstream
|
||||||
upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null)
|
upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null)
|
||||||
if [[ -n "$upstream" ]]; then
|
[[ -n "$upstream" ]] && has_remote=1 || has_remote=0
|
||||||
has_remote=1
|
|
||||||
git fetch --quiet & disown
|
|
||||||
else
|
|
||||||
has_remote=0
|
|
||||||
fi
|
|
||||||
|
|
||||||
local ahead=0 behind=0
|
local ahead=0 behind=0
|
||||||
if (( has_remote )); then
|
if (( has_remote )); then
|
||||||
ahead=$(git rev-list --left-right --count "$upstream...HEAD" 2>/dev/null | awk '{print $2}')
|
local counts
|
||||||
behind=$(git rev-list --left-right --count "$upstream...HEAD" 2>/dev/null | awk '{print $1}')
|
counts=$(git rev-list --left-right --count "${upstream}...HEAD" 2>/dev/null | tr -s '[:space:]' ' ')
|
||||||
|
read -r behind ahead <<< "$counts"
|
||||||
|
ahead=${ahead:-0}
|
||||||
|
behind=${behind:-0}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
is_clean=0
|
is_clean=0
|
||||||
@ -104,7 +103,6 @@ git_prompt_info() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$GIT_PROMPT_THEME" == "2" ]]; then
|
if [[ "$GIT_PROMPT_THEME" == "2" ]]; then
|
||||||
# Powerline Theme 2
|
|
||||||
printf "\[\e[30;44m\]"
|
printf "\[\e[30;44m\]"
|
||||||
printf "\[\e[97;44m\] %s%s" "$BRANCH_ICON" "$branch"
|
printf "\[\e[97;44m\] %s%s" "$BRANCH_ICON" "$branch"
|
||||||
((staged > 0)) && printf " %s%d" "$STAGED_ICON" "$staged"
|
((staged > 0)) && printf " %s%d" "$STAGED_ICON" "$staged"
|
||||||
@ -115,18 +113,12 @@ git_prompt_info() {
|
|||||||
((ahead > 0)) && printf " %s%d" "$AHEAD_ICON" "$ahead"
|
((ahead > 0)) && printf " %s%d" "$AHEAD_ICON" "$ahead"
|
||||||
((behind > 0)) && printf " %s%d" "$BEHIND_ICON" "$behind"
|
((behind > 0)) && printf " %s%d" "$BEHIND_ICON" "$behind"
|
||||||
((has_remote == 0)) && printf " %s" "$NO_REMOTE_ICON"
|
((has_remote == 0)) && printf " %s" "$NO_REMOTE_ICON"
|
||||||
if (( is_clean )); then
|
((is_clean)) && printf " %s" "$CLEAN_ICON" || printf " %s" "$DIRTY_ICON"
|
||||||
printf " %s" "$CLEAN_ICON"
|
|
||||||
else
|
|
||||||
printf " %s" "$DIRTY_ICON"
|
|
||||||
fi
|
|
||||||
printf "\[\e[34;107m\]"
|
printf "\[\e[34;107m\]"
|
||||||
printf "\[\e[30;107m\] \w"
|
printf "\[\e[30;107m\] \w"
|
||||||
printf "\[\e[97;49m\]"
|
printf "\[\e[97;49m\]"
|
||||||
printf "\[\e[0m\]"
|
printf "\[\e[0m\]"
|
||||||
|
|
||||||
elif [[ "$GIT_PROMPT_THEME" == "3" ]]; then
|
elif [[ "$GIT_PROMPT_THEME" == "3" ]]; then
|
||||||
# Two-line theme: git info in brackets, directory outside (in gold)
|
|
||||||
printf "\n\[\e[0;37m\]┌──[\e[0m"
|
printf "\n\[\e[0;37m\]┌──[\e[0m"
|
||||||
printf "\e[38;5;117m%s%s\e[0m" "$BRANCH_ICON" "$branch"
|
printf "\e[38;5;117m%s%s\e[0m" "$BRANCH_ICON" "$branch"
|
||||||
((staged > 0)) && printf " \e[38;5;196m%s%d\e[0m" "$STAGED_ICON" "$staged"
|
((staged > 0)) && printf " \e[38;5;196m%s%d\e[0m" "$STAGED_ICON" "$staged"
|
||||||
@ -137,17 +129,10 @@ git_prompt_info() {
|
|||||||
((ahead > 0)) && printf " \e[0;37m%s%d\e[0m" "$AHEAD_ICON" "$ahead"
|
((ahead > 0)) && printf " \e[0;37m%s%d\e[0m" "$AHEAD_ICON" "$ahead"
|
||||||
((behind > 0)) && printf " \e[0;37m%s%d\e[0m" "$BEHIND_ICON" "$behind"
|
((behind > 0)) && printf " \e[0;37m%s%d\e[0m" "$BEHIND_ICON" "$behind"
|
||||||
((has_remote == 0)) && printf " \e[38;5;250m%s\e[0m" "$NO_REMOTE_ICON"
|
((has_remote == 0)) && printf " \e[38;5;250m%s\e[0m" "$NO_REMOTE_ICON"
|
||||||
if (( is_clean )); then
|
((is_clean)) && printf " \e[0;32m%s\e[0m" "$CLEAN_ICON" || printf " \e[38;5;196m%s\e[0m" "$DIRTY_ICON"
|
||||||
printf " \e[0;32m%s\e[0m" "$CLEAN_ICON"
|
printf "\e[0;37m]\e[0m \[\e[38;5;178m\]\w\[\e[0m\]"
|
||||||
else
|
|
||||||
printf " \e[38;5;196m%s\e[0m" "$DIRTY_ICON"
|
|
||||||
fi
|
|
||||||
printf "\e[0;37m]\e[0m"
|
|
||||||
printf " \[\e[38;5;178m\]\w\[\e[0m\]"
|
|
||||||
printf "\n\[\e[0;37m\]└──\[\e[0m\]"
|
printf "\n\[\e[0;37m\]└──\[\e[0m\]"
|
||||||
|
|
||||||
else
|
else
|
||||||
# Default bracket theme
|
|
||||||
printf "\e[0;37m[\e[0m"
|
printf "\e[0;37m[\e[0m"
|
||||||
printf "\e[38;5;117m%s%s\e[0m" "$BRANCH_ICON" "$branch"
|
printf "\e[38;5;117m%s%s\e[0m" "$BRANCH_ICON" "$branch"
|
||||||
((staged > 0)) && printf " \e[38;5;196m%s%d\e[0m" "$STAGED_ICON" "$staged"
|
((staged > 0)) && printf " \e[38;5;196m%s%d\e[0m" "$STAGED_ICON" "$staged"
|
||||||
@ -158,16 +143,29 @@ git_prompt_info() {
|
|||||||
((ahead > 0)) && printf " \e[0;37m%s%d\e[0m" "$AHEAD_ICON" "$ahead"
|
((ahead > 0)) && printf " \e[0;37m%s%d\e[0m" "$AHEAD_ICON" "$ahead"
|
||||||
((behind > 0)) && printf " \e[0;37m%s%d\e[0m" "$BEHIND_ICON" "$behind"
|
((behind > 0)) && printf " \e[0;37m%s%d\e[0m" "$BEHIND_ICON" "$behind"
|
||||||
((has_remote == 0)) && printf " \e[38;5;250m%s\e[0m" "$NO_REMOTE_ICON"
|
((has_remote == 0)) && printf " \e[38;5;250m%s\e[0m" "$NO_REMOTE_ICON"
|
||||||
if (( is_clean )); then
|
((is_clean)) && printf " \e[0;32m%s\e[0m" "$CLEAN_ICON" || printf " \e[38;5;196m%s\e[0m" "$DIRTY_ICON"
|
||||||
printf " \e[0;32m%s\e[0m" "$CLEAN_ICON"
|
|
||||||
else
|
|
||||||
printf " \e[38;5;196m%s\e[0m" "$DIRTY_ICON"
|
|
||||||
fi
|
|
||||||
printf "\e[0;37m]\e[0m"
|
printf "\e[0;37m]\e[0m"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Auto-updating prompt
|
git_prompt_info() {
|
||||||
|
git rev-parse --is-inside-work-tree &>/dev/null || return
|
||||||
|
|
||||||
|
local now=$(date +%s)
|
||||||
|
local last_mod=0
|
||||||
|
[[ -f "$GIT_CACHE_FILE" ]] && last_mod=$(stat -c %Y "$GIT_CACHE_FILE" 2>/dev/null || echo 0)
|
||||||
|
|
||||||
|
if (( now - last_mod < GIT_CACHE_EXPIRY )); then
|
||||||
|
cat "$GIT_CACHE_FILE"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
local output
|
||||||
|
output=$(actual_git_prompt_info_logic)
|
||||||
|
echo "$output" > "$GIT_CACHE_FILE"
|
||||||
|
echo "$output"
|
||||||
|
}
|
||||||
|
|
||||||
update_git_prompt() {
|
update_git_prompt() {
|
||||||
local PROMPT_CHAR='$'
|
local PROMPT_CHAR='$'
|
||||||
[[ $EUID -eq 0 ]] && PROMPT_CHAR='#'
|
[[ $EUID -eq 0 ]] && PROMPT_CHAR='#'
|
||||||
@ -188,7 +186,6 @@ update_git_prompt() {
|
|||||||
|
|
||||||
PROMPT_COMMAND=update_git_prompt
|
PROMPT_COMMAND=update_git_prompt
|
||||||
|
|
||||||
# Theme switch command
|
|
||||||
gpchange() {
|
gpchange() {
|
||||||
local theme="${1:-}"
|
local theme="${1:-}"
|
||||||
if [[ "$theme" =~ ^[1-5]$ ]]; then
|
if [[ "$theme" =~ ^[1-5]$ ]]; then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user