Refactor paged display
All checks were successful
docker-image / docker (push) Successful in 9m44s

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.
This commit is contained in:
Stefan Bethke 2025-06-14 13:36:35 +02:00
commit 84925687ca
4 changed files with 54 additions and 51 deletions

View file

@ -45,7 +45,9 @@ class DBFAnimation(BubaAnimation):
sleep(60) sleep(60)
@staticmethod @staticmethod
def countdown(dt: datetime): def countdown(dt: datetime, cancelled:bool):
if cancelled:
return "--"
now = datetime.datetime.now().astimezone() now = datetime.datetime.now().astimezone()
try: try:
day = now.strftime("%y-%m-%d") day = now.strftime("%y-%m-%d")
@ -97,31 +99,20 @@ class DBFAnimation(BubaAnimation):
train = train.replace(" ", "") train = train.replace(" ", "")
return train 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): def run(self):
all = self.trains[:self.count] self.pages(self.station, [[
all_len = int((len(all) + 1) / 3) self.short_train(train['train']),
self.short_station(train['destination']),
if len(self.trains) == 0: self.countdown(train['actualDeparture'], train['isCancelled']),
sleep(5) ] for train in self.trains[:self.count]])
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)

View file

@ -31,19 +31,4 @@ class IcalEvents(BubaAnimation):
sleep(600) sleep(600)
def run(self): def run(self):
for (page, events) in enumerate(self.chunk(self.events, 3)): self.pages(self.title, [[e.summary, self.countdown(e.start)] for e in self.events])
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)

View file

@ -52,14 +52,14 @@ class Spaceapi(BubaAnimation):
for p in self.data["sensors"]["ext_3d_printer_minutes_remaining"]: for p in self.data["sensors"]["ext_3d_printer_minutes_remaining"]:
printers[p["name"]]["remaining"] = p["value"] printers[p["name"]]["remaining"] = p["value"]
printstatus = [] printstatus = []
for n, p in printers.items(): for n, p in sorted(printers.items()):
if p["busy"]: if p["busy"]:
printstatus.append(f"{n} remaining {p['remaining']}") printstatus.append(f"{n} {p['remaining']}m left")
else: else:
printstatus.append(f"{n} idle") 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.pages("CCCHH Space API", [
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) [f"CCCHH {open} {self.humanize(since)}"],
self.buba.text(page=0, row=2, col_start=0, col_end=119, text=", ".join(printstatus), align=BubaCmd.ALIGN_LEFT) [f"Outside: {temp}°C at {hum}% rel.hum."],
[", ".join(printstatus)]
sleep(10) ])

View file

@ -18,6 +18,7 @@ WEEKDAYS_DE = [
"So" "So"
] ]
class BubaAnimation: class BubaAnimation:
def __init__(self, buba: BubaCmd): def __init__(self, buba: BubaCmd):
self.log = logging.getLogger(type(self).__name__) 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 text = text[:max - 2] + "..." # we can get away with just 2, since the periods are very narrow
return text 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: class BubaAnimator:
def __init__(self, buba: BubaCmd): def __init__(self, buba: BubaCmd):