Further refactor
All checks were successful
docker-image / docker (push) Successful in 10m11s

By specifying a layout, you can format the page directly, instead of having to overrride write_row.
This commit is contained in:
Stefan Bethke 2025-06-15 17:41:34 +02:00
commit 0adabf7758
2 changed files with 38 additions and 21 deletions

View file

@ -10,12 +10,18 @@ from time import sleep
from deutschebahn.db_infoscreen import DBInfoscreen
from buba.bubaanimator import BubaAnimation
from buba.bubaanimator import BubaAnimation, LineLayoutColumn
from buba.bubacmd import BubaCmd
LOG = logging.getLogger(__name__)
class DBFAnimation(BubaAnimation):
dbf_layout = [
LineLayoutColumn(12, BubaCmd.ALIGN_LEFT),
LineLayoutColumn(81, BubaCmd.ALIGN_LEFT),
LineLayoutColumn(27, BubaCmd.ALIGN_RIGHT),
]
def __init__(self, buba: BubaCmd, ds100="AHST", station="Holstenstraße", count=3):
super().__init__(buba)
self.dbi = DBInfoscreen("trains.xatlabs.com")
@ -99,20 +105,9 @@ 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):
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]])
] for train in self.trains[:self.count]], layout=self.dbf_layout)

View file

@ -1,4 +1,5 @@
import logging
from dataclasses import dataclass
from datetime import datetime, timedelta
from itertools import islice
from threading import Thread
@ -19,7 +20,18 @@ WEEKDAYS_DE = [
]
# class layout with width, alignment; list of layouts
@dataclass
class LineLayoutColumn:
width: int
align: int
class BubaAnimation:
default_layout = [
LineLayoutColumn(90, BubaCmd.ALIGN_LEFT),
LineLayoutColumn(30, BubaCmd.ALIGN_RIGHT),
]
def __init__(self, buba: BubaCmd):
self.log = logging.getLogger(type(self).__name__)
self.buba = buba
@ -82,22 +94,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:
def write_row(self, row: int, line: list[str], layout: list[LineLayoutColumn]) -> None:
# pass a layout with n columns, or make it an instance variable
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)
elif len(line) < len(layout) and len(line) == 2:
self.write_row(row, line, self.default_layout)
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)
col = 0
for i, ll in enumerate(layout):
t = line[i] if len(line) > i else ""
if ll.width > 24:
t = self.ellipsis(t, int(ll.width / 3.7))
self.buba.text(page=0, row=row, col_start=col, col_end=col+ll.width-1, text=t, align=ll.align)
col += ll.width
def pages(self, title: str, lines: list[list[str]]) -> None:
def pages(self, title: str, lines: list[list[str]], layout=None) -> None:
# pass the layout through to write_row
if layout is None:
layout = self.default_layout
pages = list(self.chunk(lines, 3))
for n, page in enumerate(pages):
if len(pages) <= 1:
self.write_row(0, [title])
self.write_row(0, [title], layout)
else:
self.write_row(0, [title, f"({n + 1}/{len(pages)})"])
self.write_row(0, [title, f"({n + 1}/{len(pages)})"], layout)
for i in range(3):
if i >= len(page):
self.buba.text(page=0, row=i + 1, col_start=0, col_end=119, text="")
@ -105,7 +127,7 @@ class BubaAnimation:
p = page[i]
if isinstance(p, str):
p = [p, ]
self.write_row(i + 1, p)
self.write_row(i + 1, p, layout)
sleep(10)