lemonbar: Add units to modules.

Units are sections of the lemonbar, highlighted in alternating colors
This commit is contained in:
kuben
2019-08-23 17:51:40 +02:00
parent a88a619702
commit c12d49f433
3 changed files with 206 additions and 96 deletions

View File

@@ -6,13 +6,6 @@ import i3_lemonbar_common as common
displays = ''
workspaces = ''
win_title = ''
sys_load = ''
net_load = ''
volume = ''
brightness = ''
battery = ''
date = ''
language = ''
time = ''
response = ''
@@ -23,6 +16,79 @@ slr = conf.sep_l_right
sl = conf.sep_left
sll = conf.sep_l_left
class LemonUnit:
def __init__(self, name, action = None, alt_scheme=None, external=None):
self.name = name
self.action = action
self.alt_scheme = alt_scheme # None means default
self.external = external
self.items = [] # List of tuples (icon, text)
class LemonParser:
# Handle parsing of units
# Apply alternating colors
# Contains list of units
def __init__(self):
self.units = []
def register_unit(self, unit):
self.units.append(unit)
def remove_unit(self, unit):
self.remove(unit)
def format(self):
formatted = []
# Iterate over all units. Keep count to apply alternating color schemes
for count, unit in enumerate(self.units):
if unit.alt_scheme is None:
# Apply default color scheme, which is alternating
color_scheme = COLOR_SCHEME.A1 if count % 2 else COLOR_SCHEME.A2
else:
# Apply alternate color scheme
color_scheme = unit.alt_scheme
# INA is a special case
if color_scheme == COLOR_SCHEME.INA:
color_scheme = COLOR_SCHEME.A1_INA if count % 2 else COLOR_SCHEME.A2_INA
close = block(click='') if unit.action is not None else ''
b_color = color_scheme.back_color
i_color = color_scheme.icon_color
t_color = color_scheme.text_color
# Start with a major separator. Keep previous background color, set foreground color
# of separator to background color of this unit
blocks = []
blocks.append(block(fg=b_color, append=sl))
blocks.append(' ') # TODO perhaps more nice soluting than manual spacing
if len(unit.items) >= 1:
(icon, text) = unit.items[0]
blocks.append(block(click=unit.action, fg=i_color, bg=b_color, font='2'
, append=' ' + icon))
blocks.append(' ')
blocks.append(block(fg=t_color, font='1', append=text))
if len(unit.items) >= 2:
for item in unit.items[1:]:
(icon, text) = item
# Append minor separator
blocks.append(' ')
blocks.append(sll)
blocks.append(block(fg=i_color, bg=b_color, font='2', append=' ' + icon))
blocks.append(' ')
blocks.append(block(fg=t_color, font='1', append=text))
if len(unit.items) >= 1:
blocks.append(close)
formatted.append(''.join(blocks))
return ' '.join(formatted)
g_parser = LemonParser()
def block(fg = None, bg = None, font = None, click = None, append=''):
# Remeber that clickable blocks need to be closed
rtn = []
@@ -45,6 +111,7 @@ reset_power = block(fg='-', bg=conf.color_poweropts)
class COLOR_SCHEME(Enum):
A1 = (conf.color_sec_b1, conf.color_fore, conf.color_icon)
A2 = (conf.color_sec_b2, conf.color_fore, conf.color_icon)
INA = (None, None, None) # This one is special, dynamically changed to A1_INA or A2_INA
A1_INA = (conf.color_sec_b1, conf.color_disable, conf.color_disable)
A2_INA = (conf.color_sec_b2, conf.color_disable, conf.color_disable)
CPU_ALERT= (conf.color_cpu, conf.color_back, conf.color_back)
@@ -146,48 +213,6 @@ def update_workspaces(data):
workspaces = ''.join([prefix, ' '.join(wspces)])
def update_lang(data):
global language
language = single_sect(icon=conf.icon_lang, click='lang', text=data[0]
, alt=COLOR_SCHEME.A2)
def update_bright(data):
global brightness
brtxt = str(int(float(data[0])))
brightness = single_sect(icon=conf.icon_bright, click='adj_br'
, text=(brtxt+'%'), alt=COLOR_SCHEME.A1)
def update_net(data):
global net_load
if data[0] == 'down': # wlan
(wland_v, wlanu_v) = ('x', 'x')
wlan_alt = COLOR_SCHEME.A2_INA
else:
(wland_v, wlanu_v) = (data[0],data[1])
if max(float(wland_v), float(wlanu_v)) > float(conf.net_alert):
wlan_alt = COLOR_SCHEME.NET_ALERT
else:
wlan_alt = COLOR_SCHEME.A2
if data[2] == 'down': # eth
(ethd_v, ethu_v) = ('x', 'x')
eth_alt = COLOR_SCHEME.A1_INA
else:
(ethd_v, ethu_v) = (data[2],data[3])
if max(float(ethd_v), float(ethu_v)) > float(conf.net_alert):
eth_alt = COLOR_SCHEME.NET_ALERT
else:
eth_alt = COLOR_SCHEME.A1
net_load = ''.join([
double_sect(click = 'wlan', alt=wlan_alt, text1 = wland_v, text2 = wlanu_v,
icon1 = (conf.icon_wlan + conf.icon_dl), icon2 = conf.icon_ul)
, double_sect(click = 'eth', alt=eth_alt, text1 = ethd_v, text2 = ethu_v,
icon1 = (conf.icon_eth + conf.icon_dl), icon2 = conf.icon_ul)
]) # wlan_d wlan_u eth_d eth_u
def update_title(data):
global win_title
win_title = ' '.join([block(fg=conf.color_head, bg=conf.color_sec_b2)
@@ -199,19 +224,19 @@ def update_title(data):
def format_line():
# Need to end with a reset block, otherwise the color fills the %{r} spacer
if common.mode == common.bar_mode.normal:
return ''.join(['%{l}', reset, displays, workspaces, win_title, '%{r}', sys_load, net_load
, volume, brightness, battery, date, language, time, reset])
return ''.join(['%{l}', reset, displays, workspaces, win_title
, '%{r}', g_parser.format(), reset])
elif common.mode == common.bar_mode.power:
return ''.join(['%{l}', reset, power_opts, '%{r}', sys_load, net_load
, volume, brightness, battery, date, language, time, reset_power])
return ''.join(['%{l}', reset, power_opts
, '%{r}', g_parser.format(), reset_power])
elif common.mode == common.bar_mode.control:
return ''.join(['%{l}', reset, displays, workspaces, controls, '%{r}', response, time, reset])
return ''.join(['%{l}', reset, displays, workspaces, controls
, '%{r}', response, time, reset])
else:
return ''.join(['%{l}', reset, displays, workspaces, 'Not normal', '%{r}', time, reset])
return ''.join(['%{l}', reset, displays, workspaces, 'Not normal'
, '%{r}', time, reset])
parsers_dict = { 'WSP':update_workspaces
,'LANG':update_lang
,'BRIGHT':update_bright
,'WIN':update_title
,'DISP':update_displays
,'RESP':update_response
@@ -225,6 +250,14 @@ def parse_line(line_in):
WSPINA1___main INA2___web FOC5___terms INA6___stats
'''
for unit in g_parser.units:
if unit.external is not None:
for key,func in unit.external.items():
l = len(key)
if line_in[:l] == key:
func(line_in[l:].split())
break
try:
for key,func in parsers_dict.items():
l = len(key)