added battery prompt segment usable on OS X systems

pull/22/head
rjorgenson 9 years ago
parent d46634a0a7
commit acf5e97756

@ -71,6 +71,7 @@ configuration is the default:
The segments that are currently available are: The segments that are currently available are:
* [aws](#aws) - The current AWS profile, if active. * [aws](#aws) - The current AWS profile, if active.
* [battery](#battery) - Current battery status(OS X only).
* [context](#context) - Your username and host. * [context](#context) - Your username and host.
* [dir](#dir) - Your current working directory. * [dir](#dir) - Your current working directory.
* **history** - The command number for the current line. * **history** - The command number for the current line.
@ -101,6 +102,22 @@ your `~/.zshrc`:
export AWS_DEFAULT_PROFILE=<profile_name> export AWS_DEFAULT_PROFILE=<profile_name>
##### battery
This segment will display your current battery status on an OS X system(fails gracefully
on systems without a battery and non OS X systems). It can be customized in your .zshrc
with the environment variables detailed below with their default values.
POWERLEVEL9K_BATTERY_CHARGING="yellow"
POWERLEVEL9K_BATTERY_CHARGED="green"
POWERLEVEL9K_BATTERY_DISCONNECTED=$DEFAULT_COLOR
POWERLEVEL9K_BATTERY_LOW_THRESHOLD=10
POWERLEVEL9K_BATTERY_LOW_COLOR="red"
In addition to the above it supports standard _FOREGROUND value without affecting the icon color
##### context ##### context
The `context` segment (user@host string) is conditional. This lets you enable it, but only display The `context` segment (user@host string) is conditional. This lets you enable it, but only display

@ -46,6 +46,7 @@ case $POWERLEVEL9K_MODE in
BACKGROUND_JOBS_ICON $'\UE82F ' #  BACKGROUND_JOBS_ICON $'\UE82F ' # 
TEST_ICON $'\UE891' #  TEST_ICON $'\UE891' # 
TODO_ICON $'\U2611' # ☑ TODO_ICON $'\U2611' # ☑
BATTERY_ICON $'\UE894' # 
OK_ICON $'\U2713' # ✓ OK_ICON $'\U2713' # ✓
FAIL_ICON $'\U2718' # ✘ FAIL_ICON $'\U2718' # ✘
SYMFONY_ICON 'SF' SYMFONY_ICON 'SF'
@ -95,6 +96,7 @@ case $POWERLEVEL9K_MODE in
BACKGROUND_JOBS_ICON $'\UF013 ' #  BACKGROUND_JOBS_ICON $'\UF013 ' # 
TEST_ICON $'\UF291' #  TEST_ICON $'\UF291' # 
TODO_ICON $'\U2611' # ☑ TODO_ICON $'\U2611' # ☑
BATTERY_ICON $'\u1F50B' # 🔋
OK_ICON $'\UF23A' #  OK_ICON $'\UF23A' # 
FAIL_ICON $'\UF281' #  FAIL_ICON $'\UF281' # 
SYMFONY_ICON 'SF' SYMFONY_ICON 'SF'
@ -139,6 +141,7 @@ case $POWERLEVEL9K_MODE in
BACKGROUND_JOBS_ICON $'\u2699' # ⚙ BACKGROUND_JOBS_ICON $'\u2699' # ⚙
TEST_ICON '' TEST_ICON ''
TODO_ICON $'\U2611' # ☑ TODO_ICON $'\U2611' # ☑
BATTERY_ICON $'\u1F50B' # 🔋
OK_ICON $'\u2713' # ✓ OK_ICON $'\u2713' # ✓
FAIL_ICON $'\u2718' # ✘ FAIL_ICON $'\u2718' # ✘
SYMFONY_ICON 'SF' SYMFONY_ICON 'SF'
@ -611,6 +614,48 @@ prompt_aws() {
fi fi
} }
prompt_battery() {
if [[ -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then
# return if there is no battery on system
[[ -z $(ioreg -n AppleSmartBattery | grep MaxCapacity) ]] && return
# set default values of not specified in shell
[[ -z $POWERLEVEL9K_BATTERY_CHARGING ]] && POWERLEVEL9K_BATTERY_CHARGING="yellow"
[[ -z $POWERLEVEL9K_BATTERY_CHARGED ]] && POWERLEVEL9K_BATTERY_CHARGED="green"
[[ -z $POWERLEVEL9K_BATTERY_DISCONNECTED ]] && POWERLEVEL9K_BATTERY_DISCONNECTED="$DEFAULT_COLOR"
[[ -z $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && POWERLEVEL9K_BATTERY_LOW_THRESHOLD=10
[[ -z $POWERLEVEL9K_BATTERY_LOW_COLOR ]] && POWERLEVEL9K_BATTERY_LOW_COLOR="red"
[[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local fg_color="$DEFAULT_COLOR" || local fg_color=$POWERLEVEL9K_BATTERY_FOREGROUND
# get charge status
[[ $(ioreg -n AppleSmartBattery | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]] && local connected=true
[[ $(ioreg -n AppleSmartBattery | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]] && local charging=true
# convert time remaining from minutes to hours:minutes date string
local time_remaining=$(ioreg -n AppleSmartBattery | grep TimeRemaining | awk '{ print $5 }')
if [[ ! -z $time_remaining ]]; then
# this value is set to a very high number when the system is calculating
[[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(date -u -r $(($time_remaining * 60)) +%k:%M)}
fi
# get charge values
local max_capacity=$(ioreg -n AppleSmartBattery | grep MaxCapacity | awk '{ print $5 }')
local current_capacity=$(ioreg -n AppleSmartBattery | grep CurrentCapacity | awk '{ print $5 }')
# logic for string output
[[ ! -z $max_capacity && ! -z $current_capacity ]] && local bat_percent=$(ruby -e "puts ($current_capacity.to_f / $max_capacity.to_f * 100).round.to_i")
[[ $charging =~ true && $connected =~ true ]] && local conn="%F{$POWERLEVEL9K_BATTERY_CHARGING}" && local remain="($tstring)"
[[ ! $charging =~ true && $connected =~ true ]] && local conn="%F{$POWERLEVEL9K_BATTERY_CHARGED}" && local remain=""
if [[ ! $connected =~ true ]]; then
[[ $bat_percent -lt $POWERLVEL9K_BATTERY_LOW_THRESHOLD ]] && local conn="%F{$POWERLEVEL9K_BATTERY_LOW_COLOR}" || local conn="%F{$POWERLEVEL9K_BATTERY_DISCONNECTED}"
local remain="($tstring)"
fi
# display prompt_segment
[[ ! -z $bat_percent ]] && "$1_prompt_segment" "$0" "black" "$DEFAULT_COLOR" "$conn$(print_icon 'BATTERY_ICON'){$fg_color} $bat_percent%% $remain"
fi
}
# Context: user@hostname (who am I and where am I) # Context: user@hostname (who am I and where am I)
# Note that if $DEFAULT_USER is not set, this prompt segment will always print # Note that if $DEFAULT_USER is not set, this prompt segment will always print
prompt_context() { prompt_context() {
@ -965,7 +1010,7 @@ powerlevel9k_init() {
fi fi
setopt prompt_subst setopt prompt_subst
setopt LOCAL_OPTIONS setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS unsetopt XTRACE KSH_ARRAYS
setopt PROMPT_CR PROMPT_PERCENT PROMPT_SUBST MULTIBYTE setopt PROMPT_CR PROMPT_PERCENT PROMPT_SUBST MULTIBYTE

Loading…
Cancel
Save