Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions spotapi/_connect/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,26 @@ def compose(self) -> ComposeResult:
yield Static("device / waiting for Spotify", id="device")
with Horizontal(id="split"):
with Vertical(id="player-pane"):
yield Static("01 / NOW PLAYING", id="eyebrow")
yield Static("NOW PLAYING", id="eyebrow")
yield Static("Waiting for Spotify", id="track")
yield Static("Start a song on any Spotify player", id="artist")
yield Static("", id="progress")
yield Static("", id="details")
with Horizontal(id="transport"):
yield Button("◀ prev", id="previous", classes="transport-link")
yield Button("Ⅱ pause", id="toggle", classes="transport-link primary")
yield Button("next ▶", id="next", classes="transport-link")
yield Button("|◀", id="previous", classes="transport-link")
yield Button("Ⅱ", id="toggle", classes="transport-link primary")
yield Button("▶|", id="next", classes="transport-link")
with Horizontal(id="context-tabs"):
yield Button("queue", id="tab-queue", classes="tab-button active")
yield Button("results", id="tab-search", classes="tab-button")
yield Button("devices", id="tab-devices", classes="tab-button")
yield Button("help", id="tab-help", classes="tab-button")
yield Static("UP NEXT / SPOTIFY SNAPSHOT", id="context-label")
yield Static("Upcoming tracks reported by Spotify", id="context-label")
yield Static("No upcoming tracks reported by Spotify.", id="context-content")
yield ContextOptions(id="context-options", compact=True, markup=False)
with Vertical(id="activity-pane"):
with Horizontal(id="activity-header"):
yield Static("02 / ACTIVITY", id="channel-label")
yield Static("ACTIVITY", id="channel-label")
yield Button("logs F2", id="logs-toggle", classes="subtle-link")
yield Static(
"Commands and Spotify events · scroll to inspect",
Expand All @@ -254,6 +254,8 @@ async def on_mount(self) -> None:
self.query_one("#command", CommandInput).suggestions_menu = menu
self.query_one("#context-options", ContextOptions).display = False
self.query_one("#diagnostics", RichLog).display = False
self.query_one("#previous", Button).tooltip = "Previous track"
self.query_one("#next", Button).tooltip = "Next track"
self._ui_ready = True
try:
await self.catalogue.__aenter__()
Expand Down Expand Up @@ -362,9 +364,10 @@ def _render_state(self) -> None:
)
for button_id in ("previous", "toggle", "next"):
self.query_one(f"#{button_id}", Button).disabled = not actionable
self.query_one("#toggle", Button).label = (
"▶ resume" if state is None or state.is_paused else "Ⅱ pause"
)
toggle = self.query_one("#toggle", Button)
paused = state is None or state.is_paused
toggle.label = "▶" if paused else "Ⅱ"
toggle.tooltip = "Resume" if paused else "Pause"
if not actionable:
self.query_one("#track", Static).update(
"Control a selected device"
Expand All @@ -387,12 +390,15 @@ def _render_state(self) -> None:
" · ".join(part for part in (state.track.artist, state.track.album) if part)
)
meter = self.query_one("#progress", Static)
width = max(8, min(23, meter.size.width - 18))
elapsed = clock(state.position_ms)
duration = clock(state.duration_ms)
width = max(0, min(40, meter.size.width - len(elapsed) - len(duration) - 4))
bar = f" {progress_bar(state.progress, width=width)} " if width else " / "
meter.update(
Text.assemble(
(clock(state.position_ms), "bold #a8f3b5"),
(f" {progress_bar(state.progress, width=width)} ", "#6fce91"),
(clock(state.duration_ms), "#7f9292"),
(elapsed, "bold #a8f3b5"),
(bar, "#6fce91"),
(duration, "#7f9292"),
)
)
vol = device.volume_percent if device else None
Expand Down Expand Up @@ -429,9 +435,7 @@ def _render_context(self) -> None:
rows: tuple[tuple[str, str], ...] = ()
match self._active_panel:
case "devices":
label.update(
f"DEVICES / {len(self.player.devices)} FOUND · ENTER TARGET · T TRANSFER"
)
label.update(f"{len(self.player.devices)} found · Enter target · T transfer")
state = self.player.state
active = state.active_device_id if state is not None else None
target = self.player.target_device_id
Expand All @@ -444,14 +448,14 @@ def _render_context(self) -> None:
)
content.update("No devices found. Open Spotify on an existing player.")
case "search":
label.update(f"RESULTS / {len(self.hits)} TRACKS · ENTER PLAY · Q QUEUE")
label.update(f"{len(self.hits)} tracks · Enter play · Q queue")
rows = tuple(
(f"track:{index}", search_row(index, hit))
for index, hit in enumerate(self.hits, 1)
)
content.update("Search for an artist or track: search Radiohead")
case "help":
label.update("HELP / TYPE ANY COMMAND AT THE PROMPT")
label.update("Type any command at the prompt")
content.update(
"SEARCH search Daft Punk · play 1 · queue 1\n"
"PLAYER pause · resume · toggle · next · previous\n"
Expand All @@ -462,7 +466,7 @@ def _render_context(self) -> None:
" ↑↓ select · Enter play · Q queue · T transfer"
)
case "queue":
label.update("UP NEXT / OBSERVED SPOTIFY QUEUE")
label.update("Upcoming tracks reported by Spotify")
content.update(queue_rows(self._queue_items, limit=8))

if rows != self._context_options:
Expand Down
4 changes: 3 additions & 1 deletion spotapi/_connect/presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from dataclasses import dataclass

from rich.cells import cell_len, set_cell_size

from spotapi.models import PlaybackTrack
from spotapi.models.catalogue.track import Track
from spotapi.models.playback import Device
Expand Down Expand Up @@ -73,7 +75,7 @@ def prompt(self) -> str:
def _short(value: str, width: int) -> str:
"""Keep interactive rows scannable even in an IDE terminal."""
value = value.replace("\r", " ").replace("\n", " ").strip()
return value if len(value) <= width else value[: width - 1].rstrip() + "…"
return value if cell_len(value) <= width else set_cell_size(value, width - 1).rstrip() + "…"


def search_row(index: int, hit: Track) -> str:
Expand Down
25 changes: 18 additions & 7 deletions spotapi/_connect/style.tcss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Screen {
background: #0d1713;
border: solid #24382b;
}
/* Playback is centered; list content remains left-aligned. */
#eyebrow, #track, #artist, #progress, #details { text-align: center; }
#eyebrow {
height: 1;
color: #83d794;
Expand All @@ -94,11 +96,11 @@ Screen {
}
#transport {
height: 3;
margin-bottom: 0;
margin-bottom: 1;
}
.transport-link {
width: 1fr;
min-width: 8;
min-width: 3;
height: 3;
margin-right: 1;
background: #213226;
Expand Down Expand Up @@ -261,15 +263,18 @@ Screen {
.narrow #split { layout: vertical; }
.narrow #player-pane {
width: 100%;
height: 17;
height: 1fr;
margin: 0 0 1 0;
padding: 0 1;
}
.narrow #activity-pane { width: 100%; height: 1fr; }
.narrow #activity-pane { width: 100%; height: 6; padding: 0 1; }
.narrow #track { height: 1; }
.narrow .tab-button { width: 1fr; min-width: 0; height: 1; margin: 0; padding: 0; }
.narrow #context-content { max-height: 4; }
.narrow #context-options { min-height: 2; }
.narrow #artist, .narrow #details { height: 1; }
.narrow #progress { height: 1; }
.narrow #context-tabs { margin-top: 0; }
.narrow #context-tabs { height: 2; }
.narrow #transport { margin-bottom: 0; }
.short #account-strip { height: 2; padding-top: 0; }
.short #shortcut-hint { height: 1; padding-top: 0; }
Expand All @@ -280,11 +285,17 @@ Screen {
.short #eyebrow { height: 1; }
.short #player-pane { padding: 0 1; }
.short #activity-pane { padding: 0 1; }
.narrow.short #player-pane { height: 13; }
.short #transport { margin-bottom: 0; }
.narrow.short #header { height: 2; }
.narrow.short #account-strip { height: 1; }
.narrow.short #shortcut-hint, .narrow.short #eyebrow, .narrow.short #details,
.narrow.short #activity-subtitle { display: none; }
.narrow.short #player-pane { margin-bottom: 0; }
.narrow.short #activity-pane { height: 4; }
.narrow.short #activity-header { height: 1; }
.short #context-content { min-height: 1; }
.tiny #account-strip { display: none; }
.tiny #shortcut-hint { display: none; }
.tiny #context-content, .tiny #context-label, .tiny #context-tabs { display: none; }
.tiny #artist, .tiny #details { display: none; }
.narrow.tiny #player-pane { height: 7; }
.tiny #activity-subtitle { display: none; }
19 changes: 19 additions & 0 deletions tests/test_connect_presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ def test_search_results_have_useful_optional_metadata() -> None:
assert search_row(1, Track(id="x", name="Unknown", uri="spotify:track:x")) == "01 Unknown"


def test_display_truncation_uses_terminal_cells() -> None:
from rich.cells import cell_len

from spotapi._connect.presentation import _short

assert _short("abc", 3) == "abc"
assert _short("日本語", 5) == "日本…"
assert cell_len(_short("日本語", 4)) <= 4


def test_device_rows_distinguish_playing_target_and_idle() -> None:
from spotapi._connect.presentation import device_row
from spotapi.models.playback import Device
Expand Down Expand Up @@ -292,3 +302,12 @@ def capture_issue(source: str, *, keep_list_focus: bool = False) -> None:
await pilot.press("escape")
await pilot.pause()
assert application.focused is application.query_one("#command", Input)

for width, height in ((80, 32), (60, 24), (40, 20), (120, 39)):
await pilot.resize_terminal(width, height)
await pilot.pause()
assert application.has_class("narrow") == (width < 90)
assert application.has_class("short") == (height < 30)
assert application.has_class("tiny") == (height < 23)
assert application.query_one("#command", Input).region.bottom <= height
assert application.focused is application.query_one("#command", Input)
Loading