From f8ff429d392e10fbbb057c504dc9e51b4b0734ea Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 19 Feb 2024 11:54:08 -0800 Subject: support spacing as part of each module This allows spacing to be inserted only if the module is paintable. --- panel.py | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/panel.py b/panel.py index 467daa3..10abafe 100755 --- a/panel.py +++ b/panel.py @@ -45,9 +45,12 @@ class Fmt: return f'%{{A{btn}:{cmd}:}}{s}%{{A}}' class Mod: - def __init__(self): + def __init__(self, spacing=1, spacing_l=None, spacing_r=None): self.e_repaint = None + self.spacing_l = spacing_l if spacing_l is not None else spacing + self.spacing_r = spacing_r if spacing_r is not None else spacing + # changes should be applied atomically, as the main thread may read # this at any time. self.out = None @@ -59,20 +62,32 @@ class Mod: if callable(getattr(self, 'work', None)): threading.Thread(target=self.work, daemon=True).start() + def to_paint(self): + if (buf := self.out): + if self.spacing_l: + buf = Fmt.spacer(self.spacing_l) + buf + if self.spacing_r: + buf += Fmt.spacer(self.spacing_r) + return buf + def process_cmd(self, cmd): pass class ModRight(Mod): - def __init__(self): + def __init__(self, **kwargs): + kwargs.setdefault('spacing', 0) + super().__init__(**kwargs) self.out = '%{r}' class ModSpacer(Mod): - def __init__(self, n=1): + def __init__(self, n=1, **kwargs): + kwargs.setdefault('spacing', 0) + super().__init__(**kwargs) self.out = Fmt.spacer(n) class ModDate(Mod): - def __init__(self, fmts=None, tzs=None): - super().__init__() + def __init__(self, fmts=None, tzs=None, **kwargs): + super().__init__(**kwargs) self.e = threading.Event() self.fmts = fmts or ('%H:%M',) @@ -150,8 +165,8 @@ class HLWMClient(): class ModHLWMBase(Mod): client = HLWMClient() - def __init__(self): - super().__init__() + def __init__(self, **kwargs): + super().__init__(**kwargs) self.q = queue.Queue() self.cbs = [] @@ -173,8 +188,8 @@ class ModHLWMBase(Mod): return self.client.exec(*args); class ModHLWMTags(ModHLWMBase): - def __init__(self): - super().__init__() + def __init__(self, **kwargs): + super().__init__(**kwargs) self.listen('tag_', self.refresh) def post_start(self): @@ -214,8 +229,8 @@ class ModHLWMTags(ModHLWMBase): subprocess.run(('herbstclient', *cmd.split())) class ModHLWMTitle(ModHLWMBase): - def __init__(self): - super().__init__() + def __init__(self, **kwargs): + super().__init__(**kwargs) self.listen('focus_changed', self.refresh) self.listen('window_title_changed', self.refresh) @@ -228,8 +243,8 @@ class ModHLWMTitle(ModHLWMBase): self.repaint() class ModInputUnavail(Mod): - def __init__(self, path, label=''): - super().__init__() + def __init__(self, path, label='', **kwargs): + super().__init__(**kwargs) self.path = path self.label = label @@ -310,21 +325,18 @@ class Panel: while True: self.e_repaint.wait() self.e_repaint.clear() - print(''.join([m.out for m in self.mods if m.out]), + print(''.join([s for m in self.mods if (s := m.to_paint())]), file=self.panel.stdin, flush=True) Panel( - ModHLWMTags(), - ModSpacer(), + ModHLWMTags(spacing=0), ModHLWMTitle(), ModRight(), ModInputUnavail( '/dev/input/by-id/usb-HID_Keyboard_HID_Keyboard-event-kbd', label='NO KEYBOARD'), - ModSpacer(2), ModDate( fmts=('%Y-%m-%d %H:%M:%S', '%H:%M'), tzs=({'id': None}, {'id': 'UTC'}, - {'id': 'Asia/Yerevan', 'label': 'AMT'})), - ModSpacer() + {'id': 'Asia/Yerevan', 'label': 'AMT'})) ).run() -- cgit v1.2.3-70-g09d2