Skip to content

Commit

Permalink
Implement rich table views for tracks and albums
Browse files Browse the repository at this point in the history
  • Loading branch information
purefunctor committed Jun 13, 2021
1 parent 5d44c05 commit a7af4f2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages = [
python = "^3.9"
attrs = "^21.2.0"
click = "^8.0.1"
humanize = "^3.7.1"
rich = "^10.2.2"
spotipy = "^2.18.0"
toml = "^0.10.2"
Expand Down
10 changes: 4 additions & 6 deletions stlt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from pathlib import Path

import click
from rich import print
from spotipy import Spotify # type: ignore
from spotipy.cache_handler import CacheFileHandler # type: ignore
from spotipy.oauth2 import SpotifyOAuth # type: ignore

from stlt.config import load_config
from stlt.constants import CONFIG_FILE_PATH
from stlt.view import create_album_view, create_track_view


pass_spotify = click.make_pass_decorator(Spotify)
Expand Down Expand Up @@ -52,9 +54,7 @@ def saved(client: Spotify) -> None:
def saved_albums(client: Spotify, limit: int) -> None:
"""List the user's saved albums."""
response = client.current_user_saved_albums(limit=limit)
for item in response["items"]:
album = item["album"]
print(album["name"], "-", album["label"], "-", album["artists"][0]["name"])
print(create_album_view(response["items"]))


@saved.command(name="tracks")
Expand All @@ -63,9 +63,7 @@ def saved_albums(client: Spotify, limit: int) -> None:
def saved_tracks(client: Spotify, limit: int) -> None:
"""List the user's saved tracks."""
response = client.current_user_saved_tracks(limit=limit)
for item in response["items"]:
track = item["track"]
print(track["name"], "-", track["artists"][0]["name"])
print(create_track_view(response["items"]))


@stlt.command(name="login")
Expand Down
64 changes: 46 additions & 18 deletions stlt/view.py
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")

0 comments on commit a7af4f2

Please sign in to comment.