-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement rich table views for tracks and albums
- Loading branch information
1 parent
5d44c05
commit a7af4f2
Showing
4 changed files
with
67 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,55 @@ | ||
"""Rich terminal frontend using `rich`.""" | ||
|
||
from rich import box, print | ||
from datetime import timedelta | ||
import typing as t | ||
|
||
from humanize import precisedelta | ||
from rich import box | ||
from rich.abc import RichRenderable | ||
from rich.align import Align | ||
from rich.columns import Columns | ||
from rich.table import Table | ||
from rich.text import Text | ||
|
||
table = Table(expand=True, box=box.SQUARE, style="green") | ||
table.add_column(Text("RAINY NIGHT IN TALINN", justify="center")) | ||
table.add_row("[bold]Artist:[/bold] Ludwig Goransson") | ||
table.add_row("[bold]Length:[/bold] 8m00s") | ||
table.add_row( | ||
"[bold]Album:[/bold] Tenet (Original Motion Picture Soundtrack) [Deluxe Edition]" | ||
) | ||
|
||
columns = Columns( | ||
[ | ||
table, | ||
table, | ||
table, | ||
table, | ||
], | ||
width=35, | ||
) | ||
def create_track_view(items: list[t.Mapping]) -> RichRenderable: | ||
"""Create renderable views for tracks.""" | ||
columns = [] | ||
for index, item in enumerate(items): | ||
track = item["track"] | ||
table = Table(expand=True, box=box.SQUARE) | ||
name = track["name"] | ||
_name = Text(f"{name}") | ||
_name.truncate(25, overflow="ellipsis") | ||
table.add_column(Columns([Text(f"{index}."), _name])) | ||
artist = track["artists"][0]["name"] | ||
table.add_row(f"[bold]Artist:[/bold] {artist}") | ||
duration = timedelta(milliseconds=track["duration_ms"]) | ||
duration = precisedelta(duration, format="%0.0f") | ||
table.add_row(f"[bold]Duration:[/bold] {duration}") | ||
album = track["album"]["name"] | ||
_album = Text("Album: ", style="bold").append_text( | ||
Text(album, style="not bold") | ||
) | ||
_album.truncate(25, overflow="ellipsis") | ||
table.add_row(_album) | ||
columns.append(table) | ||
return Align(Columns(columns, width=35), align="center") | ||
|
||
|
||
print(Align(columns, "center")) | ||
def create_album_view(items: list[t.Mapping]) -> RichRenderable: | ||
"""Create renderable views from albums.""" | ||
columns = [] | ||
for index, item in enumerate(items): | ||
album = item["album"] | ||
name = album["name"] | ||
label = album["label"] | ||
artist = album["artists"][0]["name"] | ||
table = Table(expand=True, box=box.SQUARE) | ||
_name = Text(name) | ||
_name.truncate(25, overflow="ellipsis") | ||
table.add_column(Columns([Text(f"{index}."), _name])) | ||
table.add_row(f"[bold]Label: [/bold] {label}") | ||
table.add_row(f"[bold]Artist: [/bold] {artist}") | ||
columns.append(table) | ||
return Align(Columns(columns, width=35), align="center") |