lemonbar: Automatically register and remove units
This commit is contained in:
@@ -14,11 +14,21 @@ class LemonModule(threading.Thread):
|
||||
# sends to lemonbar queue. On click actions are registered with the module.
|
||||
# Every module runs in its own thread
|
||||
|
||||
def units(self):
|
||||
# Iterate over units
|
||||
for _, attr_instance in self.__dict__.items():
|
||||
if isinstance(attr_instance, parser.LemonUnit):
|
||||
yield attr_instance
|
||||
|
||||
def __init__(self):
|
||||
self.actions = {}
|
||||
super().__init__()
|
||||
self._start_module()
|
||||
|
||||
for unit in self.units():
|
||||
parser.g_parser.register_unit(unit)
|
||||
|
||||
|
||||
def run(self):
|
||||
if self.status_handle is None:
|
||||
return
|
||||
@@ -38,6 +48,9 @@ class LemonModule(threading.Thread):
|
||||
def stop(self):
|
||||
self._stop_module()
|
||||
|
||||
for unit in self.units():
|
||||
parser.g_parser.remove_unit(unit)
|
||||
|
||||
def parse(self, line):
|
||||
data = line.split()
|
||||
self._parse_data(data) # Update correct field
|
||||
@@ -120,8 +133,6 @@ class DateTimeModule(LemonModule):
|
||||
, alt_scheme=parser.COLOR_SCHEME.SPECIAL)
|
||||
self.time.modes = [mode for mode in common.bar_mode]
|
||||
|
||||
parser.g_parser.register_unit(self.date)
|
||||
parser.g_parser.register_unit(self.time)
|
||||
|
||||
self.register_action('date' , self.date_comm)
|
||||
self.register_action('toggle_secs' , self.toggle_secs)
|
||||
@@ -130,8 +141,6 @@ class DateTimeModule(LemonModule):
|
||||
if self.p_handle is not None:
|
||||
self.p_handle.terminate()
|
||||
|
||||
parser.g_parser.remove_unit(self.date)
|
||||
parser.g_parser.remove_unit(self.time)
|
||||
|
||||
def _parse_data(self, data):
|
||||
# Date and time
|
||||
@@ -199,9 +208,6 @@ ${else}down down${endif}\
|
||||
self.eth_load = parser.IconTextUnit('eth_load', action='eth', order=14)
|
||||
self.volume = parser.IconTextUnit('volume', action='pavu', order=20)
|
||||
|
||||
parser.g_parser.register_unit(self.wlan_load)
|
||||
parser.g_parser.register_unit(self.eth_load)
|
||||
parser.g_parser.register_unit(self.volume)
|
||||
|
||||
self.register_action('wlan' , self.nmtui)
|
||||
self.register_action('pavu' , self.pavu)
|
||||
@@ -210,9 +216,6 @@ ${else}down down${endif}\
|
||||
if self.p_handle is not None:
|
||||
self.p_handle.terminate()
|
||||
|
||||
parser.g_parser.remove_unit(self.wlan_load)
|
||||
parser.g_parser.remove_unit(self.eth_load)
|
||||
parser.g_parser.remove_unit(self.volume)
|
||||
|
||||
def _parse_data(self, data):
|
||||
# wlan and eth
|
||||
@@ -297,11 +300,6 @@ ${exec /home/kuba/.i3/scripts/lang.sh show}
|
||||
self.language = parser.IconTextUnit('language', action='lang', order=32
|
||||
, external={'LANG': self.parse_language})
|
||||
|
||||
parser.g_parser.register_unit(self.sys_load)
|
||||
parser.g_parser.register_unit(self.disk)
|
||||
parser.g_parser.register_unit(self.brightness)
|
||||
parser.g_parser.register_unit(self.battery)
|
||||
parser.g_parser.register_unit(self.language)
|
||||
|
||||
self.register_action('load' , self.htop)
|
||||
self.register_action('lang' , self.lang_comm)
|
||||
@@ -309,11 +307,6 @@ ${exec /home/kuba/.i3/scripts/lang.sh show}
|
||||
self.register_action('dpms' , self.dpms_comm)
|
||||
|
||||
def _stop_module(self):
|
||||
parser.g_parser.remove_unit(self.sys_load)
|
||||
parser.g_parser.remove_unit(self.disk)
|
||||
parser.g_parser.remove_unit(self.brightness)
|
||||
parser.g_parser.remove_unit(self.battery)
|
||||
parser.g_parser.remove_unit(self.language)
|
||||
|
||||
if self.p_handle is not None:
|
||||
self.p_handle.terminate()
|
||||
@@ -393,9 +386,6 @@ class i3Module(LemonModule):
|
||||
self.displays.modes.append(common.bar_mode.control)
|
||||
self.workspaces.modes.append(common.bar_mode.control)
|
||||
|
||||
parser.g_parser.register_unit(self.displays)
|
||||
parser.g_parser.register_unit(self.workspaces)
|
||||
parser.g_parser.register_unit(self.title)
|
||||
|
||||
self.register_action('i3-msg' , self.i3msg_comm)
|
||||
self.register_action('mode' , self.set_mode)
|
||||
@@ -411,9 +401,6 @@ class i3Module(LemonModule):
|
||||
self.i3_ws_obj.focus_callbacks.append(self.kill_floating_windows)
|
||||
|
||||
def _stop_module(self):
|
||||
parser.g_parser.remove_unit(self.displays)
|
||||
parser.g_parser.remove_unit(self.workspaces)
|
||||
parser.g_parser.remove_unit(self.title)
|
||||
|
||||
if self.i3_ws_obj is not None:
|
||||
self.i3_ws_obj.quit()
|
||||
@@ -591,8 +578,6 @@ class ScreenModule(LemonModule):
|
||||
self.response.modes = [common.bar_mode.control]
|
||||
self.controls.modes = [common.bar_mode.control]
|
||||
|
||||
parser.g_parser.register_unit(self.response)
|
||||
parser.g_parser.register_unit(self.controls)
|
||||
|
||||
self.register_action('bluetooth', self.bluetooth)
|
||||
|
||||
@@ -601,8 +586,6 @@ class ScreenModule(LemonModule):
|
||||
self.status_handle.close()
|
||||
with contextlib.suppress(FileNotFoundError):
|
||||
os.remove(config.fifo_screen_log)
|
||||
parser.g_parser.remove_unit(self.response)
|
||||
parser.g_parser.remove_unit(self.controls)
|
||||
|
||||
def _parse_data(self, data):
|
||||
line = ' '.join(data)
|
||||
@@ -652,10 +635,9 @@ class PowerOptionsModule(LemonModule):
|
||||
|
||||
self.power_opts.modes = [common.bar_mode.power]
|
||||
|
||||
parser.g_parser.register_unit(self.power_opts)
|
||||
|
||||
def _stop_module(self):
|
||||
parser.g_parser.remove_unit(self.power_opts)
|
||||
pass
|
||||
|
||||
def _parse_data(self, data):
|
||||
pass
|
||||
|
||||
@@ -9,7 +9,12 @@ slr = config.sep_l_right
|
||||
sl = config.sep_left
|
||||
sll = config.sep_l_left
|
||||
|
||||
class IconTextUnit:
|
||||
class LemonUnit:
|
||||
def __lt__(self, other):
|
||||
return self.order < other.order
|
||||
|
||||
|
||||
class IconTextUnit(LemonUnit):
|
||||
def __init__(self, name, order, action = None, alt_scheme=None, external=None):
|
||||
self.name = name
|
||||
self.action = action
|
||||
@@ -52,10 +57,7 @@ class IconTextUnit:
|
||||
blocks.append(close)
|
||||
return ''.join(blocks)
|
||||
|
||||
def __lt__(self, other):
|
||||
return self.order < other.order
|
||||
|
||||
class ButtonsUnit:
|
||||
class ButtonsUnit(LemonUnit):
|
||||
def __init__(self, name, order, alt_scheme=None, external=None):
|
||||
self.name = name
|
||||
self.order = order
|
||||
@@ -86,10 +88,7 @@ class ButtonsUnit:
|
||||
|
||||
return ''.join(blocks)
|
||||
|
||||
def __lt__(self, other):
|
||||
return self.order < other.order
|
||||
|
||||
class CustomUnit:
|
||||
class CustomUnit(LemonUnit):
|
||||
def __init__(self, name, format_function, order, action = None, alt_scheme=None, external=None):
|
||||
self.name = name
|
||||
self.action = action
|
||||
@@ -100,9 +99,6 @@ class CustomUnit:
|
||||
self.items = [] # List of tuples (icon, text)
|
||||
self.modes = [common.bar_mode.normal]
|
||||
|
||||
def __lt__(self, other):
|
||||
return self.order < other.order
|
||||
|
||||
class LemonParser:
|
||||
# Handle parsing of units
|
||||
# Apply alternating colors
|
||||
|
||||
Reference in New Issue
Block a user