1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# -*- coding: utf8 -*-
"""
Module for displaying information about battery.
Requires:
- the 'acpi' command line
@author shadowprince and AdamBSteele
@license Eclipse Public License
"""
from __future__ import division # python2 compatibility
from time import time
import math
import subprocess
BLOCKS = ["_", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]
CHARGING_CHARACTER = "⚡"
EMPTY_BLOCK_CHARGING = '|'
EMPTY_BLOCK_DISCHARGING = '⍀'
FULL_BLOCK = '█'
class Py3status:
"""
Configuration parameters:
- color_* : None means - get it from i3status config
- format : text with "text" mode. percentage with % replaces {}
- hide_when_full : hide any information when battery is fully charged
- mode : for primitive-one-char bar, or "text" for text percentage ouput
"""
# available configuration parameters
cache_timeout = 30
color_bad = None
color_charging = "#FCE94F"
color_degraded = None
color_good = None
format = "Battery: {}"
hide_when_full = False
mode = "bar"
notification = False
def battery_level(self, i3s_output_list, i3s_config):
response = {}
# Example acpi raw output: "Battery 0: Discharging, 43%, 00:59:20 remaining"
acpi_raw = subprocess.check_output(["acpi"], stderr=subprocess.STDOUT)
acpi_clean = acpi_raw.translate(None, ',')
# Example list: ['Battery', '0:', 'Discharging', '43%', '00:59:20', 'remaining']
acpi_list = acpi_clean.split(' ')
charging = True if acpi_list[2] == "Charging" else False
percent_charged = int(acpi_list[3].translate(None, '%'))
self.time_remaining = ' '.join(acpi_list[4:])
battery_full = False
if self.mode == "bar":
if charging:
full_text = CHARGING_CHARACTER
else:
full_text = BLOCKS[int(math.ceil(percent_charged/100*(len(BLOCKS) - 1)))]
elif self.mode == "ascii_bar":
full_part = FULL_BLOCK * int(percent_charged/10)
if charging:
empty_part = EMPTY_BLOCK_CHARGING * (10 - int(percent_charged/10))
else:
empty_part = EMPTY_BLOCK_DISCHARGING * (10 - int(percent_charged/10))
full_text = full_part + empty_part
else:
full_text = self.format.format(str(percent_charged) + "%")
response['full_text'] = full_text
if percent_charged < 30:
response['color'] = (
self.color_degraded
if self.color_degraded
else i3s_config['color_degraded']
)
if percent_charged < 10:
response['color'] = (
self.color_bad
if self.color_bad
else i3s_config['color_bad']
)
if battery_full:
response['color'] = (
self.color_good
if self.color_good
else i3s_config['color_good']
)
response['full_text'] = "" if self.hide_when_full else BLOCKS[-1]
elif charging:
response['color'] = self.color_charging
response['cached_until'] = time() + self.cache_timeout
return response
def on_click(self, i3s_output_list, i3s_config, event):
"""
Display a notification with the remaining charge time.
"""
if self.notification and self.time_remaining:
subprocess.call(
['notify-send', '{}'.format(self.time_remaining), '-t', '4000'],
stdout=open('/dev/null', 'w'),
stderr=open('/dev/null', 'w')
)
if __name__ == "__main__":
from time import sleep
x = Py3status()
while True:
print(x.battery_level([], {}))
sleep(1)
|