diff options
author | Weitian LI <liweitianux@gmail.com> | 2014-06-13 21:12:21 +0800 |
---|---|---|
committer | Weitian LI <liweitianux@gmail.com> | 2014-06-13 21:12:21 +0800 |
commit | 78f7be4e4b6501b15b5fefc9f7824bbadf0daf2a (patch) | |
tree | b761e28709e4a6f45458c323b8870150827ef831 /.i3/py3status/40-power.py | |
parent | e1ce158f65c472b7c8d14c04fce94c85bc881a6c (diff) | |
download | dotfiles-78f7be4e4b6501b15b5fefc9f7824bbadf0daf2a.tar.bz2 |
Added various dotfiles.
* Xresources
* profile, xprofile, xinitrc
* bash (aliases, logout, profile, rc, completion)
* gitconfig
* gtkrc and gtk-bookmarks
* i3 configs (with py3status configs)
* i3status.conf
* lftp/rc
* tmux.conf
* xbindkeysrc
* mpdconf
* ncmpcpp (config and keys)
* sbclrc
* vifm (vifmrc, colors)
* urxvt (perl extensions)
* conkyrc (and conky/cronograph configs)
Diffstat (limited to '.i3/py3status/40-power.py')
-rw-r--r-- | .i3/py3status/40-power.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/.i3/py3status/40-power.py b/.i3/py3status/40-power.py new file mode 100644 index 0000000..96b8ce5 --- /dev/null +++ b/.i3/py3status/40-power.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Weitian LI <liweitianux@gmail.com> +# 2014/05/15 + +""" +power module for py3status +""" + +## Get output of shell command: +## python 3.x: +## >>> subprocess.getoutput(cmd) +## >>> subprocess.getstatusoutput(cmd) +## python 2.7.x: +## >>> subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) + +import re +import subprocess +import time + + +POSITION = 0 + +BAT_PRESENT = '/sys/class/power_supply/BAT0/present' +# regex to match the '1' at the beginning +one_p = re.compile(r'(^1).*') + + +class Py3status: + """ + Power module to show whether AC/battery is online. + + ac_status: online, offline + bat_status: present, na + """ + + def power(self, i3status_output_json, i3status_config): + """ + Display power status. + """ + response = { + 'full_text': '', + 'name': 'power', + 'instance': 'first', + } + # AC online + ac_cmd = 'on_ac_power' + ac_cmd_out = subprocess.getstatusoutput(ac_cmd) + if ac_cmd_out[0] == 0: + ac_text = '' # Icons: uF237 (ac-online) + self.ac_status = 'online' + response['color'] = i3status_config['color_good'] + else: + ac_text = '' # Icons: uF236 (ac-offline) + self.ac_status = 'offline' + response['color'] = i3status_config['color_bad'] + # Battery present + with open(BAT_PRESENT, 'r') as bat_present_f: + bat_present_c = bat_present_f.read() + bat_m = one_p.match(bat_present_c) + if bat_m.group(1): + bat_text = ' ' # Icons: uF3CF (battery-vertical) + self.bat_status = 'present' + else: + bat_text = '' + self.bat_status = 'na' + # + response['full_text'] = '{0} {1}'.format(ac_text, bat_text) + # cache status for 5 seconds + response['cached_until'] = time.time() + 5 + return (POSITION, response) + |