Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Print the battery percentage using colors for High, Medium, Low battery levels. Uses acpitool to parse for battery percentage level.
#!/usr/bin/env python
# coding=UTF-8
import sys, subprocess
p = subprocess.Popen(["acpitool", "-ab"], stdout=subprocess.PIPE)
output = p.communicate()[0]
# find the line with the battery percentage
o_cur = [l for l in output.splitlines() if '%' in l][0]
# parse the battery percentage from acpitool stdout
b_cur = [perc.strip()[:-1] for perc in o_cur.split(',') if '%' in perc][0]
b_val = float(b_cur)
# Output
out = (b_cur + u'%').encode('utf-8')
color_green = '\033[32m'
color_yellow = '\033[33m'
color_red = '\033[31m'
color_reset = '\033[00m'
color_out = (
color_green if b_val > 80
else color_yellow if b_val > 30
else color_red
out = color_out + out + color_reset + '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment