From 10ef712727f1b5c4e57f330a50822edbbda37808 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 4 Jun 2024 12:38:04 -0500 Subject: fix(i3blocks): clear title on empty workspace --- i3blocks/.local/lib/i3blocks/i3blocks-title | 43 ++++++++++++++++------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'i3blocks') diff --git a/i3blocks/.local/lib/i3blocks/i3blocks-title b/i3blocks/.local/lib/i3blocks/i3blocks-title index 44c89de..1247253 100755 --- a/i3blocks/.local/lib/i3blocks/i3blocks-title +++ b/i3blocks/.local/lib/i3blocks/i3blocks-title @@ -9,24 +9,28 @@ import i3ipc from i3ipc.aio import Connection -class Status: - def __init__(self, title: str = "", width: int = 30): +class Printer: + def __init__(self, title: str | None = None, width: int = 30): self.width = width - self.title = title - if len(title) > self.width: - self.iter = deque(list(title + " ")) - else: - self.iter = list(title) + match title: + case str("") | None: + self.iter = [""] + case str(s): + self.iter = deque(list(s + " ")) def __iter__(self): while self.iter: - title = "".join(islice(self.iter, 0, self.width)) - yield title - if isinstance(self.iter, deque): + yield "".join(islice(self.iter, 0, self.width)) + if len(self.iter) > self.width: self.iter.rotate(-1) else: break + async def print(self): + for status in iter(self): + print(status, flush=True) + await asyncio.sleep(0.5) + class Blocklet: EVENTS = [ @@ -47,24 +51,25 @@ class Blocklet: self.i3 = await Connection().connect() for event in Blocklet.EVENTS: - self.i3.on(event, lambda _, event: self.update_focus(event.container)) + self.i3.on(event, lambda _, event: self.update_focus(event.container.name)) + + self.i3.on( + i3ipc.Event.WORKSPACE_FOCUS, + lambda _, event: self.update_focus(), + ) + return self async def main(self): if focused := (await self.i3.get_tree()).find_focused(): - self.update_focus(focused) + self.update_focus(focused.name) await self.i3.main() - async def printer(self, title: str): - for status in iter(Status(title)): - print(status, flush=True) - await asyncio.sleep(0.5) - - def update_focus(self, container: i3ipc.Con): + def update_focus(self, title: str = None): if self.printer_task is not None: self.printer_task.cancel() - self.printer_task = self.task_group.create_task(self.printer(container.name)) + self.printer_task = self.task_group.create_task(Printer(title).print()) async def main(): -- cgit v1.2.3-70-g09d2