|
| 1 | +"""Render an astronomical summary table with figlets and times. |
| 2 | +
|
| 3 | +This module builds a two-column Rich table containing large figlet |
| 4 | +art for sunrise/sunset, followed by an "Event | Local Time" header |
| 5 | +row and the corresponding event times converted to the app timezone. |
| 6 | +""" |
| 7 | + |
| 8 | +from pyfiglet import Figlet |
| 9 | +from rich.align import Align |
| 10 | +from rich.console import Group |
| 11 | +from rich.table import Table |
| 12 | +from rich.text import Text |
| 13 | + |
| 14 | +from sample_python_app.core import Settings |
| 15 | +from sample_python_app.models import AstronomicalData |
| 16 | + |
| 17 | + |
| 18 | +def build_astro_table(astro: AstronomicalData, settings: Settings): |
| 19 | + """Build a rich Table for the astronomical data. |
| 20 | +
|
| 21 | + The table renders large figlets first, then a manual header row |
| 22 | + "Event | Local Time" immediately below the figlets so the |
| 23 | + column labels appear under the art. |
| 24 | + """ |
| 25 | + astro_table = Table(show_header=False, box=None) |
| 26 | + |
| 27 | + sunrise_local = astro.sunrise.astimezone(settings.tz) |
| 28 | + sun_art = Figlet(font="small", width=60).renderText("SUNRISE") |
| 29 | + sun_set_art = Figlet(font="small", width=60).renderText("SUNSET") |
| 30 | + sun_text = Text(sun_art, style="bold yellow") |
| 31 | + sun_set_text = Text(sun_set_art, style="bold blue") |
| 32 | + sunrise_time_art = Figlet(font="mini", width=60).renderText( |
| 33 | + sunrise_local.strftime("%I:%M %p") |
| 34 | + ) |
| 35 | + sunset_time_art = Figlet(font="mini", width=60).renderText( |
| 36 | + astro.sunset.astimezone(settings.tz).strftime("%I:%M %p") |
| 37 | + ) |
| 38 | + sunrise_time_text = Text(sunrise_time_art, style="bold yellow") |
| 39 | + sunset_time_text = Text(sunset_time_art, style="bold blue") |
| 40 | + |
| 41 | + astro_table.add_row( |
| 42 | + Align( |
| 43 | + Group(Align.center(sun_text), Align.center(sunrise_time_text)), |
| 44 | + align="center", |
| 45 | + vertical="middle", |
| 46 | + ), |
| 47 | + Align( |
| 48 | + Group(Align.center(sun_set_text), Align.center(sunset_time_text)), |
| 49 | + align="center", |
| 50 | + vertical="middle", |
| 51 | + ), |
| 52 | + ) |
| 53 | + |
| 54 | + astro_table.add_row( |
| 55 | + Text("Event", style="bold magenta", justify="center"), |
| 56 | + Text("Local Time", style="bold magenta", justify="center"), |
| 57 | + ) |
| 58 | + |
| 59 | + tz = settings.tz |
| 60 | + time_fmt = "%I:%M %p %Z" |
| 61 | + for name, dt in astro.as_local(tz).items(): |
| 62 | + label = name.replace("_", " ").title() |
| 63 | + value = dt.strftime(time_fmt) if hasattr(dt, "strftime") else str(dt) |
| 64 | + astro_table.add_row(f"[bold #ff6ec7]{label}[/]", f"[bold #00eaff]{value}[/]") |
| 65 | + |
| 66 | + return astro_table |
0 commit comments