zsh
prompt_manoj_help () {
cat <<'EOF'
This prompt is not really color themable, I just use this in case I
ever decide to go the angry fruit salad route. This is also unlikely
to be a prompt that is very interesting; interesting prompts are, to
me, a distraction.
What I need from a prompt is simplicity; and the information that is
useful is user name, host, and path of the current directory, in a
title in the xterm, or s shortish string in a console.
Colors are present for the case of a laptop, then, if the battery is
discharging, then the prompt color should change with the state of the
battery.
EOF
}
LISTPROMPT=''
SPROMPT='zsh: correct '%R' to '%r' ? ([Y]es/[N]o/[E]dit/[A]bort) '
typeset -Ag ansi_colors other_colors term_color_fg term_color_bg
ansi_colors=(
black 0 yellow 3 cyan 6
red 1 blue 4 white 7
green 2 magenta 5
)
other_colors=(
black 0 cyan 3 yellow 6
blue 1 red 4 white 7
green 2 magenta 5
)
if [[ -n "$terminfo[setaf]" ]]; then
for k in ${(k)ansi_colors}; do
term_color_fg[$k]=$(echoti setaf $ansi_colors[$k])
term_color_bg[$k]=$(echoti setab $ansi_colors[$k])
done
elif [[ -n "$terminfo[setf]" ]]; then
for k in ${(k)ansi_colors}; do
term_color_fg[$k]=$(echoti setf $other_colors[$k])
term_color_bg[$k]=$(echoti setb $other_colors[$k])
done
else
for k in ${(k)ansi_colors}; do
term_color_fg[$k]=
term_color_bg[$k]=
done
fi
APM="/usr/bin/apm"
prompt_manoj_apm_percent () {
state_line=$($APM)
cut_leading=${state_line#*: }
APM_PERCENT=${cut_leading%%%*}
echo $APM_PERCENT
}
prompt_manoj_apm_charge () {
case "$($APM)" in
*on-line*)
APM_CHARGE="+" ;;
*off-line*)
APM_CHARGE="-" ;;
esac
echo $APM_CHARGE
}
prompt_manoj_echo_apm () {
echo -n "($(prompt_manoj_apm_charge)$(prompt_manoj_apm_percent)) "
}
prompt_manoj_apm_color () {
if [ "$(prompt_manoj_apm_charge)" = "+" ]; then
echo "B%$term_color_fg[cyan]"
else
case $(prompt_manoj_apm_percent) in
10?) echo "$term_color_fg[white]%B" ;;
9?) echo "$term_color_fg[green]" ;;
8?) echo "$term_color_fg[green]" ;;
7?) echo "$term_color_fg[green]" ;;
6?) echo "$term_color_fg[blue]" ;;
5?) echo "$term_color_fg[blue]" ;;
4?) echo "$term_color_fg[blue]" ;;
3?) echo "$term_color_fg[yellow]%B" ;;
2?) echo "$term_color_fg[yellow]%B" ;;
1?) echo "$term_color_fg[red]" ;;
?) echo "$term_color_fg[red]%B${terminfo[blink]}" ;;
*) echo "$term_color_fg[magenta]" ;;
esac
fi
}
prompt_manoj_acpi_percentage () {
local designed last_full current d l c percent
last_full=0
current=0
for battery in /proc/acpi/battery/BAT?; do
l=$(egrep '^last full capacity:' $battery/info 2>/dev/null | \
sed -e 's/^last full capacity: *//' -e 's/ *mAh.*$//')
c=$(egrep '^remaining capacity:' $battery/state 2>/dev/null | \
sed -e 's/^remaining capacity: *//' -e 's/ *mAh.*$//')
(( last_full = last_full + l ))
(( current = current + c ))
done
percent=100
if [[ -n "$last_full" && $last_full -gt 0 ]]; then
if [[ $current -lt $last_full ]]; then
((percent = 100 * current / last_full ))
fi
fi
echo $percent
}
prompt_manoj_acpi_color () {
if [[ ! -e /proc/acpi/battery/BAT0 ]]; then
echo "%B$term_color_fg[green]"
return
fi
local state
result=
for battery in /proc/acpi/battery/BAT?; do
state=$(egrep '^charging state:' $battery/state | sed -e 's/^charging state: *//')
case state in
charged)
result=${result:=charged}
;;
discharging)
result=discharging
;;
charging)
result=charging
;;
*)
esac
done
if [[ "$result" == "charged" ]]; then
echo -n "%B$term_color_fg[white]"
return
elif [[ "$result" == "charging" ]]; then
echo -n "B%$term_color_fg[cyan]"
return
fi
case $(prompt_manoj_acpi_percentage) in
10?) echo -n "%B$term_color_fg[white]" ;;
9?) echo -n "$term_color_fg[green]" ;;
8?) echo -n "$term_color_fg[green]" ;;
7?) echo -n "$term_color_fg[green]" ;;
6?) echo -n "$term_color_fg[blue]" ;;
5?) echo -n "$term_color_fg[blue]" ;;
4?) echo -n "$term_color_fg[blue]" ;;
3?) echo -n "%B$term_color_fg[yellow]" ;;
2?) echo -n "%B$term_color_fg[yellow]" ;;
1?) echo -n "$term_color_fg[red]" ;;
?) echo -n "%B${terminfo[blink]}$term_color_fg[red]" ;;
*) echo -n "$term_color_fg[magenta]" ;;
esac
}
prompt_manoj_chpwd () {
setopt noxtrace localoptions
if [ "$TERM" = "xterm" ] || [ "$TERM" = "xterm-debian" ] ||
[ "$TERM" = "Eterm" ] || [ "$TERM" = "xterm-256color" ]; then
workdir=$(print -P '%30<<%~')
if [[ $#workdir -gt 25 ]]; then
workdir=${workdir/[^\/]#/...}
fi
case $TERM in
sun-cmd)
print -n "\e]l$psvar[4]$workdir\e\\"
;;
screen)
print -n "\ek"$psvar[4]$workdir"\e"\\\ ;
;;
*xterm*|rxvt|(dt|k|E|x)term)
print -n "\e]2;$psvar[4]$workdir\a"
;;
*)
:
esac
else
workdir=$(print -P '%20<<%~')
if [[ $#workdir -gt 18 ]]; then
workdir=${workdir/[^\/]#/...}
fi
fi
if command -v less >/dev/null 2>&1; then
ls -asCF --color=always | less -ELQ; else
ls -asCF | more -ds;
fi
}
__sub_dir() {
local sub_dir
sub_dir=$(readlink -f "${PWD}")
sub_dir=${sub_dir#$1}
echo ${sub_dir#/}
}
__arch_dir() {
base_dir=$(tla tree-root 2>/dev/null) || return 1
base_dir=$(readlink -f "$base_dir")
sub_dir=$(__sub_dir "${base_dir}")
branch=$(tla tree-id)
branch=${branch##[^/]*/}
vcs="arch"
}
__bzr_dir() {
local branch_nick revno
base_dir=$(bzr info 2>/dev/null) || return 1
base_dir=$(echo "$base_dir" | sed -rne 's, *branch root: ,,p')
if [[ "$base_dir" == "." ]]; then
base_dir=$PWD
else
base_dir=$(readlink -f "$base_dir")
fi
sub_dir=$(__sub_dir "${base_dir}")
bzr version-info | while read i j; do
case "$i" in
revno:) revno="$j";;
branch-nick:) branch_nick="$j";;
esac
done
branch="bzr:${branch}@$revno"
vcs=bzr
}
__git_dir() {
base_dir=$(git-rev-parse --show-cdup 2>/dev/null) || return 1
if [[ -n "$base_dir" ]]; then
base_dir=$(readlink -f "$base_dir")
else
base_dir=$PWD
fi
sub_dir=$(git-rev-parse --show-prefix)
sub_dir=${sub_dir%/}
branch=$(git-symbolic-ref -q HEAD || git-name-rev --name-only HEAD 2>/dev/null)
if test -d "$base_dir/../.dotest"; then
if test -f "$base_dir/../.dotest/rebasing"; then
vcs_op="rebase"
elif test -f "$base_dir/../.dotest/applying"; then
vcs_op="am"
else
vcs_op="am/rebase"
fi
elif test -f "$base_dir/.dotest-merge/interactive"; then
vcs_op="rebase -i"
branch="$(cat "$base_dir/.dotest-merge/head-name")"
elif test -d "$base_dir/.dotest-merge"; then
vcs_op="rebase -m"
branch="$(cat "$base_dir/.dotest-merge/head-name")"
elif test -f "$base_dir/MERGE_HEAD"; then
vcs_op="merge"
else
if test -f "$base_dir/BISECT_LOG"; then
vcs_op="bisect"
branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
branch="$(cut -c1-7 "$base_dir/HEAD")..."
fi
fi
dirty=''
branch=${branch#refs/heads/}
if (! ((git diff --cached --quiet) && (git diff --quiet))); then
dirty='yes'
else
dirty=''
fi
vcs="git"
}
__svn_dir() {
[[ -d ".svn" ]] || return 1
base_dir="."
while [[ -d "$base_dir/../.svn" ]]; do base_dir="$base_dir/.."; done
base_dir=$(readlink -f "$base_dir")
sub_dir=$(__sub_dir "${base_dir}")
branch=$(svn info "$base_dir" | awk '/^URL/ { sub(".*/","",$0); r=$0 } /^Revision/ { sub("[^0-9]*","",$0); print r":"$0 }')
vcs="svn"
}
__svk_dir() {
[[ -f ~/.svk/config ]] || return 1
base_dir=$(awk '/: *$/ { sub(/^ */,"",$0); sub(/: *$/,"",$0); if (match("'${PWD}'", $0"(/|$)")) { print $0; d=1; } } /depotpath/ && d == 1 { sub(".*/","",$0); r=$0 } /revision/ && d == 1 { print r ":" $2; exit 1 }' ~/.svk/config) && return 1
branch=${base_dir##*
}
base_dir=${base_dir%%
*}
sub_dir=$(__sub_dir "${base_dir}")
vcs="svk"
}
__hg_dir() {
base_dir=$(hg root 2>/dev/null) || return 1
base_dir=$(readlink -f "$base_dir")
sub_dir=$(__sub_dir "${base_dir}")
branch="hg:$(hg branch)"
vcs="hg"
}
__vcs_get_prompt_path_components()
{
if [ -z "${1:-}" ]; then
pwdnamed="%${_PROMPT_PATH_MAXLEN}<..<%~%<<"
pwdnamed="${(%)pwdnamed}"
echo "$pwdnamed"
return
fi
local reporoot branch workdir
workdir=''
reporoot="${1%%/}"
branch="$2"
local pwdnamed="%~"
pwdnamed="${(%)pwdnamed}"
typeset -la apwd apwdnamed areporoot
apwd=(${(s:/:)PWD})
apwdnamed=(${(s:/:)pwdnamed})
areporoot=(${(s:/:)reporoot})
local precomps postcomps
postcomps=$(($#apwd - $#areporoot))
precomps=$(($#apwdnamed - $postcomps))
local postfix
if (( $postcomps > 0 )); then
postfix="%${postcomps}~"
postfix="${(%)postfix}"
fi
local prelen minlen prefix
prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix))
minlen=${_PROMPT_PATH_MINLEN:-10}
(( $prelen < $minlen )) && prelen=$minlen
prefix="%${prelen}<..<%-${precomps}~%<<"
prefix="${(%)prefix}"
echo "$prefix" "$branch" "$postfix"
}
__vcs_set_prompt_variables()
{
workdir=''
vcs=''
dirty=''
psvar=("${(%)p}")
__git_dir || __arch_dir || __svn_dir || __bzr_dir if [[ -z "$vcs" ]]; then
local p="%${MAXLEN}<..<%~%<<"
psvar=("${(%)p}")
return
fi
set -- $(__vcs_get_prompt_path_components "$base_dir" "$branch")
psvar[1]="$1"
if [[ -n "$vcs_op" ]]; then
psvar[2]="[$2::$vcs_op]"
else
psvar[2]="[$2]"
fi
psvar[3]="$3"
psvar[4]=$(print -P "%n\\\@%m:")
if [[ -n "$dirty" ]]; then
psvar[5]="$dirty"
fi
}
if [ $(id -u) != 0 ]; then
_update_vcs_prompt_vars_if_vcs_ran() {
case "$(history $(($HISTCMD - 1)))" in
*${vcs}*) __vcs_set_prompt_variables "$vcs"
esac
}
precmd_functions+=(_update_vcs_prompt_vars_if_vcs_ran)
_update_vcs_prompt_vars() {
__vcs_set_prompt_variables
}
chpwd_functions+=(_update_vcs_prompt_vars)
_update_vcs_prompt_vars
fi
prompt_manoj_precmd () {
setopt noxtrace localoptions
}
prompt_manoj_setup () {
PS2="%_> "
PS3="?# "
PS4="+%N:%i> "
RPS1='%~'
RPS2='<%^'
if [[ -d /proc/apm ]]; then battery_color () { prompt_manoj_apm_color }
elif [[ -d /proc/acpi ]]; then battery_color () { prompt_manoj_acpi_color }
else battery_color () { echo "$term_color_fg[green]" }
fi
if [[ -n "$terminfo[rev]" ]]; then rev_vid="$terminfo[rev]";
else rev_vid=; fi
if [[ -n "$terminfo[dim]" ]]; then rev_vid="$terminfo[dim]";
else rev_vid=; fi
if [[ -n "$terminfo[sgr0]" ]]; then norm_vid="$terminfo[sgr0]";
else norm_vid=; fi
case $TERM in
emacs|dumb)
PS1="%(!,#,_)_> "
;;
*xterm*|rxvt|screen|sun-cmd|(dt|k|E)term)
PS1="%(5v,%{$term_color_fg[magenta]%},)%(2v.%B%2v%b.)%{$dim_vid$(battery_color)%}%(?,_,%?)%(!,#,_)>%{$norm_vid%} "
RPS1="%{$dim_vid$(battery_color)%}%40<...<%~%{$reset_color%}"
;;
*)
PS1="%(5v,%{$term_color_fg[magenta]%},)%(2v.%B%2v%b.)%u%{$dim_vid$(battery_color)%}%(?,_,%?)%(!,#,_)>%{$norm_vid%} "
RPS1="%{$dim_vid$(battery_color)%}%40<...<%~%{$reset_color%}"
esac
precmd () { }
preexec () { }
chpwd () { prompt_manoj_chpwd }
}
prompt_manoj_setup "$@"