From 84925687ca38b1c55d10b5bf158560abd7156373 Mon Sep 17 00:00:00 2001 From: Stefan Bethke Date: Sat, 14 Jun 2025 13:36:35 +0200 Subject: [PATCH] Refactor paged display If oyu have a list of things you would like to show, you can simply use BubaAnimation.pages(). The default layout is a long title left-justified and a short info (date, time) right-justified. Overrride BubaAnimation.write_row() if you need a different layout. --- buba/animations/dbf.py | 47 ++++++++++++++--------------------- buba/animations/icalevents.py | 17 +------------ buba/animations/spaceapi.py | 14 +++++------ buba/bubaanimator.py | 27 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 51 deletions(-) diff --git a/buba/animations/dbf.py b/buba/animations/dbf.py index a444dbf..0d74729 100644 --- a/buba/animations/dbf.py +++ b/buba/animations/dbf.py @@ -45,7 +45,9 @@ class DBFAnimation(BubaAnimation): sleep(60) @staticmethod - def countdown(dt: datetime): + def countdown(dt: datetime, cancelled:bool): + if cancelled: + return "--" now = datetime.datetime.now().astimezone() try: day = now.strftime("%y-%m-%d") @@ -97,31 +99,20 @@ class DBFAnimation(BubaAnimation): train = train.replace(" ", "") return train + def write_row(self, row: int, line: list[str], split=100) -> None: + if len(line) < 3: + super().write_row(row, line) + else: + self.buba.text(page=0, row=row, col_start=0, col_end=11, + text=line[0]) + self.buba.text(page=0, row=row, col_start=12, col_end=104, + text=line[1]) + self.buba.text(page=0, row=row, col_start=105, col_end=119, + text=line[2], align=BubaCmd.ALIGN_RIGHT) + def run(self): - all = self.trains[:self.count] - all_len = int((len(all) + 1) / 3) - - if len(self.trains) == 0: - sleep(5) - - for page, trains in enumerate(self.chunk(all, 3)): - if all_len == 1: - title = self.station - else: - title = f"{self.station} ({page + 1}/{all_len})" - self.buba.text(page=0, row=0, col_start=0, col_end=92, text=title, align=BubaCmd.ALIGN_LEFT) - for i, train in enumerate(trains): - if train['isCancelled']: - when = "--" - else: - when = self.countdown(train['actualDeparture']) - self.buba.text(page=0, row=i + 1, col_start=0, col_end=11, text=self.short_train(train['train'])) - self.buba.text(page=0, row=i + 1, col_start=12, col_end=104, - text=self.short_station(train['destination'])) - self.buba.text(page=0, row=i + 1, col_start=105, col_end=119, - text=when, align=BubaCmd.ALIGN_RIGHT) - self.buba.set_page(0) - for i in range(5): - self.buba.text(page=0, row=0, col_start=93, col_end=119, text=datetime.datetime.now().strftime("%H:%M"), - align=BubaCmd.ALIGN_RIGHT) - sleep(2) + self.pages(self.station, [[ + self.short_train(train['train']), + self.short_station(train['destination']), + self.countdown(train['actualDeparture'], train['isCancelled']), + ] for train in self.trains[:self.count]]) diff --git a/buba/animations/icalevents.py b/buba/animations/icalevents.py index 955095a..b476cab 100644 --- a/buba/animations/icalevents.py +++ b/buba/animations/icalevents.py @@ -31,19 +31,4 @@ class IcalEvents(BubaAnimation): sleep(600) def run(self): - for (page, events) in enumerate(self.chunk(self.events, 3)): - if len(self.events) > 3: - self.buba.text(page=0, row=0, col_start=0, col_end=119, - text=f"{self.title} ({page + 1}/{int((len(self.events) + 2) / 3)})", - align=BubaCmd.ALIGN_LEFT) - else: - self.buba.text(page=0, row=0, col_start=0, col_end=119, text=self.title, align=BubaCmd.ALIGN_LEFT) - for i in range(3): - if i >= len(events): - self.buba.text(page=0, row=i + 1, col_start=0, col_end=119, text="") - else: - event = events[i] - self.buba.text(page=0, row=i + 1, col_start=0, col_end=100, text=self.ellipsis(event.summary, 25)) - self.buba.text(page=0, row=i + 1, col_start=101, col_end=119, - text=self.countdown(event.start), align=BubaCmd.ALIGN_RIGHT) - sleep(10) + self.pages(self.title, [[e.summary, self.countdown(e.start)] for e in self.events]) diff --git a/buba/animations/spaceapi.py b/buba/animations/spaceapi.py index 07bb0db..bd033f8 100644 --- a/buba/animations/spaceapi.py +++ b/buba/animations/spaceapi.py @@ -52,14 +52,14 @@ class Spaceapi(BubaAnimation): for p in self.data["sensors"]["ext_3d_printer_minutes_remaining"]: printers[p["name"]]["remaining"] = p["value"] printstatus = [] - for n, p in printers.items(): + for n, p in sorted(printers.items()): if p["busy"]: - printstatus.append(f"{n} remaining {p['remaining']}") + printstatus.append(f"{n} {p['remaining']}m left") else: printstatus.append(f"{n} idle") - self.buba.text(page=0, row=0, col_start=0, col_end=119, text=f"CCCHH {open} {self.humanize(since)}", align=BubaCmd.ALIGN_LEFT) - self.buba.text(page=0, row=1, col_start=0, col_end=119, text=f"Outside: {temp}°C at {hum}% rel.hum.", align=BubaCmd.ALIGN_LEFT) - self.buba.text(page=0, row=2, col_start=0, col_end=119, text=", ".join(printstatus), align=BubaCmd.ALIGN_LEFT) - - sleep(10) + self.pages("CCCHH Space API", [ + [f"CCCHH {open} {self.humanize(since)}"], + [f"Outside: {temp}°C at {hum}% rel.hum."], + [", ".join(printstatus)] + ]) diff --git a/buba/bubaanimator.py b/buba/bubaanimator.py index a9c7493..255eb1b 100644 --- a/buba/bubaanimator.py +++ b/buba/bubaanimator.py @@ -18,6 +18,7 @@ WEEKDAYS_DE = [ "So" ] + class BubaAnimation: def __init__(self, buba: BubaCmd): self.log = logging.getLogger(type(self).__name__) @@ -81,6 +82,32 @@ class BubaAnimation: text = text[:max - 2] + "..." # we can get away with just 2, since the periods are very narrow return text + def write_row(self, row: int, line: list[str], split=100) -> None: + if len(line) == 1: + self.buba.text(page=0, row=row, col_start=0, col_end=119, + text=self.ellipsis(line[0], 35), align=BubaCmd.ALIGN_LEFT) + else: + self.buba.text(page=0, row=row, col_start=0, col_end=split, text=self.ellipsis(line[0], 25)) + self.buba.text(page=0, row=row, col_start=split+1, col_end=119, + text=line[1], align=BubaCmd.ALIGN_RIGHT) + + def pages(self, title: str, lines: list[list[str]]) -> None: + pages = list(self.chunk(lines, 3)) + for n, page in enumerate(pages): + if len(pages) <= 1: + self.write_row(0, [title]) + else: + self.write_row(0, [title, f"({n + 1}/{len(pages)})"]) + for i in range(3): + if i >= len(page): + self.buba.text(page=0, row=i + 1, col_start=0, col_end=119, text="") + else: + p = page[i] + if isinstance(p, str): + p = [p, ] + self.write_row(i + 1, p) + sleep(10) + class BubaAnimator: def __init__(self, buba: BubaCmd):