Skip to content

Instantly share code, notes, and snippets.

@ilkka
Created June 25, 2014 07:35
Show Gist options
  • Save ilkka/4d4ddfd3beb0b94e6e96 to your computer and use it in GitHub Desktop.
Save ilkka/4d4ddfd3beb0b94e6e96 to your computer and use it in GitHub Desktop.
Linux / Mac battery level in zsh right side prompt
...
if [[ -r $HOME/.zshstuff/batterylevel.py ]]; then
RPROMPT="$RPROMPT $(python $HOME/.zshstuff/batterylevel.py)"
fi
# Bonus! Indicator of stopped (^Z'd) jobs
function stopped_jobs(){
if [[ "$(jobs)" =~ "suspended" ]]; then
echo "%{$fg_bold[red]%}⌚ " # this is supposed to be a clock symbol
fi
}
RPROMPT="\$(stopped_jobs)$RPROMPT"
#!/usr/bin/env python
# coding=UTF-8
import math, subprocess, sys
if sys.platform == 'linux' or sys.platform == 'linux2':
try:
b_max = float(file('/sys/class/power_supply/BAT0/energy_full').read().strip())
b_cur = float(file('/sys/class/power_supply/BAT0/energy_now').read().strip())
except IOError:
sys.exit(0)
elif sys.platform == 'darwin':
p = subprocess.Popen(["ioreg", "-rc", "AppleSmartBattery"], stdout=subprocess.PIPE)
output = p.communicate()[0]
o_max = [l for l in output.splitlines() if 'MaxCapacity' in l][0]
o_cur = [l for l in output.splitlines() if 'CurrentCapacity' in l][0]
b_max = float(o_max.rpartition('=')[-1].strip())
b_cur = float(o_cur.rpartition('=')[-1].strip())
else:
sys.exit(0)
charge = b_cur / b_max
charge_threshold = int(math.ceil(10 * charge))
# Output
total_slots, slots = 10, []
filled = int(math.ceil(charge_threshold * (total_slots / 10.0))) * u'▸'
empty = (total_slots - len(filled)) * u'▹'
out = (filled + empty).encode('utf-8')
import sys
# these are supposed to be right-facing filled triangles...
color_green = '%{%}'
color_yellow = '%{%}'
color_red = '%{%}'
color_reset = '%{%}'
color_out = (
color_green if len(filled) > 6
else color_yellow if len(filled) > 4
else color_red
)
out = color_out + out + color_reset
sys.stdout.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment