Optimize bash/zsh checks

Avoid string comparisons. Use instead true/false builtins.
So:
    if [[ $_LP_WORKING_SHELL == bash ]]
becomes:
    if $_LP_SHELL_bash
This commit is contained in:
Olivier Mengué 2013-05-05 00:11:18 +02:00
parent 7e01b1648c
commit b424c62ba8

View File

@ -56,14 +56,16 @@ if test -n "$BASH_VERSION" -a -n "$PS1" -a -n "$TERM" ; then
fi fi
unset bash bmajor bminor unset bash bmajor bminor
_LP_WORKING_SHELL=bash _LP_SHELL_bash=true
_LP_SHELL_zsh=false
_LP_OPEN_ESC="\[" _LP_OPEN_ESC="\["
_LP_CLOSE_ESC="\]" _LP_CLOSE_ESC="\]"
_LP_USER_SYMBOL="\u" _LP_USER_SYMBOL="\u"
_LP_HOST_SYMBOL="\h" _LP_HOST_SYMBOL="\h"
_LP_TIME_SYMBOL="\t" _LP_TIME_SYMBOL="\t"
elif test -n "$ZSH_VERSION" ; then elif test -n "$ZSH_VERSION" ; then
_LP_WORKING_SHELL=zsh _LP_SHELL_bash=false
_LP_SHELL_zsh=true
_LP_OPEN_ESC="%{" _LP_OPEN_ESC="%{"
_LP_CLOSE_ESC="%}" _LP_CLOSE_ESC="%}"
_LP_USER_SYMBOL="%n" _LP_USER_SYMBOL="%n"
@ -472,7 +474,7 @@ _lp_proxy()
_lp_shorten_path() _lp_shorten_path()
{ {
if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 || -n "$PROMPT_DIRTRIM" ]] ; then if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 || -n "$PROMPT_DIRTRIM" ]] ; then
if [[ "$_LP_WORKING_SHELL" == bash ]]; then if $_LP_SHELL_bash; then
echo "\\w" echo "\\w"
else else
print -P '%~' print -P '%~'
@ -484,7 +486,7 @@ _lp_shorten_path()
local -i len=${#p} local -i len=${#p}
local -i max_len=$((${COLUMNS:-80}*$LP_PATH_LENGTH/100)) local -i max_len=$((${COLUMNS:-80}*$LP_PATH_LENGTH/100))
if [[ "$_LP_WORKING_SHELL" == bash ]]; then if $_LP_SHELL_bash; then
if (( len > max_len )) if (( len > max_len ))
then then
# index of the directory to keep from the root # index of the directory to keep from the root
@ -552,7 +554,7 @@ _lp_shorten_path()
else else
echo "$p" echo "$p"
fi fi
else # [[ "$_LP_WORKING_SHELL" == zsh ]] else # zsh
if (( len > max_len )); then if (( len > max_len )); then
print -P "%-${LP_PATH_KEEP}~%${max_len}<${LP_MARK_SHORTEN_PATH}<%~%<<" print -P "%-${LP_PATH_KEEP}~%${max_len}<${LP_MARK_SHORTEN_PATH}<%~%<<"
else else
@ -1126,9 +1128,9 @@ _lp_battery_color()
ret="${ret}${LP_COLORMAP_0}" ret="${ret}${LP_COLORMAP_0}"
fi fi
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then if $_LP_SHELL_bash; then
ret="${ret}${bat}%" ret="${ret}${bat}%"
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then else # zsh
ret="${ret}${bat}%%" ret="${ret}${bat}%%"
fi fi
fi # LP_PERCENTS_ALWAYS fi # LP_PERCENTS_ALWAYS
@ -1188,9 +1190,9 @@ _lp_load_color()
local ret="$(_lp_color_map $load) ${LP_MARK_LOAD}" local ret="$(_lp_color_map $load) ${LP_MARK_LOAD}"
if [[ "$LP_PERCENTS_ALWAYS" -eq "1" ]]; then if [[ "$LP_PERCENTS_ALWAYS" -eq "1" ]]; then
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then if $_LP_SHELL_bash; then
ret="${ret}$load%" ret="${ret}$load%"
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then else # zsh
ret="${ret}$load%%" ret="${ret}$load%%"
fi fi
fi fi
@ -1299,7 +1301,7 @@ _lp_smart_mark()
local mark local mark
if [[ -n "$LP_MARK_DEFAULT" ]]; then if [[ -n "$LP_MARK_DEFAULT" ]]; then
mark=$LP_MARK_DEFAULT mark=$LP_MARK_DEFAULT
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then elif $_LP_SHELL_zsh; then
mark="%(!.#.%%)" mark="%(!.#.%%)"
else else
mark="\\\$" mark="\\\$"
@ -1552,15 +1554,15 @@ prompt_on()
# if liquidprompt has not been already set # if liquidprompt has not been already set
if [[ -z "$LP_LIQUIDPROMPT" ]] ; then if [[ -z "$LP_LIQUIDPROMPT" ]] ; then
LP_OLD_PS1="$PS1" LP_OLD_PS1="$PS1"
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then if $_LP_SHELL_bash; then
LP_OLD_PROMPT_COMMAND="$PROMPT_COMMAND" LP_OLD_PROMPT_COMMAND="$PROMPT_COMMAND"
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then else # zsh
LP_OLD_PROMPT_COMMAND="$precmd" LP_OLD_PROMPT_COMMAND="$precmd"
fi fi
fi fi
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then if $_LP_SHELL_bash; then
PROMPT_COMMAND=_lp_set_prompt PROMPT_COMMAND=_lp_set_prompt
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then else # zsh
function precmd { function precmd {
_lp_set_prompt _lp_set_prompt
} }
@ -1575,9 +1577,9 @@ prompt_on()
prompt_off() prompt_off()
{ {
PS1=$LP_OLD_PS1 PS1=$LP_OLD_PS1
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then if $_LP_SHELL_bash; then
PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then else # zsh
precmd=$LP_OLD_PROMPT_COMMAND precmd=$LP_OLD_PROMPT_COMMAND
fi fi
} }
@ -1586,9 +1588,9 @@ prompt_off()
prompt_OFF() prompt_OFF()
{ {
PS1="\$ " PS1="\$ "
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then if $_LP_SHELL_bash; then
PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then else # zsh
precmd=$LP_OLD_PROMPT_COMMAND precmd=$LP_OLD_PROMPT_COMMAND
fi fi
} }