lpr/liquidprompt

928 lines
26 KiB
Plaintext
Raw Normal View History

################################################################################
# LIQUID PROMPT
# An intelligent and non intrusive prompt for bash and zsh
################################################################################
2012-07-07 05:35:58 -04:00
# Licensed under the AGPL version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###########
# AUTHORS #
###########
# nojhan <nojhan@gmail.com> # Main author.
# Aurelien Requiem <aurelien@requiem.fr> # Major clean refactoring, variable path length, error codes, several bugfixes.
# Joris Dedieu <joris@pontiac3.nfrance.com> # Portability framework, FreeBSD support, bugfixes.
# Ludovic Rousseau <ludovic.rousseau@gmail.com> # Lot of bugfixes.
# Yann 'Ze' Richard <ze@nbox.org> # Do not fail on missing commands.
# François Schmidts <fschmidts@olfeo.com> # Simpler SSH_IP acquiring method.
# Thomas Debesse <thomas.debesse@gmail.com> # Fix columns use.
# Florian Le Frioux <florian@lefrioux.fr> # Use ± mark when root in VCS dir.
# Luc Didry <luc@fiat-tux.fr> # Zsh port
# See the README.md file for a summary of features.
WORKING_SHELL=$(ps -p $$ | tail -n1 | awk '{print $NF}')
if [[ "$WORKING_SHELL" == "bash" ]]; then
# Check for recent enough version of bash.
[[ -z "$BASH_VERSION" || -z "$PS1" || -z "$TERM" ]] && return;
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [[ $bmajor -lt 3 ]] || [[ $bmajor -eq 3 && $bminor -lt 2 ]]; then
unset bash bmajor bminor
return
fi
2012-07-17 03:23:44 -04:00
unset bash bmajor bminor
OPENESCAPE="\["
CLOSEESCAPE="\]"
USERSYMBOL="\u"
HOSTSYMBOL="\h"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
OPENESCAPE="%{"
CLOSEESCAPE="%}"
USERSYMBOL="%n"
HOSTSYMBOL="%m"
fi
2012-07-22 16:28:50 -04:00
#################
# CONFIGURATION #
#################
2012-08-04 11:51:13 -04:00
# Default values
LP_BATTERY_THRESHOLD=${LP_BATTERY_THRESHOLD:-75}
LP_LOAD_THRESHOLD=${LP_LOAD_THRESHOLD:-60}
LP_PATH_LENGTH=${LP_PATH_LENGTH:-35}
LP_PATH_KEEP=${LP_PATH_KEEP:-2}
LP_REVERSE=${LP_REVERSE:-0}
LP_HOSTNAME_ALWAYS=${LP_HOSTNAME_ALWAYS:-0}
LP_PS1=${LP_PS1:-""}
# Default config file may be the XDG standard ~/.config/liquidpromptrc,
2012-08-04 11:51:13 -04:00
# but heirloom dotfile has priority.
if [[ -f "$HOME/.liquidpromptrc" ]]
then
configfile="$HOME/.liquidpromptrc"
elif [[ -z "$XDG_HOME_DIR" ]]
then
2012-08-04 11:51:13 -04:00
configfile="$HOME/.config/liquidpromptrc"
else
2012-08-04 11:51:13 -04:00
configfile="$XDG_HOME_DIR/liquidpromptrc"
fi
if [[ -f "$configfile" ]]
then
2012-08-04 11:51:13 -04:00
source "$configfile"
fi
2012-08-04 11:51:13 -04:00
2012-07-22 16:28:50 -04:00
###############
# OS specific #
###############
# OS detection, default to Linux
OS="Linux"
case $(uname) in
2012-07-22 16:28:50 -04:00
"Linux" ) OS="Linux" ;;
"FreeBSD") OS="FreeBSD" ;;
"Darwin") OS="Darwin" ;;
"DragonFly") OS="FreeBSD" ;;
"SunOS") OS="SunOS" ;;
esac
2012-07-22 16:28:50 -04:00
# Colors declarations
if [[ "$OS" == "FreeBSD" ]] ; then
BLACK="${OPENESCAPE}$(tput AF 0)${CLOSEESCAPE}"
BOLD_GRAY="${OPENESCAPE}$(tput md ; tput AF 0)${CLOSEESCAPE}"
WHITE="${OPENESCAPE}$(tput AF 7)${CLOSEESCAPE}"
BOLD_WHITE="${OPENESCAPE}$(tput md ; tput AF 7)${CLOSEESCAPE}"
RED="${OPENESCAPE}$(tput AF 1)${CLOSEESCAPE}"
BOLD_RED="${OPENESCAPE}$(tput md ; tput AF 1)${CLOSEESCAPE}"
WARN_RED="${OPENESCAPE}$(tput AF 0 ; tput setab 1)${CLOSEESCAPE}"
CRIT_RED="${OPENESCAPE}$(tput md; tput AF 7 ; tput setab 1)${CLOSEESCAPE}"
GREEN="${OPENESCAPE}$(tput AF 2)${CLOSEESCAPE}"
BOLD_GREEN="${OPENESCAPE}$(tput md ; tput AF 2)${CLOSEESCAPE}"
YELLOW="${OPENESCAPE}$(tput AF 3)${CLOSEESCAPE}"
BOLD_YELLOW="${OPENESCAPE}$(tput md ; tput AF 3)${CLOSEESCAPE}"
BLUE="${OPENESCAPE}$(tput AF 4)${CLOSEESCAPE}"
BOLD_BLUE="${OPENESCAPE}$(tput md ; tput AF 4)${CLOSEESCAPE}"
PURPLE="${OPENESCAPE}$(tput AF 5)${CLOSEESCAPE}"
PINK="${OPENESCAPE}$(tput md ; tput AF 5)${CLOSEESCAPE}"
CYAN="${OPENESCAPE}$(tput AF 6)${CLOSEESCAPE}"
BOLD_CYAN="${OPENESCAPE}$(tput md ; tput AF 6)${CLOSEESCAPE}"
NO_COL="${OPENESCAPE}$(tput me)${CLOSEESCAPE}"
else
# default to Linux
BLACK="${OPENESCAPE}$(tput setaf 0)${CLOSEESCAPE}"
BOLD_GRAY="${OPENESCAPE}$(tput bold ; tput setaf 0)${CLOSEESCAPE}"
WHITE="${OPENESCAPE}$(tput setaf 7)${CLOSEESCAPE}"
BOLD_WHITE="${OPENESCAPE}$(tput bold ; tput setaf 7)${CLOSEESCAPE}"
RED="${OPENESCAPE}$(tput setaf 1)${CLOSEESCAPE}"
BOLD_RED="${OPENESCAPE}$(tput bold ; tput setaf 1)${CLOSEESCAPE}"
WARN_RED="${OPENESCAPE}$(tput setaf 0 ; tput setab 1)${CLOSEESCAPE}"
CRIT_RED="${OPENESCAPE}$(tput bold; tput setaf 7 ; tput setab 1)${CLOSEESCAPE}"
GREEN="${OPENESCAPE}$(tput setaf 2)${CLOSEESCAPE}"
BOLD_GREEN="${OPENESCAPE}$(tput bold ; tput setaf 2)${CLOSEESCAPE}"
YELLOW="${OPENESCAPE}$(tput setaf 3)${CLOSEESCAPE}"
BOLD_YELLOW="${OPENESCAPE}$(tput bold ; tput setaf 3)${CLOSEESCAPE}"
BLUE="${OPENESCAPE}$(tput setaf 4)${CLOSEESCAPE}"
BOLD_BLUE="${OPENESCAPE}$(tput bold ; tput setaf 4)${CLOSEESCAPE}"
PURPLE="${OPENESCAPE}$(tput setaf 5)${CLOSEESCAPE}"
PINK="${OPENESCAPE}$(tput bold ; tput setaf 5)${CLOSEESCAPE}"
CYAN="${OPENESCAPE}$(tput setaf 6)${CLOSEESCAPE}"
BOLD_CYAN="${OPENESCAPE}$(tput bold ; tput setaf 6)${CLOSEESCAPE}"
NO_COL="${OPENESCAPE}$(tput sgr0)${CLOSEESCAPE}"
fi
# Foreground colors
# can be set to white or black
FG=$WHITE
BOLD_FG=$BOLD_WHITE
if [[ $LP_REVERSE == 1 ]] ; then
FG=$BLACK
BOLD_FG=$BOLD_GRAY
fi
# get cpu number
2012-07-23 10:40:43 -04:00
__cpunum_Linux()
{
grep -c '^[Pp]rocessor' /proc/cpuinfo
}
2012-07-23 10:40:43 -04:00
__cpunum_FreeBSD()
{
sysctl -n hw.ncpu
}
__cpunum_Darwin()
{
__cpunum_FreeBSD
}
2012-07-23 10:40:43 -04:00
__cpunum_SunOS()
{
kstat -m cpu_info | grep "module: cpu_info" | wc -l
}
__CPUNUM=$(__cpunum_$OS)
# get current load
__load_Linux()
{
load=$(awk '{print $1}' /proc/loadavg)
echo -n "$load"
}
__load_FreeBSD()
{
load=$(LANG=C sysctl -n vm.loadavg | awk '{print $2}')
echo -n "$load"
}
__load_Darwin()
{
__load_FreeBSD
}
__load_SunOS()
{
2012-08-11 05:42:29 -04:00
load=$(LANG=C uptime | awk '{print substr($10,0,length($10))}')
echo -n "$load"
}
###############
# Who are we? #
###############
# Yellow for root, light grey if the user is not the login one, else no color.
__user()
{
# if user is not root
if [[ "$EUID" -ne "0" ]] ; then
# if user is not login user
if [[ ${USER} != "$(logname 2>/dev/null)" ]]; then
user="${FG}${USERSYMBOL}${NO_COL}"
else
user="${USERSYMBOL}"
fi
else
user="${BOLD_YELLOW}${USERSYMBOL}${NO_COL}"
fi
echo -ne $user
}
#################
# Where are we? #
#################
__connection()
{
THIS_TTY=tty$(ps aux | grep $$ | grep bash | awk '{ print $7 }')
SESS_SRC=$(who | grep $THIS_TTY | awk '{ print $6 }')
# Are we in an SSH connexion?
SSH_FLAG=0
2012-07-17 05:01:54 -04:00
SSH_IP=${SSH_CLIENT%% *}
if [[ ! -z $SSH_IP ]] ; then
SSH_FLAG=1
fi
SSH2_IP=$(echo $SSH2_CLIENT | awk '{ print $1 }')
if [[ ! -z $SSH2_IP ]] ; then
SSH_FLAG=1
fi
if [[ $SSH_FLAG -eq 1 ]] ; then
CONN="ssh"
elif [[ -z $SESS_SRC ]] ; then
CONN="lcl"
elif [[ $SESS_SRC = "(:0.0)" || $SESS_SRC = "" ]] ; then
CONN="lcl"
else
CONN="tel"
fi
echo -ne $CONN
}
# Put the hostname if not locally connected
# color it in cyan within SSH, and a warning red if within telnet
# else diplay the host without color
__host_color()
{
conn=$(__connection)
ret="${NO_COL}"
if [[ "$conn" == "lcl" ]] ; then
if [[ $LP_HOSTNAME_ALWAYS == 0 ]] ; then
ret="${ret}" # no hostname if local
else
ret="${ret}@${HOSTSYMBOL}"
fi
elif [[ "$conn" == "ssh" ]] ; then
ret="${ret}@${BOLD_CYAN}${HOSTSYMBOL}"
elif [[ "$conn" == "tel" ]] ; then
ret="${ret}@${WARN_RED}${HOSTSYMBOL}"
else
ret="${ret}@${HOSTSYMBOL}"
fi
echo -ne "${ret}${NO_COL}"
}
# put an arrow if an http proxy is set
__proxy()
{
if [[ ! -z "$http_proxy" ]] ; then
echo -ne "↥"
fi
}
# BASH/ZSH function that shortens
# a very long path for display by removing
# the left most parts and replacing them
# with a leading ...
#
# the first argument is the path
#
# the second argument is the maximum allowed
# length including the '/'s and ...
# http://hbfs.wordpress.com/2009/09/01/short-pwd-in-bash-prompts/
#
# + keep some left part of the path if asked
__shorten_path()
{
# the character that will replace the part of the path that is masked
local mask=" … "
# index of the directory to keep from the root (starts at 0 whith bash, 1 with zsh)
local keep=$((LP_PATH_KEEP-1))
if [[ "$WORKING_SHELL" == "zsh" ]]; then
keep=$LP_PATH_KEEP
fi
local len_percent=$2
local p=$(echo "$1" | sed -e "s|$HOME|~|")
local len="${#p}"
2012-08-07 14:59:58 -04:00
local max_len=$((${COLUMNS:-80}*$len_percent/100))
local mask_len="${#mask}"
local slashes=0
2012-08-12 04:08:59 -04:00
if [[ "$WORKING_SHELL" == "bash" ]]; then
if [[ "$len" -gt "$max_len" ]]
then
# finds all the '/' in
# the path and stores their
# positions
#
local pos=()
for ((i=0;i<len;i++))
do
if [[ "${p:i:1}" == "/" ]]
then
pos=(${pos[@]} $i)
slashes=$((${slashes}+1))
fi
done
pos=(${pos[@]} $len)
# we have the '/'s, let's find the
# left-most that doesn't break the
# length limit
#
local i=$keep
if [[ $keep > $slashes ]] ; then
i=$slashes
fi
while [[ "$((len-pos[i]))" -gt "$((max_len-mask_len))" ]]
do
i=$((i+1))
done
# let us check if it's OK to
# print the whole thing
#
if [[ "${pos[i]}" -eq "0" ]]
then
# the path is shorter than
# the maximum allowed length,
# so no need for ...
#
echo "$p"
elif [[ "${pos[i]}" = "$len" ]]
then
# constraints are broken because
# the maximum allowed size is smaller
# than the last part of the path, plus
# ' … '
#
echo "${p:0:((${pos[${keep}]}+1))}${mask}${p:((len-max_len+mask_len))}"
else
# constraints are satisfied, at least
# some parts of the path, plus ' … ', are
# shorter than the maximum allowed size
#
echo "${p:0:((${pos[${keep}]}+1))}${mask}${p:pos[i]}"
fi
2012-08-12 04:08:59 -04:00
else
echo "$p"
fi
2012-08-12 04:08:59 -04:00
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
2012-08-12 04:51:29 -04:00
if [[ "$len" -gt "$max_len" ]]; then
echo "%-${keep}~%${max_len}<${mask}<%~%<<"
else
echo "%~"
fi
fi
}
# Display a ":"
# colored in green if user have write permission on the current directory
# colored in red if it have not.
__permissions_color()
{
2012-08-12 04:52:03 -04:00
if [[ -w "${PWD}" ]]; then
echo "${GREEN}:${NO_COL}"
else
echo "${RED}:${NO_COL}"
fi
}
2012-07-22 16:28:50 -04:00
2012-07-19 05:21:18 -04:00
################
# Related jobs #
################
# Either attached running jobs (started with $ myjob &)
# or attached stopped jobs (suspended with Ctrl-Z)
2012-07-29 16:05:19 -04:00
# or detached screens sessions running on the host
__jobcount_color()
{
2012-07-29 16:05:19 -04:00
local running=$(jobs -r | wc -l | tr -d " ")
local stopped=$(jobs -s | wc -l | tr -d " ")
local screens=$(screen -ls 2> /dev/null | grep -c Detach )
local m_detached="d"
local m_stop="z"
local m_run="&"
2012-07-17 03:23:44 -04:00
# d/&/z
if [[ $screens != "0" && $running != "0" && $stopped != "0" ]] ; then
rep="${NO_COL}${YELLOW}${screens}${m_detached}${NO_COL}/${YELLOW}${running}${m_run}${NO_COL}/${BOLD_YELLOW}${stopped}${m_stop}${NO_COL}"
2012-07-29 16:05:19 -04:00
# _/&/_
elif [[ $screens == "0" && $running != "0" && $stopped == "0" ]] ; then
rep="${NO_COL}${YELLOW}${running}${m_run}${NO_COL}"
2012-07-29 16:05:19 -04:00
# _/_/z
elif [[ $screens == "0" && $running == "0" && $stopped != "0" ]] ; then
rep="${NO_COL}${BOLD_YELLOW}${stopped}${m_stop}${NO_COL}"
2012-07-29 16:05:19 -04:00
# d/_/_
elif [[ $screens != "0" && $running == "0" && $stopped == "0" ]] ; then
rep="${NO_COL}${YELLOW}${screens}${m_detached}${NO_COL}"
2012-07-29 16:05:19 -04:00
# d/&/_
elif [[ $screens != "0" && $running != "0" && $stopped == "0" ]] ; then
rep="${NO_COL}${YELLOW}${screens}${m_detached}${NO_COL}/${YELLOW}${running}${m_run}${NO_COL}"
2012-07-29 16:05:19 -04:00
# d/_/z
elif [[ $screens != "0" && $running == "0" && $stopped != "0" ]] ; then
rep="${NO_COL}${YELLOW}${screens}${m_detached}${NO_COL}/${BOLD_YELLOW}${stopped}${m_stop}${NO_COL}"
# _/&/z
elif [[ $screens == "0" && $running != "0" && $stopped != "0" ]] ; then
rep="${NO_COL}${YELLOW}${running}${m_run}${NO_COL}/${BOLD_YELLOW}${stopped}${m_stop}${NO_COL}"
2012-07-17 03:23:44 -04:00
fi
echo -ne "$rep"
}
2012-07-19 05:21:18 -04:00
# Display the return value of the last command, if different from zero
__return_value()
{
if [[ "$1" -ne "0" ]]
2012-07-19 05:21:18 -04:00
then
echo -ne "$1"
fi
}
2012-07-19 03:59:09 -04:00
######################
2012-07-19 03:59:09 -04:00
# VCS branch display #
######################
2012-07-19 03:59:09 -04:00
# GIT #
# Get the branch name of the current directory
__git_branch()
{
if git rev-parse --git-dir >/dev/null 2>&1 && [[ ! -z "$(git branch)" ]] ; then
2012-07-17 03:23:44 -04:00
echo -n "$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p;')"
fi
}
# Set a color depending on the branch state:
# - green if the repository is up to date
# - yellow if there is some commits not pushed
# - red if there is changes to commit
#
# Add the number of pending commits and the impacted lines.
__git_branch_color()
{
2012-07-17 03:23:44 -04:00
command -v git >/dev/null 2>&1 || return 1;
branch=$(__git_branch)
if [[ ! -z "$branch" ]] ; then
2012-07-17 03:23:44 -04:00
git diff --quiet >/dev/null 2>&1
GD=$?
git diff --cached --quiet >/dev/null 2>&1
GDC=$?
has_commit=$(git rev-list --no-merges --count origin/${branch}..${branch} 2>/dev/null)
if [[ -z "$has_commit" ]] ; then
2012-07-17 03:23:44 -04:00
has_commit=0
2012-07-19 04:49:07 -04:00
fi
if [[ "$GD" -eq 1 || "$GDC" -eq "1" ]] ; then
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
# Changes to commit and commits to push
ret="${RED}${branch}${NO_COL}(${PURPLE}$has_lines${NO_COL},${YELLOW}$has_commit${NO_COL})"
2012-07-17 03:23:44 -04:00
else
ret="${RED}${branch}${NO_COL}(${PURPLE}$has_lines${NO_COL})" # changes to commit
2012-07-17 03:23:44 -04:00
fi
else
if [[ "$has_commit" -gt "0" ]] ; then
2012-07-17 03:23:44 -04:00
# some commit(s) to push
2012-07-19 05:14:42 -04:00
ret="${YELLOW}${branch}${NO_COL}(${YELLOW}$has_commit${NO_COL})"
2012-07-17 03:23:44 -04:00
else
2012-07-19 05:14:42 -04:00
ret="${GREEN}${branch}${NO_COL}" # nothing to commit or push
2012-07-17 03:23:44 -04:00
fi
fi
echo -ne "$ret"
2012-07-17 03:23:44 -04:00
fi
}
2012-07-19 03:59:09 -04:00
# MERCURIAL #
# Get the branch name of the current directory
__hg_branch()
{
branch="$(hg branch 2>/dev/null)"
if [[ $? -eq 0 ]] && [[ ! -z "$(hg branch)" ]] ; then
2012-07-19 03:59:09 -04:00
echo -n "$(hg branch)"
fi
}
# Set a color depending on the branch state:
# - green if the repository is up to date
# - red if there is changes to commit
# - TODO: yellow if there is some commits not pushed
__hg_branch_color()
{
command -v hg >/dev/null 2>&1 || return 1;
branch=$(__hg_branch)
if [[ ! -z "$branch" ]] ; then
if [[ $(hg status --quiet -n | wc -l | sed -e "s/ //g") = 0 ]] ; then
2012-07-19 05:14:42 -04:00
ret="${GREEN}${branch}${NO_COL}"
2012-07-19 03:59:09 -04:00
else
2012-07-19 05:14:42 -04:00
ret="${RED}${branch}${NO_COL}" # changes to commit
2012-07-19 03:59:09 -04:00
fi
echo -ne "$ret"
2012-07-19 03:59:09 -04:00
fi
}
2012-07-19 04:49:07 -04:00
# SUBVERSION #
# Get the branch name of the current directory
# For the first level of the repository, gives the repository name
2012-07-19 04:49:07 -04:00
__svn_branch()
{
infos=$(svn info --xml 2>/dev/null)
ret=$?
if [[ $ret -eq 0 ]] ; then
root=$(echo "$infos" | grep "^<root>" | sed "s/^<root>.*\/\([^\/]*\)<\/root>$/\1/")
subrep=$(echo "$infos" | grep "^<url>" | sed "s/^<url>.*\/$root\/\(.*\)<\/url>$/\1/")
if [[ "$subrep" == *"url>"* ]] ; then
2012-07-19 04:49:07 -04:00
echo -n $root
else
branch=$(basename $subrep)
2012-07-19 04:49:07 -04:00
echo -n $branch
fi
fi
}
# Set a color depending on the branch state:
# - green if the repository is up to date
# - red if there is changes to commit
# Note that, due to subversion way of managing changes,
# informations are only displayed for the CURRENT directory.
2012-07-19 04:49:07 -04:00
__svn_branch_color()
{
command -v svn >/dev/null 2>&1 || return 1;
branch=$(__svn_branch)
if [[ ! -z "$branch" ]] ; then
2012-07-19 04:49:07 -04:00
commits=$(svn status | grep -v "?" | wc -l)
commits=$((commits))
if [[ $commits = 0 ]] ; then
2012-07-19 05:14:42 -04:00
ret="${GREEN}${branch}${NO_COL}"
2012-07-19 04:49:07 -04:00
else
2012-07-19 05:14:42 -04:00
ret="${RED}${branch}${NO_COL}(${YELLOW}$commits${NO_COL})" # changes to commit
2012-07-19 04:49:07 -04:00
fi
echo -ne "$ret"
2012-07-19 04:49:07 -04:00
fi
}
##################
# Battery status #
##################
# Get the battery status in percent
# returns 0 (and battery level) if battery is discharging and under threshold
# returns 1 (and battery level) if battery is discharging and above 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 4 if no battery support
__battery()
{
command -v acpi >/dev/null 2>&1 || return 4; # or no battery support
local acpi="$(acpi --battery 2>/dev/null)"
local bat=$( echo $acpi | sed "s/^Battery .*, \([0-9]*\)%.*$/\1/")
if [[ -z "${bat}" ]] ; then
# not battery level found
return 4
# discharging
elif [[ "$acpi" == *"Discharging"* ]] ; then
if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then
# under threshold
echo -n "${bat}"
return 0
else
# above threshold
echo -n "${bat}"
return 1
fi
# charging
2012-07-17 03:23:44 -04:00
else
if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then
# under threshold
echo -n "${bat}"
return 2
else
# above threshold
echo -n "${bat}"
return 3
fi
2012-07-17 03:23:44 -04:00
fi
}
# Compute a gradient of background/foreground colors depending on the battery status
# Display:
# a green ⌁ if the battery is charging and above threshold
# a yellow ⌁ if the battery is charging and under threshold
# a red ⌁ if the battery is discharging but above threshold
__battery_color()
{
local mark="⌁"
local bat
local ret
2012-07-17 03:23:44 -04:00
bat=$(__battery)
ret=$?
2012-07-17 03:23:44 -04:00
if [[ $ret == 4 || $bat == 100 ]] ; then
# no battery support or battery full: nothing displayed
return
elif [[ $ret == 3 && $bat != 100 ]] ; then
# charging and above threshold and not 100%
# green ⌁
echo -ne "${GREEN}$mark${NO_COL}"
return
elif [[ $ret == 2 ]] ; then
# charging but under threshold
# yellow ⌁
echo -ne "${YELLOW}$mark${NO_COL}"
return
elif [[ $ret == 1 ]] ; then
# discharging but above threshold
# red ⌁
echo -ne "${RED}$mark${NO_COL}"
return
2012-07-17 03:23:44 -04:00
# discharging and under threshold
elif [[ "$bat" != "" ]] ; then
ret="${mark}${NO_COL}"
if [[ ${bat} -le 100 ]] && [[ ${bat} -gt 75 ]] ; then
ret="${ret}${GREEN}"
elif [[ ${bat} -le 75 ]] && [[ ${bat} -gt 50 ]] ; then
ret="${ret}${BOLD_GREEN}"
elif [[ ${bat} -le 50 ]] && [[ ${bat} -gt 20 ]] ; then
ret="${ret}${BOLD_YELLOW}"
elif [[ ${bat} -le 20 ]] && [[ ${bat} -gt 10 ]] ; then
ret="${ret}${BOLD_RED}"
elif [[ ${bat} -le 10 ]] && [[ ${bat} -gt 5 ]] ; then
2012-07-19 05:14:42 -04:00
ret="${ret}${WARN_RED}"
2012-07-17 03:23:44 -04:00
else
2012-07-19 05:14:42 -04:00
ret="${ret}${CRIT_RED}"
2012-07-17 03:23:44 -04:00
fi
if [[ "$WORKING_SHELL" == "bash" ]]; then
echo -ne "${ret}${bat}%${NO_COL}"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
echo -ne "${ret}${bat}%%${NO_COL}"
fi
2012-08-12 04:52:03 -04:00
fi
}
###############
# System load #
###############
# Compute a gradient of background/forground colors depending on the battery status
__load_color()
{
2012-07-17 03:23:44 -04:00
# Colour progression is important ...
# bold gray -> bold green -> bold yellow -> bold red ->
# black on red -> bold white on red
#
# Then we have to choose the values at which the colours switch, with
# anything past yellow being pretty important.
loadval=$(__load_$OS)
load=$(echo $loadval | sed 's/\.//g;s/^0*//g' )
if [[ -z "$load" ]] ; then
2012-07-17 03:23:44 -04:00
load=0
fi
let "load=$load/$__CPUNUM"
if [[ $load -ge $LP_LOAD_THRESHOLD ]]
2012-07-17 03:23:44 -04:00
then
ret="⌂${NO_COL}"
if [[ $load -lt 70 ]] ; then
ret="${ret}${FG}"
elif [[ $load -ge 1 ]] && [[ $load -lt 80 ]] ; then
ret="${ret}${BOLD_GREEN}"
elif [[ $load -ge 80 ]] && [[ $load -lt 95 ]] ; then
ret="${ret}${BOLD_YELLOW}"
elif [[ $load -ge 95 ]] && [[ $load -lt 150 ]] ; then
ret="${ret}${BOLD_RED}"
elif [[ $load -ge 150 ]] && [[ $load -lt 200 ]] ; then
2012-07-19 05:14:42 -04:00
ret="${ret}${WARN_RED}"
2012-07-17 03:23:44 -04:00
else
2012-07-19 05:14:42 -04:00
ret="${ret}${CRIT_RED}"
2012-07-17 03:23:44 -04:00
fi
2012-07-19 05:14:42 -04:00
ret="${ret}$load%${NO_COL}"
2012-07-17 03:23:44 -04:00
echo -ne "${ret}"
fi
}
2012-07-19 05:21:18 -04:00
##########
# DESIGN #
##########
# Set the prompt mark to ± if git, to ☿ if mercurial, to ‡ if subversion
# to # if root and else $
__smart_mark()
{
local COL=${BOLD_FG}
if [[ "$EUID" -eq "0" ]] ; then
COL=${BOLD_RED}
fi
local mark="\$"
if [[ "$WORKING_SHELL" == "zsh" ]]; then
mark="%(!.#.$)"
fi
if [[ ! -z $(__git_branch) ]] ; then
mark="±"
elif [[ ! -z $(__hg_branch) ]] ; then
mark="☿"
elif [[ ! -z $(__svn_branch) ]] ; then
mark="‡"
2012-07-17 03:23:44 -04:00
fi
echo -ne "${COL}${mark}${NO_COL}"
}
2012-07-19 08:17:32 -04:00
# insert a space on the right
2012-07-19 05:14:42 -04:00
__sr()
{
if [[ ! -z "$1" ]] ; then
echo -n "$1 "
2012-07-19 05:14:42 -04:00
fi
}
2012-07-19 08:17:32 -04:00
# insert a space on the left
2012-07-19 05:14:42 -04:00
__sl()
{
if [[ ! -z "$1" ]] ; then
echo -n " $1"
2012-07-19 05:14:42 -04:00
fi
}
2012-07-19 08:17:32 -04:00
# insert two space, before and after
2012-07-19 05:14:42 -04:00
__sb()
{
if [[ ! -z "$1" ]] ; then
echo -n " $1 "
2012-07-17 03:23:44 -04:00
fi
}
2012-07-19 05:21:18 -04:00
########################
# Construct the prompt #
########################
__set_bash_prompt()
{
# as this get the last returned code, it should be called first
__RET=$(__sl "$(__return_value $?)")
# execute the old prompt
$LP_OLD_PROMPT_COMMAND
# left of main prompt: space at right
__JOBS=$(__sr "$(__jobcount_color)")
__LOAD=$(__sr "$(__load_color)")
__BATT=$(__sr "$(__battery_color)")
2012-07-19 05:14:42 -04:00
# in main prompt: no space
__USER=$(__user)
__HOST=$(__host_color)
__PERM=$(__permissions_color)
__PWD=$(__shorten_path "$PWD" $LP_PATH_LENGTH)
__PROXY=$(__proxy)
2012-07-19 05:14:42 -04:00
# right of main prompt: space at left
__GIT=$(__sl "$(__git_branch_color)")
__HG=$(__sl "$(__hg_branch_color)")
__SVN=$(__sl "$(__svn_branch_color)")
2012-07-19 05:14:42 -04:00
# end of the prompt line: double spaces
__MARK=$(__sb "$(__smart_mark)")
2012-07-19 05:14:42 -04:00
if [[ -z $LP_PS1 ]] ; then
# add jobs, load and battery
PS1="${__BATT}${__LOAD}${__JOBS}"
# add user, host and permissions colon
PS1="${PS1}[${__USER}${__HOST}${NO_COL}${__PERM}"
2012-07-19 08:17:32 -04:00
# if not root
if [[ "$EUID" -ne "0" ]]
then
# path in foreground color
PS1="${PS1}${BOLD_FG}${__PWD}${NO_COL}]${__PROXY}"
# add VCS infos
PS1="${PS1}${__GIT}${__HG}${__SVN}"
else
# path in yellow
PS1="${PS1}${YELLOW}${__PWD}${NO_COL}]${__PROXY}"
# do not add VCS infos
fi
# add return code and prompt mark
PS1="${PS1}${PURPLE}${__RET}${NO_COL}${__MARK}"
# Glue the bash prompt always go to the first column.
# Avoid glitches after interrupting a command with Ctrl-C
# Does not seem to be necessary anymore?
#PS1="\[\033[G\]${PS1}${NO_COL}"
2012-07-17 03:23:44 -04:00
else
PS1=$LP_PS1
2012-07-17 03:23:44 -04:00
fi
}
# Activate the liquid prompt
prompt_on()
{
# if liquidprompt has not been already set
if [[ -z "$LP_LIQUIDPROMPT" ]] ; then
LP_OLD_PS1="$PS1"
if [[ "$WORKING_SHELL" == "bash" ]]; then
LP_OLD_PROMPT_COMMAND="$PROMPT_COMMAND"
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
LP_OLD_PROMPT_COMMAND="$precmd"
fi
fi
if [[ "$WORKING_SHELL" == "bash" ]]; then
PROMPT_COMMAND=__set_bash_prompt
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
function precmd {
__set_bash_prompt
}
fi
# Keep in mind that LP has been sourced
# (to avoid recursive prompt command).
LP_LIQUIDPROMPT=1
}
# Come back to the old prompt
prompt_off()
{
PS1=$LP_OLD_PS1
if [[ "$WORKING_SHELL" == "bash" ]]; then
PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
precmd=$LP_OLD_PROMPT_COMMAND
fi
}
# Use an empty prompt: just the \$ mark
prompt_OFF()
{
PS1="\$ "
if [[ "$WORKING_SHELL" == "bash" ]]; then
PROMPT_COMMAND=$LP_OLD_PROMPT_COMMAND
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
precmd=$LP_OLD_PROMPT_COMMAND
fi
}
# By default, sourcing liquidprompt.bash will activate the liquid prompt
prompt_on
# vim: set ts=4 sw=4 tw=120 :