Improve bash prompt speed
Avoiding starting many processes during creation of the PS1 string makes the shell much more responsive, especially under Cygwin, which has problems with fork().
This commit is contained in:
parent
c5baba5f5f
commit
907eb892de
1 changed files with 22 additions and 44 deletions
|
@ -114,6 +114,8 @@ HISTFILE="$XDG_CACHE_HOME/bash/history"
|
||||||
PS1_GIT=$(tput setaf $Green) # color for git branch
|
PS1_GIT=$(tput setaf $Green) # color for git branch
|
||||||
PS1_VENV=$(tput setaf $Violet) # color for python virtual env
|
PS1_VENV=$(tput setaf $Violet) # color for python virtual env
|
||||||
PS1_JOBS=$(tput setaf $Magenta) # color for background jobs
|
PS1_JOBS=$(tput setaf $Magenta) # color for background jobs
|
||||||
|
PS1_SEP=" > " # separator between prompt parts
|
||||||
|
PS1_SEP_COLOR="" # is set in __update_colors
|
||||||
PS1_RST=$(tput sgr0)
|
PS1_RST=$(tput sgr0)
|
||||||
|
|
||||||
GIT_PS1_SHOWDIRTYSTATE=1
|
GIT_PS1_SHOWDIRTYSTATE=1
|
||||||
|
@ -122,56 +124,26 @@ HISTFILE="$XDG_CACHE_HOME/bash/history"
|
||||||
GIT_PS1_SHOWUPSTREAM=verbose
|
GIT_PS1_SHOWUPSTREAM=verbose
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PROMPT_COMMAND=__ps1_set
|
||||||
PS2="... "
|
PS2="... "
|
||||||
|
|
||||||
PROMPT_COMMAND=__ps1_set
|
|
||||||
|
|
||||||
__ps1_set() {
|
__ps1_set() {
|
||||||
local last_status=$?
|
local last_status=$? prompt_char prompt host_color sep
|
||||||
local prompt color sep_color sep
|
|
||||||
|
|
||||||
if [ $EUID -eq 0 ]; then
|
prompt_char=">>>>>>>>>>"
|
||||||
prompt=$(printf '#%.0s' $(seq 1 $SHLVL))
|
host_color=$PS1_DEFAULT
|
||||||
color=$PS1_ROOT
|
[ -n "$SSH_CLIENT" ] && host_color=$PS1_SSH
|
||||||
else
|
[ $EUID -eq 0 ] && { host_color=$PS1_ROOT; prompt_char="!!!!!!!!!!"; }
|
||||||
prompt=$(printf '\$%.0s' $(seq 1 $SHLVL))
|
prompt="${prompt_char:0:$SHLVL}"
|
||||||
if [ -n "$SSH_CLIENT" ]; then
|
sep="$PS1_SEP_COLOR$PS1_SEP$PS1_RST"
|
||||||
color=$PS1_SSH
|
|
||||||
else
|
|
||||||
color=$PS1_DEFAULT
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$BACKGROUND" = "light" ]; then
|
|
||||||
sep_color=$(tput setaf $Base1)
|
|
||||||
else
|
|
||||||
sep_color=$(tput setaf $Base01)
|
|
||||||
fi
|
|
||||||
sep="$sep_color > $PS1_RST"
|
|
||||||
|
|
||||||
PS1="\n"
|
PS1="\n"
|
||||||
if [ $last_status -ne 0 ]; then
|
[ $last_status -ne 0 ] && PS1+="$PS1_ERR$last_status$PS1_RST$sep"
|
||||||
PS1+="$PS1_ERR$last_status$PS1_RST$sep" # last exit code (in non-zero)
|
PS1+="$host_color\h$PS1_RST$sep$PS1_PWD\w$PS1_RST"
|
||||||
fi
|
PS1+=$(__git_ps1 "$sep$PS1_GIT%s$PS1_RST")
|
||||||
PS1+="$color\h$PS1_RST$sep$PS1_PWD\w$PS1_RST" # user@host pwd
|
[ -n "$VIRTUAL_ENV" ] && PS1+="$sep$PS1_VENV${VIRTUAL_ENV##*/}$PS1_RST"
|
||||||
PS1+=$(__git_ps1 "$sep$PS1_GIT%s$PS1_RST") # git status (if in repo)
|
[ "$(jobs | wc -l)" -gt 0 ] && PS1+="$sep${PS1_JOBS}bg: \j$PS1_RST"
|
||||||
PS1+=$(__ps1_venv "$sep$PS1_VENV(%s)$PS1_RST") # python virtual env (if active)
|
PS1+="\n$prompt "
|
||||||
PS1+=$(__ps1_jobs "$sep$PS1_JOBS%s$PS1_RST") # background jobs (if any)
|
|
||||||
PS1+="\n$prompt " # ] $
|
|
||||||
}
|
|
||||||
|
|
||||||
__ps1_venv() {
|
|
||||||
local venv
|
|
||||||
venv="$(basename "$VIRTUAL_ENV" 2>/dev/null)"
|
|
||||||
# shellcheck disable=SC2059 # variable format string is really needed here
|
|
||||||
[ -n "$venv" ] && printf "${1:-%s}" "$venv"
|
|
||||||
}
|
|
||||||
|
|
||||||
__ps1_jobs() {
|
|
||||||
local n
|
|
||||||
n=$(jobs | wc -l)
|
|
||||||
# shellcheck disable=SC2059 # variable format string is really needed here
|
|
||||||
[ "$n" -gt 0 ] && printf "${1:-%s}" "$n job$([ "$n" -gt 1 ] && echo -n s)"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
@ -240,6 +212,12 @@ _update_colors() {
|
||||||
|
|
||||||
export BACKGROUND="$1"
|
export BACKGROUND="$1"
|
||||||
|
|
||||||
|
if [ "$BACKGROUND" = "light" ]; then
|
||||||
|
PS1_SEP_COLOR=$(tput setaf $Base1)
|
||||||
|
else
|
||||||
|
PS1_SEP_COLOR=$(tput setaf $Base01)
|
||||||
|
fi
|
||||||
|
|
||||||
cursor=$Red_RGB
|
cursor=$Red_RGB
|
||||||
if [ "$BACKGROUND" = "light" ]; then
|
if [ "$BACKGROUND" = "light" ]; then
|
||||||
fg=$Base01_RGB
|
fg=$Base01_RGB
|
||||||
|
|
Loading…
Add table
Reference in a new issue