Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created September 7, 2012 23:57
Show Gist options
  • Save pachacamac/3670839 to your computer and use it in GitHub Desktop.
Save pachacamac/3670839 to your computer and use it in GitHub Desktop.
Simple Battery Indicator for Gnome, since Ubuntu 12.04 trolled me, I wrote my own ... fuck you ;)
#!/usr/bin/env python
import sys
import gtk
import appindicator
import re
import commands
PING_FREQUENCY = 30 # seconds
class CheckBattery:
def __init__(self):
self.ind = appindicator.Indicator("simple-battery-indicator",
"battery-empty",
appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.menu_setup()
self.ind.set_menu(self.menu)
def menu_setup(self):
self.menu = gtk.Menu()
self.status_item = gtk.MenuItem("Status")
self.status_item.show()
self.menu.append(self.status_item)
#self.quit_item = gtk.MenuItem("Quit")
#self.quit_item.connect("activate", self.quit)
#self.quit_item.show()
#self.menu.append(self.quit_item)
def main(self):
self.check_battery()
gtk.timeout_add(PING_FREQUENCY * 1000, self.check_battery)
gtk.main()
def quit(self, widget):
sys.exit(0)
def check_battery(self):
# python n00b needs to fall back to ruby ;)
percent, icon, charging, hours = commands.getoutput("""ruby -e '
info = `cat /proc/acpi/battery/BAT1/*`
charging = info.match(/charging state:\s*(\D+)\n/)[1]
full = info.match(/last full capacity:\s*(\d+)/)[1].to_f
current = info.match(/remaining capacity:\s*(\d+)/)[1].to_f
rate = info.match(/present rate:\s*(\d+)/)[1].to_f
percent = (current / full) * 100
bpercent = ((percent / 20).round * 20).to_s
bpercent = "0#{bpercent}" if bpercent.size == 2
hours = (current / rate).round(2)
puts "#{percent.to_i} battery-#{bpercent}#{charging == "charging" ? "-charging" : ""} #{charging} #{hours}"
'""").split()
self.ind.set_icon(icon)
self.status_item.set_label(percent + "% " + charging + "\n" + hours + " hours left")
return True
if __name__ == "__main__":
indicator = CheckBattery()
indicator.main()
@pachacamac
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment