fix #26 : prefix with _lp_ or LP_ and local variables

This commit is contained in:
nojhan 2012-08-13 23:15:24 +02:00
parent 6f0bf08f61
commit 7b8ba5e7f9
3 changed files with 219 additions and 174 deletions

View File

@ -110,12 +110,12 @@ path
it only when connected with a remote shell it only when connected with a remote shell
You can sort what you want to see by exporting the `LP_PS1` variable, using the You can sort what you want to see by exporting the `LP_PS1` variable, using the
variables you will found in the `__set_bash_prompt` function. variables you will found in the `_lp_set_bash_prompt` function.
For example, if you just want to have a liquidprompt displaying the user and the For example, if you just want to have a liquidprompt displaying the user and the
host, with a normal path in blue and only the git support: host, with a normal path in blue and only the git support:
export LP_PS1=`echo -ne "[\${__USER}\${__HOST}:\${BLUE}\$(pwd)\${NO_COL}] \${__GIT} \\\$ "` export LP_PS1=`echo -ne "[\${LP_USER}\${LP_HOST}:\${BLUE}\$(pwd)\${NO_COL}] \${LP_GIT} \\\$ "`
Note that you need to properly escape dollars in a string that wil be Note that you need to properly escape dollars in a string that wil be
interpreted by bash at each prompt. interpreted by bash at each prompt.

View File

@ -37,14 +37,14 @@
# See the README.md file for a summary of features. # See the README.md file for a summary of features.
WORKING_SHELL=${SHELL##*/} _LP_WORKING_SHELL=${SHELL##*/}
# A login shell starts with a "-" # A login shell starts with a "-"
if [[ "$WORKING_SHELL" == "-bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "-bash" ]]; then
WORKING_SHELL="bash" _LP_WORKING_SHELL="bash"
fi fi
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
# Check for recent enough version of bash. # Check for recent enough version of bash.
[[ -z "$BASH_VERSION" || -z "$PS1" || -z "$TERM" ]] && return; [[ -z "$BASH_VERSION" || -z "$PS1" || -z "$TERM" ]] && return;
@ -55,15 +55,15 @@ if [[ "$WORKING_SHELL" == "bash" ]]; then
fi fi
unset bash bmajor bminor unset bash bmajor bminor
OPENESCAPE="\[" _LP_OPEN_ESC="\["
CLOSEESCAPE="\]" _LP_OPEN_ESC="\]"
USERSYMBOL="\u" _LP_USER_SYMBOL="\u"
HOSTSYMBOL="\h" _LP_HOST_SYMBOL="\h"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
OPENESCAPE="%{" _LP_OPEN_ESC="%{"
CLOSEESCAPE="%}" _LP_OPEN_ESC="%}"
USERSYMBOL="%n" _LP_USER_SYMBOL="%n"
HOSTSYMBOL="%m" _LP_HOST_SYMBOL="%m"
fi fi
@ -90,97 +90,102 @@ LP_SUBVERSION_MARK=${LP_SUBVERSION_MARK:-"‡"}
# Default config file may be the XDG standard ~/.config/liquidpromptrc, # Default config file may be the XDG standard ~/.config/liquidpromptrc,
# but heirloom dotfile has priority. # but heirloom dotfile has priority.
if [[ -f "/etc/liquidpromptrc" ]] _lp_source_config()
then {
configfile="/etc/liquidpromptrc" local configfile
fi if [[ -f "/etc/liquidpromptrc" ]]
if [[ -f "$HOME/.liquidpromptrc" ]] then
then configfile="/etc/liquidpromptrc"
configfile="$HOME/.liquidpromptrc" fi
elif [[ -z "$XDG_HOME_DIR" ]] if [[ -f "$HOME/.liquidpromptrc" ]]
then then
configfile="$HOME/.config/liquidpromptrc" configfile="$HOME/.liquidpromptrc"
else elif [[ -z "$XDG_HOME_DIR" ]]
configfile="$XDG_HOME_DIR/liquidpromptrc" then
fi configfile="$HOME/.config/liquidpromptrc"
if [[ -f "$configfile" ]] else
then configfile="$XDG_HOME_DIR/liquidpromptrc"
source "$configfile" fi
fi if [[ -f "$configfile" ]]
then
source "$configfile"
fi
}
# do source config files
_lp_source_config
############### ###############
# OS specific # # LP_OS specific #
############### ###############
# OS detection, default to Linux # LP_OS detection, default to Linux
OS="Linux" LP_OS="Linux"
case $(uname) in case $(uname) in
"Linux" ) OS="Linux" ;; "Linux" ) LP_OS="Linux" ;;
"FreeBSD") OS="FreeBSD" ;; "FreeBSD") LP_OS="FreeBSD" ;;
"Darwin") OS="Darwin" ;; "Darwin") LP_OS="Darwin" ;;
"DragonFly") OS="FreeBSD" ;; "DragonFly") LP_OS="FreeBSD" ;;
"SunOS") OS="SunOS" ;; "SunOS") LP_OS="SunOS" ;;
esac esac
# Colors declarations # Colors declarations
if [[ "$OS" == "FreeBSD" ]] ; then if [[ "$LP_OS" == "FreeBSD" ]] ; then
BLACK="${OPENESCAPE}$(tput AF 0)${CLOSEESCAPE}" BLACK="${_LP_OPEN_ESC}$(tput AF 0)${_LP_OPEN_ESC}"
BOLD_GRAY="${OPENESCAPE}$(tput md ; tput AF 0)${CLOSEESCAPE}" BOLD_GRAY="${_LP_OPEN_ESC}$(tput md ; tput AF 0)${_LP_OPEN_ESC}"
WHITE="${OPENESCAPE}$(tput AF 7)${CLOSEESCAPE}" WHITE="${_LP_OPEN_ESC}$(tput AF 7)${_LP_OPEN_ESC}"
BOLD_WHITE="${OPENESCAPE}$(tput md ; tput AF 7)${CLOSEESCAPE}" BOLD_WHITE="${_LP_OPEN_ESC}$(tput md ; tput AF 7)${_LP_OPEN_ESC}"
RED="${OPENESCAPE}$(tput AF 1)${CLOSEESCAPE}" RED="${_LP_OPEN_ESC}$(tput AF 1)${_LP_OPEN_ESC}"
BOLD_RED="${OPENESCAPE}$(tput md ; tput AF 1)${CLOSEESCAPE}" BOLD_RED="${_LP_OPEN_ESC}$(tput md ; tput AF 1)${_LP_OPEN_ESC}"
WARN_RED="${OPENESCAPE}$(tput AF 0 ; tput setab 1)${CLOSEESCAPE}" WARN_RED="${_LP_OPEN_ESC}$(tput AF 0 ; tput setab 1)${_LP_OPEN_ESC}"
CRIT_RED="${OPENESCAPE}$(tput md; tput AF 7 ; tput setab 1)${CLOSEESCAPE}" CRIT_RED="${_LP_OPEN_ESC}$(tput md; tput AF 7 ; tput setab 1)${_LP_OPEN_ESC}"
GREEN="${OPENESCAPE}$(tput AF 2)${CLOSEESCAPE}" GREEN="${_LP_OPEN_ESC}$(tput AF 2)${_LP_OPEN_ESC}"
BOLD_GREEN="${OPENESCAPE}$(tput md ; tput AF 2)${CLOSEESCAPE}" BOLD_GREEN="${_LP_OPEN_ESC}$(tput md ; tput AF 2)${_LP_OPEN_ESC}"
YELLOW="${OPENESCAPE}$(tput AF 3)${CLOSEESCAPE}" YELLOW="${_LP_OPEN_ESC}$(tput AF 3)${_LP_OPEN_ESC}"
BOLD_YELLOW="${OPENESCAPE}$(tput md ; tput AF 3)${CLOSEESCAPE}" BOLD_YELLOW="${_LP_OPEN_ESC}$(tput md ; tput AF 3)${_LP_OPEN_ESC}"
BLUE="${OPENESCAPE}$(tput AF 4)${CLOSEESCAPE}" BLUE="${_LP_OPEN_ESC}$(tput AF 4)${_LP_OPEN_ESC}"
BOLD_BLUE="${OPENESCAPE}$(tput md ; tput AF 4)${CLOSEESCAPE}" BOLD_BLUE="${_LP_OPEN_ESC}$(tput md ; tput AF 4)${_LP_OPEN_ESC}"
PURPLE="${OPENESCAPE}$(tput AF 5)${CLOSEESCAPE}" PURPLE="${_LP_OPEN_ESC}$(tput AF 5)${_LP_OPEN_ESC}"
PINK="${OPENESCAPE}$(tput md ; tput AF 5)${CLOSEESCAPE}" PINK="${_LP_OPEN_ESC}$(tput md ; tput AF 5)${_LP_OPEN_ESC}"
CYAN="${OPENESCAPE}$(tput AF 6)${CLOSEESCAPE}" CYAN="${_LP_OPEN_ESC}$(tput AF 6)${_LP_OPEN_ESC}"
BOLD_CYAN="${OPENESCAPE}$(tput md ; tput AF 6)${CLOSEESCAPE}" BOLD_CYAN="${_LP_OPEN_ESC}$(tput md ; tput AF 6)${_LP_OPEN_ESC}"
NO_COL="${OPENESCAPE}$(tput me)${CLOSEESCAPE}" NO_COL="${_LP_OPEN_ESC}$(tput me)${_LP_OPEN_ESC}"
else else
# default to Linux # default to Linux
BLACK="${OPENESCAPE}$(tput setaf 0)${CLOSEESCAPE}" BLACK="${_LP_OPEN_ESC}$(tput setaf 0)${_LP_OPEN_ESC}"
BOLD_GRAY="${OPENESCAPE}$(tput bold ; tput setaf 0)${CLOSEESCAPE}" BOLD_GRAY="${_LP_OPEN_ESC}$(tput bold ; tput setaf 0)${_LP_OPEN_ESC}"
WHITE="${OPENESCAPE}$(tput setaf 7)${CLOSEESCAPE}" WHITE="${_LP_OPEN_ESC}$(tput setaf 7)${_LP_OPEN_ESC}"
BOLD_WHITE="${OPENESCAPE}$(tput bold ; tput setaf 7)${CLOSEESCAPE}" BOLD_WHITE="${_LP_OPEN_ESC}$(tput bold ; tput setaf 7)${_LP_OPEN_ESC}"
RED="${OPENESCAPE}$(tput setaf 1)${CLOSEESCAPE}" RED="${_LP_OPEN_ESC}$(tput setaf 1)${_LP_OPEN_ESC}"
BOLD_RED="${OPENESCAPE}$(tput bold ; tput setaf 1)${CLOSEESCAPE}" BOLD_RED="${_LP_OPEN_ESC}$(tput bold ; tput setaf 1)${_LP_OPEN_ESC}"
WARN_RED="${OPENESCAPE}$(tput setaf 0 ; tput setab 1)${CLOSEESCAPE}" WARN_RED="${_LP_OPEN_ESC}$(tput setaf 0 ; tput setab 1)${_LP_OPEN_ESC}"
CRIT_RED="${OPENESCAPE}$(tput bold; tput setaf 7 ; tput setab 1)${CLOSEESCAPE}" CRIT_RED="${_LP_OPEN_ESC}$(tput bold; tput setaf 7 ; tput setab 1)${_LP_OPEN_ESC}"
GREEN="${OPENESCAPE}$(tput setaf 2)${CLOSEESCAPE}" GREEN="${_LP_OPEN_ESC}$(tput setaf 2)${_LP_OPEN_ESC}"
BOLD_GREEN="${OPENESCAPE}$(tput bold ; tput setaf 2)${CLOSEESCAPE}" BOLD_GREEN="${_LP_OPEN_ESC}$(tput bold ; tput setaf 2)${_LP_OPEN_ESC}"
YELLOW="${OPENESCAPE}$(tput setaf 3)${CLOSEESCAPE}" YELLOW="${_LP_OPEN_ESC}$(tput setaf 3)${_LP_OPEN_ESC}"
BOLD_YELLOW="${OPENESCAPE}$(tput bold ; tput setaf 3)${CLOSEESCAPE}" BOLD_YELLOW="${_LP_OPEN_ESC}$(tput bold ; tput setaf 3)${_LP_OPEN_ESC}"
BLUE="${OPENESCAPE}$(tput setaf 4)${CLOSEESCAPE}" BLUE="${_LP_OPEN_ESC}$(tput setaf 4)${_LP_OPEN_ESC}"
BOLD_BLUE="${OPENESCAPE}$(tput bold ; tput setaf 4)${CLOSEESCAPE}" BOLD_BLUE="${_LP_OPEN_ESC}$(tput bold ; tput setaf 4)${_LP_OPEN_ESC}"
PURPLE="${OPENESCAPE}$(tput setaf 5)${CLOSEESCAPE}" PURPLE="${_LP_OPEN_ESC}$(tput setaf 5)${_LP_OPEN_ESC}"
PINK="${OPENESCAPE}$(tput bold ; tput setaf 5)${CLOSEESCAPE}" PINK="${_LP_OPEN_ESC}$(tput bold ; tput setaf 5)${_LP_OPEN_ESC}"
CYAN="${OPENESCAPE}$(tput setaf 6)${CLOSEESCAPE}" CYAN="${_LP_OPEN_ESC}$(tput setaf 6)${_LP_OPEN_ESC}"
BOLD_CYAN="${OPENESCAPE}$(tput bold ; tput setaf 6)${CLOSEESCAPE}" BOLD_CYAN="${_LP_OPEN_ESC}$(tput bold ; tput setaf 6)${_LP_OPEN_ESC}"
NO_COL="${OPENESCAPE}$(tput sgr0)${CLOSEESCAPE}" NO_COL="${_LP_OPEN_ESC}$(tput sgr0)${_LP_OPEN_ESC}"
fi fi
@ -195,50 +200,53 @@ fi
# get cpu number # get cpu number
__cpunum_Linux() _lp_cpunum_Linux()
{ {
grep -c '^[Pp]rocessor' /proc/cpuinfo grep -c '^[Pp]rocessor' /proc/cpuinfo
} }
__cpunum_FreeBSD() _lp_cpunum_FreeBSD()
{ {
sysctl -n hw.ncpu sysctl -n hw.ncpu
} }
__cpunum_Darwin() _lp_cpunum_Darwin()
{ {
__cpunum_FreeBSD _lp_cpunum_FreeBSD
} }
__cpunum_SunOS() _lp_cpunum_SunOS()
{ {
kstat -m cpu_info | grep -c "module: cpu_info" kstat -m cpu_info | grep -c "module: cpu_info"
} }
__CPUNUM=$(__cpunum_$OS) _lp_CPUNUM=$(_lp_cpunum_$LP_OS)
# get current load # get current load
__load_Linux() _lp_load_Linux()
{ {
local load
load=$(awk '{print $1}' /proc/loadavg) load=$(awk '{print $1}' /proc/loadavg)
echo -n "$load" echo -n "$load"
} }
__load_FreeBSD() _lp_load_FreeBSD()
{ {
local load
load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}') load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}')
echo -n "$load" echo -n "$load"
} }
__load_Darwin() _lp_load_Darwin()
{ {
__load_FreeBSD _lp_load_FreeBSD
} }
__load_SunOS() _lp_load_SunOS()
{ {
local load
load=$(LANG=C uptime | awk '{print substr($10,0,length($10))}') load=$(LANG=C uptime | awk '{print substr($10,0,length($10))}')
echo -n "$load" echo -n "$load"
} }
@ -249,18 +257,19 @@ __load_SunOS()
############### ###############
# Yellow for root, light grey if the user is not the login one, else no color. # Yellow for root, light grey if the user is not the login one, else no color.
__user() _lp_user()
{ {
local user
# if user is not root # if user is not root
if [[ "$EUID" -ne "0" ]] ; then if [[ "$EUID" -ne "0" ]] ; then
# if user is not login user # if user is not login user
if [[ ${USER} != "$(logname 2>/dev/null)" ]]; then if [[ ${USER} != "$(logname 2>/dev/null)" ]]; then
user="${FG}${USERSYMBOL}${NO_COL}" user="${FG}${_LP_USER_SYMBOL}${NO_COL}"
else else
user="${USERSYMBOL}" user="${_LP_USER_SYMBOL}"
fi fi
else else
user="${BOLD_YELLOW}${USERSYMBOL}${NO_COL}" user="${BOLD_YELLOW}${_LP_USER_SYMBOL}${NO_COL}"
fi fi
echo -ne $user echo -ne $user
@ -271,22 +280,28 @@ __user()
# Where are we? # # Where are we? #
################# #################
__connection() _lp_connection()
{ {
local THIS_TTY
THIS_TTY=$(awk -v pid=$$ '$0 ~ pid && /bash/{ print $7 }' < <(ps aux) ) THIS_TTY=$(awk -v pid=$$ '$0 ~ pid && /bash/{ print $7 }' < <(ps aux) )
local SESS_SRC
SESS_SRC=$(who | awk -v thistty="$THIS_TTY" '$0 ~ thistty { print $6 }') SESS_SRC=$(who | awk -v thistty="$THIS_TTY" '$0 ~ thistty { print $6 }')
# Are we in an SSH connexion? # Are we in an SSH connexion?
local SSH_FLAG
SSH_FLAG=0 SSH_FLAG=0
local SSH_IP
SSH_IP=${SSH_CLIENT%% *} SSH_IP=${SSH_CLIENT%% *}
if [[ ! -z $SSH_IP ]] ; then if [[ ! -z $SSH_IP ]] ; then
SSH_FLAG=1 SSH_FLAG=1
fi fi
local SSH2_IP
SSH2_IP=$(echo $SSH2_CLIENT | awk '{ print $1 }') SSH2_IP=$(echo $SSH2_CLIENT | awk '{ print $1 }')
if [[ ! -z $SSH2_IP ]] ; then if [[ ! -z $SSH2_IP ]] ; then
SSH_FLAG=1 SSH_FLAG=1
fi fi
local CONN
if [[ $SSH_FLAG -eq 1 ]] ; then if [[ $SSH_FLAG -eq 1 ]] ; then
CONN="ssh" CONN="ssh"
elif [[ -z $SESS_SRC ]] ; then elif [[ -z $SESS_SRC ]] ; then
@ -303,30 +318,32 @@ __connection()
# Put the hostname if not locally connected # Put the hostname if not locally connected
# color it in cyan within SSH, and a warning red if within telnet # color it in cyan within SSH, and a warning red if within telnet
# else diplay the host without color # else diplay the host without color
__host_color() _lp_host_color()
{ {
conn=$(__connection) local conn
conn=$(_lp_connection)
local ret
ret="${NO_COL}" ret="${NO_COL}"
if [[ "$conn" == "lcl" ]] ; then if [[ "$conn" == "lcl" ]] ; then
if [[ $LP_HOSTNAME_ALWAYS == 0 ]] ; then if [[ $LP_HOSTNAME_ALWAYS == 0 ]] ; then
ret="${ret}" # no hostname if local ret="${ret}" # no hostname if local
else else
ret="${ret}@${HOSTSYMBOL}" ret="${ret}@${_LP_HOST_SYMBOL}"
fi fi
elif [[ "$conn" == "ssh" ]] ; then elif [[ "$conn" == "ssh" ]] ; then
ret="${ret}@${BOLD_CYAN}${HOSTSYMBOL}" ret="${ret}@${BOLD_CYAN}${_LP_HOST_SYMBOL}"
elif [[ "$conn" == "tel" ]] ; then elif [[ "$conn" == "tel" ]] ; then
ret="${ret}@${WARN_RED}${HOSTSYMBOL}" ret="${ret}@${WARN_RED}${_LP_HOST_SYMBOL}"
else else
ret="${ret}@${HOSTSYMBOL}" ret="${ret}@${_LP_HOST_SYMBOL}"
fi fi
echo -ne "${ret}${NO_COL}" echo -ne "${ret}${NO_COL}"
} }
# put an arrow if an http proxy is set # put an arrow if an http proxy is set
__proxy() _lp_proxy()
{ {
if [[ ! -z "$http_proxy" ]] ; then if [[ ! -z "$http_proxy" ]] ; then
echo -ne $LP_PROXY_MARK echo -ne $LP_PROXY_MARK
@ -345,13 +362,13 @@ __proxy()
# http://hbfs.wordpress.com/2009/09/01/short-pwd-in-bash-prompts/ # http://hbfs.wordpress.com/2009/09/01/short-pwd-in-bash-prompts/
# #
# + keep some left part of the path if asked # + keep some left part of the path if asked
__shorten_path() _lp_shorten_path()
{ {
# the character that will replace the part of the path that is masked # the character that will replace the part of the path that is masked
local mask=" … " local mask=" … "
# index of the directory to keep from the root (starts at 0 whith bash, 1 with zsh) # index of the directory to keep from the root (starts at 0 whith bash, 1 with zsh)
local keep=$((LP_PATH_KEEP-1)) local keep=$((LP_PATH_KEEP-1))
if [[ "$WORKING_SHELL" == "zsh" ]]; then if [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
keep=$LP_PATH_KEEP keep=$LP_PATH_KEEP
fi fi
@ -364,7 +381,7 @@ __shorten_path()
local mask_len="${#mask}" local mask_len="${#mask}"
local slashes=0 local slashes=0
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
if [[ "$len" -gt "$max_len" ]] if [[ "$len" -gt "$max_len" ]]
then then
# finds all the '/' in # finds all the '/' in
@ -424,7 +441,7 @@ __shorten_path()
else else
echo "$p" echo "$p"
fi fi
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
if [[ "$len" -gt "$max_len" ]]; then if [[ "$len" -gt "$max_len" ]]; then
echo "%-${keep}~%${max_len}<${mask}<%~%<<" echo "%-${keep}~%${max_len}<${mask}<%~%<<"
else else
@ -436,7 +453,7 @@ __shorten_path()
# Display a ":" # Display a ":"
# colored in green if user have write permission on the current directory # colored in green if user have write permission on the current directory
# colored in red if it have not. # colored in red if it have not.
__permissions_color() _lp_permissions_color()
{ {
if [[ -w "${PWD}" ]]; then if [[ -w "${PWD}" ]]; then
echo "${GREEN}:${NO_COL}" echo "${GREEN}:${NO_COL}"
@ -453,7 +470,7 @@ __permissions_color()
# Either attached running jobs (started with $ myjob &) # Either attached running jobs (started with $ myjob &)
# or attached stopped jobs (suspended with Ctrl-Z) # or attached stopped jobs (suspended with Ctrl-Z)
# or detached screens sessions running on the host # or detached screens sessions running on the host
__jobcount_color() _lp_jobcount_color()
{ {
local running=$(( $(jobs -r | wc -l) )) local running=$(( $(jobs -r | wc -l) ))
local stopped=$(( $(jobs -s | wc -l) )) local stopped=$(( $(jobs -s | wc -l) ))
@ -461,6 +478,7 @@ __jobcount_color()
local m_detached="d" local m_detached="d"
local m_stop="z" local m_stop="z"
local m_run="&" local m_run="&"
local rep
# d/&/z # d/&/z
if [[ $screens != "0" && $running != "0" && $stopped != "0" ]] ; then if [[ $screens != "0" && $running != "0" && $stopped != "0" ]] ; then
@ -494,7 +512,7 @@ __jobcount_color()
} }
# Display the return value of the last command, if different from zero # Display the return value of the last command, if different from zero
__return_value() _lp_return_value()
{ {
if [[ "$1" -ne "0" ]] if [[ "$1" -ne "0" ]]
then then
@ -510,7 +528,7 @@ __return_value()
# GIT # # GIT #
# Get the branch name of the current directory # Get the branch name of the current directory
__git_branch() _lp_git_branch()
{ {
if git rev-parse --git-dir >/dev/null 2>&1 && [[ ! -z "$(git branch)" ]] ; then if git rev-parse --git-dir >/dev/null 2>&1 && [[ ! -z "$(git branch)" ]] ; then
echo -n "$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p;')" echo -n "$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p;')"
@ -523,22 +541,28 @@ __git_branch()
# - red if there is changes to commit # - red if there is changes to commit
# #
# Add the number of pending commits and the impacted lines. # Add the number of pending commits and the impacted lines.
__git_branch_color() _lp_git_branch_color()
{ {
command -v git >/dev/null 2>&1 || return 1; command -v git >/dev/null 2>&1 || return 1;
branch=$(__git_branch) local branch
branch=$(_lp_git_branch)
if [[ ! -z "$branch" ]] ; then if [[ ! -z "$branch" ]] ; then
local GD
git diff --quiet >/dev/null 2>&1 git diff --quiet >/dev/null 2>&1
GD=$? GD=$?
local GDC
git diff --cached --quiet >/dev/null 2>&1 git diff --cached --quiet >/dev/null 2>&1
GDC=$? GDC=$?
local has_commit
has_commit=$(git rev-list --no-merges --count origin/${branch}..${branch} 2>/dev/null) has_commit=$(git rev-list --no-merges --count origin/${branch}..${branch} 2>/dev/null)
if [[ -z "$has_commit" ]] ; then if [[ -z "$has_commit" ]] ; then
has_commit=0 has_commit=0
fi fi
if [[ "$GD" -eq 1 || "$GDC" -eq "1" ]] ; then if [[ "$GD" -eq 1 || "$GDC" -eq "1" ]] ; then
local has_line
has_lines=$(git diff --numstat | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d/-%d\n", plus, minus)}') has_lines=$(git diff --numstat | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d/-%d\n", plus, minus)}')
if [[ "$has_commit" -gt "0" ]] ; then if [[ "$has_commit" -gt "0" ]] ; then
# Changes to commit and commits to push # Changes to commit and commits to push
@ -562,8 +586,9 @@ __git_branch_color()
# MERCURIAL # # MERCURIAL #
# Get the branch name of the current directory # Get the branch name of the current directory
__hg_branch() _lp_hg_branch()
{ {
local branch
branch="$(hg branch 2>/dev/null)" branch="$(hg branch 2>/dev/null)"
if [[ $? -eq 0 ]] && [[ ! -z "$(hg branch)" ]] ; then if [[ $? -eq 0 ]] && [[ ! -z "$(hg branch)" ]] ; then
echo -n "$(hg branch)" echo -n "$(hg branch)"
@ -574,10 +599,12 @@ __hg_branch()
# - green if the repository is up to date # - green if the repository is up to date
# - red if there is changes to commit # - red if there is changes to commit
# - TODO: yellow if there is some commits not pushed # - TODO: yellow if there is some commits not pushed
__hg_branch_color() _lp_hg_branch_color()
{ {
command -v hg >/dev/null 2>&1 || return 1; command -v hg >/dev/null 2>&1 || return 1;
branch=$(__hg_branch) local branch
local ret
branch=$(_lp_hg_branch)
if [[ ! -z "$branch" ]] ; then if [[ ! -z "$branch" ]] ; then
if [[ $(( $(hg status --quiet -n | wc -l) )) = 0 ]] ; then if [[ $(( $(hg status --quiet -n | wc -l) )) = 0 ]] ; then
ret="${GREEN}${branch}${NO_COL}" ret="${GREEN}${branch}${NO_COL}"
@ -592,16 +619,21 @@ __hg_branch_color()
# Get the branch name of the current directory # Get the branch name of the current directory
# For the first level of the repository, gives the repository name # For the first level of the repository, gives the repository name
__svn_branch() _lp_svn_branch()
{ {
local infos
local ret
infos=$(svn info --xml 2>/dev/null) infos=$(svn info --xml 2>/dev/null)
ret=$? ret=$?
if [[ $ret -eq 0 ]] ; then if [[ $ret -eq 0 ]] ; then
local root
root=$(echo "$infos" | awk -v FS=">|</" '/^<root>/ { print $2 }') root=$(echo "$infos" | awk -v FS=">|</" '/^<root>/ { print $2 }')
local subrep
subrep=$(echo "$infos" | awk -v FS=">|</" '/^<url>/ { print $2 }') subrep=$(echo "$infos" | awk -v FS=">|</" '/^<url>/ { print $2 }')
if [[ "$subrep" == *"url>"* ]] ; then if [[ "$subrep" == *"url>"* ]] ; then
echo -n $root echo -n $root
else else
local branch
branch=$(basename $subrep) branch=$(basename $subrep)
echo -n $branch echo -n $branch
fi fi
@ -613,13 +645,16 @@ __svn_branch()
# - red if there is changes to commit # - red if there is changes to commit
# Note that, due to subversion way of managing changes, # Note that, due to subversion way of managing changes,
# informations are only displayed for the CURRENT directory. # informations are only displayed for the CURRENT directory.
__svn_branch_color() _lp_svn_branch_color()
{ {
command -v svn >/dev/null 2>&1 || return 1; command -v svn >/dev/null 2>&1 || return 1;
branch=$(__svn_branch) local branch
branch=$(_lp_svn_branch)
if [[ ! -z "$branch" ]] ; then if [[ ! -z "$branch" ]] ; then
local commits
commits=$(( $(svn status | grep -v "?" -c) )) commits=$(( $(svn status | grep -v "?" -c) ))
if [[ $commits = 0 ]] ; then if [[ $commits = 0 ]] ; then
local ret
ret="${GREEN}${branch}${NO_COL}" ret="${GREEN}${branch}${NO_COL}"
else else
ret="${RED}${branch}${NO_COL}(${YELLOW}$commits${NO_COL})" # changes to commit ret="${RED}${branch}${NO_COL}(${YELLOW}$commits${NO_COL})" # changes to commit
@ -639,11 +674,13 @@ __svn_branch_color()
# returns 2 (and battery level) if battery is charging but under threshold # returns 2 (and battery level) if battery is charging but under threshold
# returns 3 (and battery level) if battery is charging and above threshold # returns 3 (and battery level) if battery is charging and above threshold
# returns 4 if no battery support # returns 4 if no battery support
__battery() _lp_battery()
{ {
command -v acpi >/dev/null 2>&1 || return 4; # or no battery support command -v acpi >/dev/null 2>&1 || return 4; # or no battery support
local acpi="$(acpi --battery 2>/dev/null)" local acpi
local bat=$( echo $acpi | sed "s/^Battery .*, \([0-9]*\)%.*$/\1/") acpi="$(acpi --battery 2>/dev/null)"
local bat
bat=$( echo $acpi | sed "s/^Battery .*, \([0-9]*\)%.*$/\1/")
if [[ -z "${bat}" ]] ; then if [[ -z "${bat}" ]] ; then
# not battery level found # not battery level found
@ -681,13 +718,13 @@ __battery()
# a yellow ⏚ if the battery is charging and under threshold # a yellow ⏚ if the battery is charging and under threshold
# a yellow ⌁ if the battery is discharging but above threshold # a yellow ⌁ if the battery is discharging but above threshold
# a red ⌁ if the battery is discharging and above threshold # a red ⌁ if the battery is discharging and above threshold
__battery_color() _lp_battery_color()
{ {
local mark=$LP_BATTERY_MARK local mark=$LP_BATTERY_MARK
local chargingmark=$LP_ADAPTER_MARK local chargingmark=$LP_ADAPTER_MARK
local bat local bat
local ret local ret
bat=$(__battery) bat=$(_lp_battery)
ret=$? ret=$?
if [[ $ret == 4 || $bat == 100 ]] ; then if [[ $ret == 4 || $bat == 100 ]] ; then
@ -711,6 +748,7 @@ __battery_color()
# discharging and under threshold # discharging and under threshold
elif [[ "$bat" != "" ]] ; then elif [[ "$bat" != "" ]] ; then
local ret
ret="${mark}${NO_COL}" ret="${mark}${NO_COL}"
if [[ ${bat} -le 100 ]] && [[ ${bat} -gt 75 ]] ; then if [[ ${bat} -le 100 ]] && [[ ${bat} -gt 75 ]] ; then
ret="${ret}${GREEN}" ret="${ret}${GREEN}"
@ -726,9 +764,9 @@ __battery_color()
ret="${ret}${CRIT_RED}" ret="${ret}${CRIT_RED}"
fi fi
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
echo -ne "${ret}${bat}%${NO_COL}" echo -ne "${ret}${bat}%${NO_COL}"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
echo -ne "${ret}${bat}%%${NO_COL}" echo -ne "${ret}${bat}%%${NO_COL}"
fi fi
fi fi
@ -740,7 +778,7 @@ __battery_color()
############### ###############
# Compute a gradient of background/forground colors depending on the battery status # Compute a gradient of background/forground colors depending on the battery status
__load_color() _lp_load_color()
{ {
# Colour progression is important ... # Colour progression is important ...
# bold gray -> bold green -> bold yellow -> bold red -> # bold gray -> bold green -> bold yellow -> bold red ->
@ -749,15 +787,18 @@ __load_color()
# Then we have to choose the values at which the colours switch, with # Then we have to choose the values at which the colours switch, with
# anything past yellow being pretty important. # anything past yellow being pretty important.
loadval=$(__load_$OS) local loadval
local load
loadval=$(_lp_load_$LP_OS)
load=$(echo $loadval | sed 's/\.//g;s/^0*//g' ) load=$(echo $loadval | sed 's/\.//g;s/^0*//g' )
if [[ -z "$load" ]] ; then if [[ -z "$load" ]] ; then
load=0 load=0
fi fi
let "load=$load/$__CPUNUM" let "load=$load/$_lp_CPUNUM"
if [[ $load -ge $LP_LOAD_THRESHOLD ]] if [[ $load -ge $LP_LOAD_THRESHOLD ]]
then then
local ret
ret="${LP_LOAD_MARK}${NO_COL}" ret="${LP_LOAD_MARK}${NO_COL}"
if [[ $load -lt 70 ]] ; then if [[ $load -lt 70 ]] ; then
ret="${ret}${FG}" ret="${ret}${FG}"
@ -772,9 +813,9 @@ __load_color()
else else
ret="${ret}${CRIT_RED}" ret="${ret}${CRIT_RED}"
fi fi
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
ret="${ret}$load%${NO_COL}" ret="${ret}$load%${NO_COL}"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
ret="${ret}$load%%${NO_COL}" ret="${ret}$load%%${NO_COL}"
fi fi
echo -ne "${ret}" echo -ne "${ret}"
@ -788,29 +829,31 @@ __load_color()
# Set the prompt mark to ± if git, to ☿ if mercurial, to ‡ if subversion # Set the prompt mark to ± if git, to ☿ if mercurial, to ‡ if subversion
# to # if root and else $ # to # if root and else $
__smart_mark() _lp_smart_mark()
{ {
local COL=${BOLD_FG} local COL
COL=${BOLD_FG}
if [[ "$EUID" -eq "0" ]] ; then if [[ "$EUID" -eq "0" ]] ; then
COL=${BOLD_RED} COL=${BOLD_RED}
fi fi
local mark="\$" local mark
if [[ "$WORKING_SHELL" == "zsh" ]]; then mark="\$"
if [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
mark="%(!.#.$)" mark="%(!.#.$)"
fi fi
if [[ ! -z $(__git_branch) ]] ; then if [[ ! -z $(_lp_git_branch) ]] ; then
mark=$LP_GIT_MARK mark=$LP_GIT_MARK
elif [[ ! -z $(__hg_branch) ]] ; then elif [[ ! -z $(_lp_hg_branch) ]] ; then
mark=$LP_MERCURIAL_MARK mark=$LP_MERCURIAL_MARK
elif [[ ! -z $(__svn_branch) ]] ; then elif [[ ! -z $(_lp_svn_branch) ]] ; then
mark=$LP_SUBVERSION_MARK mark=$LP_SUBVERSION_MARK
fi fi
echo -ne "${COL}${mark}${NO_COL}" echo -ne "${COL}${mark}${NO_COL}"
} }
# insert a space on the right # insert a space on the right
__sr() _lp_sr()
{ {
if [[ ! -z "$1" ]] ; then if [[ ! -z "$1" ]] ; then
echo -n "$1 " echo -n "$1 "
@ -818,7 +861,7 @@ __sr()
} }
# insert a space on the left # insert a space on the left
__sl() _lp_sl()
{ {
if [[ ! -z "$1" ]] ; then if [[ ! -z "$1" ]] ; then
echo -n " $1" echo -n " $1"
@ -826,7 +869,7 @@ __sl()
} }
# insert two space, before and after # insert two space, before and after
__sb() _lp_sb()
{ {
if [[ ! -z "$1" ]] ; then if [[ ! -z "$1" ]] ; then
echo -n " $1 " echo -n " $1 "
@ -838,54 +881,54 @@ __sb()
# Construct the prompt # # Construct the prompt #
######################## ########################
__set_bash_prompt() _lp_set_bash_prompt()
{ {
# as this get the last returned code, it should be called first # as this get the last returned code, it should be called first
__RET=$(__sl "$(__return_value $?)") LP_RET=$(_lp_sl "$(_lp_return_value $?)")
# execute the old prompt # execute the old prompt
$LP_OLD_PROMPT_COMMAND $LP_OLD_PROMPT_COMMAND
# left of main prompt: space at right # left of main prompt: space at right
__JOBS=$(__sr "$(__jobcount_color)") LP_JOBS=$(_lp_sr "$(_lp_jobcount_color)")
__LOAD=$(__sr "$(__load_color)") LP_LOAD=$(_lp_sr "$(_lp_load_color)")
__BATT=$(__sr "$(__battery_color)") LP_BATT=$(_lp_sr "$(_lp_battery_color)")
# in main prompt: no space # in main prompt: no space
__USER=$(__user) LP_USER=$(_lp_user)
__HOST=$(__host_color) LP_HOST=$(_lp_host_color)
__PERM=$(__permissions_color) LP_PERM=$(_lp_permissions_color)
__PWD=$(__shorten_path "$PWD" $LP_PATH_LENGTH) LP_PWD=$(_lp_shorten_path "$PWD" $LP_PATH_LENGTH)
__PROXY=$(__proxy) LP_PROXY=$(_lp_proxy)
# right of main prompt: space at left # right of main prompt: space at left
__GIT=$(__sl "$(__git_branch_color)") LP_GIT=$(_lp_sl "$(_lp_git_branch_color)")
__HG=$(__sl "$(__hg_branch_color)") LP_HG=$(_lp_sl "$(_lp_hg_branch_color)")
__SVN=$(__sl "$(__svn_branch_color)") LP_SVN=$(_lp_sl "$(_lp_svn_branch_color)")
# end of the prompt line: double spaces # end of the prompt line: double spaces
__MARK=$(__sb "$(__smart_mark)") LP_MARK=$(_lp_sb "$(_lp_smart_mark)")
if [[ -z $LP_PS1 ]] ; then if [[ -z $LP_PS1 ]] ; then
# add jobs, load and battery # add jobs, load and battery
PS1="${__BATT}${__LOAD}${__JOBS}" PS1="${LP_BATT}${LP_LOAD}${LP_JOBS}"
# add user, host and permissions colon # add user, host and permissions colon
PS1="${PS1}[${__USER}${__HOST}${NO_COL}${__PERM}" PS1="${PS1}[${LP_USER}${LP_HOST}${NO_COL}${LP_PERM}"
# if not root # if not root
if [[ "$EUID" -ne "0" ]] if [[ "$EUID" -ne "0" ]]
then then
# path in foreground color # path in foreground color
PS1="${PS1}${BOLD_FG}${__PWD}${NO_COL}]${__PROXY}" PS1="${PS1}${BOLD_FG}${LP_PWD}${NO_COL}]${LP_PROXY}"
# add VCS infos # add VCS infos
PS1="${PS1}${__GIT}${__HG}${__SVN}" PS1="${PS1}${LP_GIT}${LP_HG}${LP_SVN}"
else else
# path in yellow # path in yellow
PS1="${PS1}${YELLOW}${__PWD}${NO_COL}]${__PROXY}" PS1="${PS1}${YELLOW}${LP_PWD}${NO_COL}]${LP_PROXY}"
# do not add VCS infos # do not add VCS infos
fi fi
# add return code and prompt mark # add return code and prompt mark
PS1="${PS1}${PURPLE}${__RET}${NO_COL}${__MARK}" PS1="${PS1}${PURPLE}${LP_RET}${NO_COL}${LP_MARK}"
# Glue the bash prompt always go to the first column. # Glue the bash prompt always go to the first column.
# Avoid glitches after interrupting a command with Ctrl-C # Avoid glitches after interrupting a command with Ctrl-C
@ -902,19 +945,19 @@ 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 [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
LP_OLD_PROMPT_COMMAND="$PROMPT_COMMAND" LP_OLD_PROMPT_COMMAND="$PROMPT_COMMAND"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
LP_OLD_PROMPT_COMMAND="$precmd" LP_OLD_PROMPT_COMMAND="$precmd"
else else
echo "${RED}Shell $WORKING_SHELL not supported $NO_COL" echo "${RED}Shell $_LP_WORKING_SHELL not supported $NO_COL"
fi fi
fi fi
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
PROMPT_COMMAND=__set_bash_prompt PROMPT_COMMAND=_lp_set_bash_prompt
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
function precmd { function precmd {
__set_bash_prompt _lp_set_bash_prompt
} }
fi fi
@ -927,9 +970,9 @@ prompt_on()
prompt_off() prompt_off()
{ {
PS1=$LP_OLD_PS1 PS1=$LP_OLD_PS1
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
precmd=$LP_OLD_PROMPT_COMMAND precmd=$LP_OLD_PROMPT_COMMAND
fi fi
} }
@ -938,9 +981,9 @@ prompt_off()
prompt_OFF() prompt_OFF()
{ {
PS1="\$ " PS1="\$ "
if [[ "$WORKING_SHELL" == "bash" ]]; then if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND
elif [[ "$WORKING_SHELL" == "zsh" ]]; then elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
precmd=$LP_OLD_PROMPT_COMMAND precmd=$LP_OLD_PROMPT_COMMAND
fi fi
} }

View File

@ -30,7 +30,7 @@ LP_REVERSE=0
LP_HOSTNAME_ALWAYS=0 LP_HOSTNAME_ALWAYS=0
# If charset is UTF-8 # If charset is UTF-8
if [[ $(locale -k LC_CTYPE | sed -n 's/^charmap="\(.*\)"/\1/p') == *"UTF-8"* ]] ; then if [[ "$(locale -k LC_CTYPE | sed -n 's/^charmap="\(.*\)"/\1/p')" == *"UTF-8"* ]] ; then
LP_BATTERY_MARK="⌁" LP_BATTERY_MARK="⌁"
LP_ADAPTER_MARK="⏚" LP_ADAPTER_MARK="⏚"
LP_LOAD_MARK="⌂" LP_LOAD_MARK="⌂"
@ -47,3 +47,5 @@ else
LP_MERCURIAL_MARK="m" LP_MERCURIAL_MARK="m"
LP_SUBVERSION_MARK="=" LP_SUBVERSION_MARK="="
fi fi
# vim: set ts=4 sw=4 tw=120 ft=sh: