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 datetime
import enum import enum
import random import random
import signal
import typing import typing
import i3ipc import i3ipc
@ -191,6 +192,7 @@ class Bar(ComposableText):
self.refresh = asyncio.Event() self.refresh = asyncio.Event()
self.taskGroup = asyncio.TaskGroup() self.taskGroup = asyncio.TaskGroup()
self.providers: list["Provider"] = list() self.providers: list["Provider"] = list()
self.running = True
self.screens = [] self.screens = []
i3 = i3ipc.Connection() i3 = i3ipc.Connection()
@ -213,16 +215,24 @@ class Bar(ComposableText):
async def refresher() -> None: async def refresher() -> None:
assert proc.stdin assert proc.stdin
while True: while self.running:
await self.refresh.wait() await self.refresh.wait()
self.refresh.clear() self.refresh.clear()
proc.stdin.write(self.getMarkup().encode()) proc.stdin.write(self.getMarkup().encode())
async with self.taskGroup as tg: async with self.taskGroup as tg:
tg.create_task(refresher()) ref = tg.create_task(refresher())
for provider in self.providers: for provider in self.providers:
tg.create_task(provider.run()) 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: def generateMarkup(self) -> str:
return "".join(section.getMarkup() for section in self.screens) + "\n" return "".join(section.getMarkup() for section in self.screens) + "\n"
@ -290,7 +300,7 @@ class TimeProvider(SingleSectionProvider):
async def run(self) -> None: async def run(self) -> None:
await super().run() await super().run()
while True: while self.section.bar.running:
now = datetime.datetime.now() now = datetime.datetime.now()
# section.setText(now.strftime("%a %y-%m-%d %H:%M:%S.%f")) # section.setText(now.strftime("%a %y-%m-%d %H:%M:%S.%f"))
self.section.setText("-" * (now.second % 10)) self.section.setText("-" * (now.second % 10))
@ -330,4 +340,3 @@ async def main() -> None:
asyncio.run(main()) asyncio.run(main())
# TODO Replace while True with while bar.running or something