frobarng: Graceful exit

This commit is contained in:
Geoffrey Frogeye 2024-08-14 03:29:34 +02:00
parent 6690f3aa0d
commit c7535d8ed8
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8

View file

@ -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