Skip to content

Instantly share code, notes, and snippets.

@hellekin
Created November 28, 2011 19:43
Show Gist options
  • Save hellekin/1401708 to your computer and use it in GitHub Desktop.
Save hellekin/1401708 to your computer and use it in GitHub Desktop.
not yet a promptinit theme, but pretty nice already
# -*- sh-mode -*-
#
## ZSH PROMPT
#
# {{{ PROMPT SETTINGS
PROMPT_SUBST=1 # Enable prompt expansion
PROMPT_BANG=1 # Enable ! as history event number
PROMPT_PERCENT=1 # Enable % parameter expansion
BATTERY=1 # Display battery state in prompt
BATTERY_INFO="⚡" # Single character battery state
GROUP_INFO="" # Display current group if not primary
# Colored prompt is warmer :)
(( ${+functions[colors]} )) || { autoload -U colors && colors }
# Allow prompt themes
# !! That loads add-zsh-hook
(( ${+functions[promptinit]} )) || \
{ fpath=($ZDOTDIR/prompts $fpath); autoload -Uz promptinit && promptinit }
# }}}
# {{{ PROMPT HELPERS
# {{{ - single character battery status
battery() {
[[ $BATTERY -gt 0 ]] || return 0
local -a battery_chars
local char color percent
percent="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
if [[ -z "$percent" ]] ; then
echo "zsh:battery: missing acpi command." >&2
return 1
fi
battery_chars=( ⚡ ▉ ▇ ▆ ▅ ▄ ▃ ▂ ▁ )
if [[ "$percent" -gt 99 ]] ; then char=1; colr="%F{2}%K{0}"; # green ⚡
elif [[ "$percent" -gt 90 ]] ; then char=2; colr="%F{2}%K{2}";
elif [[ "$percent" -gt 87 ]] ; then char=3; colr="%F{0}%K{2}";
elif [[ "$percent" -gt 75 ]] ; then char=4; colr="%F{0}%K{2}";
elif [[ "$percent" -gt 62 ]] ; then char=5; colr="%F{0}%K{3}"; # yellow
elif [[ "$percent" -gt 50 ]] ; then char=6; colr="%F{0}%K{3}";
elif [[ "$percent" -gt 37 ]] ; then char=7; colr="%F{0}%K{9}"; # orange
elif [[ "$percent" -gt 25 ]] ; then char=8; colr="%F{0}%K{9}";
elif [[ "$percent" -gt 12 ]] ; then char=9; colr="%F{0}%K{1}"; # red
else char=2; colr="%F{1}%K{0}";
fi
BATTERY_INFO="%{$colr%}${battery_chars[$char]}%{%f%k%}"
}
# }}}
# {{{ - display group unless user's primary (Debian-specific)
g_unless_u()
{
local g=$(id -gn)
# GROUP_INFO="%F{1}1%F{2}2%F{3}3%F{4}4%F{5}5%F{6}6%F{7}7%F{8}8%F{9}9%F{10}10" && return 0
[[ "$LOGNAME" == "$g" ]] && GROUP_INFO="" && return 0
GROUP_INFO="%F{13}:%f%b%F{5}${g}%f" # purple : and magenta group
}
# }}}
# {{{ - set PROMPT
prompt_hellekin_prompt()
{
case "$TERM" in
(rxvt*|xterm*)
PROMPT=""
# VCS info on its own line
[[ -n ${vcs_info_msg_0_} ]] && {
PROMPT+=${vcs_info_msg_0_}"
"
}
# (user)% or (user:group)% or (root)#
PROMPT+="%B" # Make it bold
PROMPT+="%(!.%F{1}.%F{13})(%f" # ( red if root, violet otherwise
PROMPT+="%F{9}%n%f${GROUP_INFO}" # user[:group]
PROMPT+="%(!.%F{1}%)%#.%F{13}%)%%)%f" # )# red if root, violet otherwise
PROMPT+="%1(j. %F{yellow}%{⌛%G%}%f.)" # yellow hourglass if there are jobs
PROMPT+="%(?.. [%F{yellow}%?%f])" # yellow [exitcode command] if non-zero
PROMPT+="%b " # remove bold
;;
(dumb|*)
PROMPT="%n@%m %B%4~%b%# " # Emacs friendly
;;
esac
return 0
}
# }}}
# {{{ - precmd() hook
prompt_hellekin_precmd()
{
[[ $NOPRECMD -gt 0 ]] && return 0
# just use DONTSETRPROMPT=1 to be able to overwrite RPROMPT
if [[ $DONTSETRPROMPT -eq 0 ]] ; then
RPROMPT="%B%F{blue}%4~%F@%F{green}%m%f %T%b"
# update battery information
if [[ $BATTERY -gt 0 ]] ; then
battery
RPROMPT+=" ${BATTERY_INFO}"
fi
# update project info
if [ -n "$PROJECT" ]; then
RPROMPT="%B%F{9}$PROJECT%f project%b %B%F{7}|%f%b $RPROMPT"
fi
fi
# Update PROMPT
g_unless_u; prompt_hellekin_prompt
}
add-zsh-hook precmd prompt_hellekin_precmd
# }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment