Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My hacked up zsh prompt. Displays info on RVM, git, and battery (on MacBooks). Drop in ~/.oh-my-zsh/custom/themes/ and go
## janky theme
#
# based on https://github.com/robbyrussell/oh-my-zsh/blob/master/themes/blinks.zsh-theme
# certain things cribbed from https://github.com/robbyrussell/oh-my-zsh/blob/master/themes/agnoster.zsh-theme
function _battery_capacity {
ioreg -n AppleSmartBattery -r | \
awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "")}'
}
function _battery_charging_char {
ioreg -n AppleSmartBattery -r | \
awk '$1~/ExternalConnected/{gsub("Yes", "+");gsub("No", "%%"); print substr($0, length, 1)}'
}
function _git_repo_status_char {
setopt promptsubst
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' get-revision true
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' stagedstr '✚'
zstyle ':vcs_info:git:*' unstagedstr '●'
zstyle ':vcs_info:*' formats ' %u%c'
zstyle ':vcs_info:*' actionformats '%u%c'
vcs_info
if [[ $vcs_info_msg_0_ == " " ]]; then
echo -n ''
else
echo $vcs_info_msg_0_
fi
}
function _prompt_char {
prompt_segment $bkg fg "$(_git_repo_status_char; bold '>')"
}
#function _rvm_prompt {
# ${HOME}/.rvm/bin/rvm-prompt
#}
function _ruby_version {
#_rvm_prompt | sed -e 's/^ruby-//' | cut -s -d '-' -f1
}
function _ruby_gemset {
echo -n "$(rbenv gemset active | awk '{ print $1 }')"
}
function _prompt_rbenv {
## TODO: only show if not "system"
echo -n "$(rbenv_prompt_info)@$(_ruby_gemset)"
}
#function _prompt_rvm {
# local version=$(_ruby_version)
# local gemset=$(_ruby_gemset)
# if [[ ! -z $gemset ]]; then
# prompt_segment $bkg green "${version}@${gemset}"
# fi
#}
# Context: user@hostname (who am I and where am I)
function _prompt_context {
local user=`whoami`
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
_prompt_user; _prompt_at; _prompt_hostname
else
echo -n ''
fi
}
function _cwd {
prompt_segment $bkg yellow '%~'
}
function bold() {
local text=$1
echo -n "%B${text}%b"
}
function _prompt_user {
prompt_segment $bkg green '%n'
}
function _prompt_at {
# echo -n '%F{blue}@'
prompt_segment $bkg blue '@'
}
function _prompt_hostname {
prompt_segment $bkg cyan '%m'
}
function _prompt_battery {
prompt_segment $bkg cyan "$(_battery_capacity)$(_battery_charging_char)"
}
function _prompt_git {
prompt_segment $bkg green "$(git_prompt_info)"
}
function blank_line {
echo -n '%{%f%k%b%}'
}
# function blurt() {
# zparseopts b=bold u=underline t:=text
# if [ $bold -eq '-b' ]; then
# echo 'BOLD!!!'
# done
# }
function prompt_segment {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
echo -n "%{%k%b%f%}%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}"
else
echo -n "%{$bg%}%{$fg%}"
fi
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
# This theme works with both the "dark" and "light" variants of the
# Solarized color schema. Set the SOLARIZED_THEME variable to one of
# these two values to choose. If you don't specify, we'll assume you're
# using the "dark" variant.
case ${SOLARIZED_THEME:-dark} in
light) bkg=white;;
*) bkg=black;;
esac
ZSH_THEME_GIT_PROMPT_PREFIX="%B%K{${bkg}%F{green} [%{%B%F{blue}%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{%f%k%b%K{${bkg}}%B%F{green}%}]"
ZSH_THEME_GIT_PROMPT_DIRTY=" %{%F{red}%}*%{%f%k%b%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""
PROMPT='$(blank_line)
$(_prompt_context)$(_cwd)$(git_prompt_info) $(_prompt_rbenv)%E%{%f%k%b%}
$(_prompt_char)%{%f%k%b%} '
RPROMPT=$(_prompt_battery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.