From c7535d8ed8b4364020d5a1fcf5c7efa542bd4e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Wed, 14 Aug 2024 03:29:34 +0200 Subject: [PATCH] frobarng: Graceful exit --- hm/desktop/frobar/.dev/new.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/hm/desktop/frobar/.dev/new.py b/hm/desktop/frobar/.dev/new.py index 4f88a6e..bab7e0c 100644 --- a/hm/desktop/frobar/.dev/new.py +++ b/hm/desktop/frobar/.dev/new.py @@ -4,6 +4,7 @@ import asyncio import datetime import enum import random +import signal import typing import i3ipc @@ -191,6 +192,7 @@ class Bar(ComposableText): self.refresh = asyncio.Event() self.taskGroup = asyncio.TaskGroup() self.providers: list["Provider"] = list() + self.running = True self.screens = [] i3 = i3ipc.Connection() @@ -213,16 +215,24 @@ class Bar(ComposableText): async def refresher() -> None: assert proc.stdin - while True: + while self.running: await self.refresh.wait() self.refresh.clear() proc.stdin.write(self.getMarkup().encode()) async with self.taskGroup as tg: - tg.create_task(refresher()) + ref = tg.create_task(refresher()) for provider in self.providers: tg.create_task(provider.run()) + def exit() -> None: + print("Terminating") + ref.cancel() + self.running = False + + loop = asyncio.get_event_loop() + loop.add_signal_handler(signal.SIGINT, exit) + def generateMarkup(self) -> str: return "".join(section.getMarkup() for section in self.screens) + "\n" @@ -290,7 +300,7 @@ class TimeProvider(SingleSectionProvider): async def run(self) -> None: await super().run() - while True: + while self.section.bar.running: now = datetime.datetime.now() # section.setText(now.strftime("%a %y-%m-%d %H:%M:%S.%f")) self.section.setText("-" * (now.second % 10)) @@ -330,4 +340,3 @@ async def main() -> None: asyncio.run(main()) -# TODO Replace while True with while bar.running or something