1) Uses textuals CSS_PATH and an external tcss file to bundle all DEFAULT_CSS into one file
2) Added keyboard shortcut bindings for switching the language (Ctrl-T) and showing the help (Ctrl-A) The help should use a markdown viewer 3) Include a test folder containing all the tests
This commit is contained in:
+80
-44
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
from pathlib import Path
|
||||
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.widgets import Button, Footer, Header, Input, Label, ProgressBar, Static
|
||||
from textual.worker import Worker, WorkerState
|
||||
@@ -13,54 +14,23 @@ from i18n import I18n
|
||||
|
||||
from . import ffmpeg_client, ytdlp_client
|
||||
from .models import FormatEntry, VideoInfo
|
||||
from .screens import ErrorScreen, FormatSelectScreen
|
||||
from .screens import ErrorScreen, FormatSelectScreen, HelpScreen, LanguageSelectScreen
|
||||
|
||||
|
||||
class DownloadManagerApp(App[None]):
|
||||
"""A textual UI for downloading a single film via yt-dlp."""
|
||||
|
||||
CSS = """
|
||||
#form Input {
|
||||
margin-bottom: 1;
|
||||
}
|
||||
# All widget styling (this screen plus every modal screen) lives in one
|
||||
# external stylesheet rather than scattered DEFAULT_CSS/CSS blocks.
|
||||
CSS_PATH = "styles.tcss"
|
||||
|
||||
.field-row {
|
||||
height: auto;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
.field-row Label {
|
||||
width: 10;
|
||||
padding-top: 1;
|
||||
}
|
||||
|
||||
.field-row Input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
#selection-area {
|
||||
height: auto;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
#progress-text {
|
||||
margin-top: 1;
|
||||
}
|
||||
|
||||
#progress-bar {
|
||||
width: 100%;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
#button-row {
|
||||
height: auto;
|
||||
align-horizontal: left;
|
||||
}
|
||||
|
||||
#button-row Button {
|
||||
margin-right: 1;
|
||||
}
|
||||
"""
|
||||
# `priority=True` makes these global: they fire even while an Input (or
|
||||
# any other widget with its own key bindings, e.g. Input's built-in
|
||||
# ctrl+a "select all") has focus.
|
||||
BINDINGS = [
|
||||
Binding("ctrl+t", "switch_language", "Language", priority=True),
|
||||
Binding("ctrl+a", "show_help", "Help", priority=True),
|
||||
]
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
@@ -80,10 +50,10 @@ class DownloadManagerApp(App[None]):
|
||||
yield Header()
|
||||
with Vertical(id="form"):
|
||||
with Horizontal(classes="field-row"):
|
||||
yield Label(t("label.url", default="Url"))
|
||||
yield Label(t("label.url", default="Url"), id="url-label")
|
||||
yield Input(id="url-input", placeholder=t("placeholder.url", default="https://..."))
|
||||
with Horizontal(classes="field-row"):
|
||||
yield Label(t("label.name", default="Name"))
|
||||
yield Label(t("label.name", default="Name"), id="name-label")
|
||||
yield Input(
|
||||
id="name-input",
|
||||
placeholder=t("placeholder.name", default="destination filename"),
|
||||
@@ -136,6 +106,72 @@ class DownloadManagerApp(App[None]):
|
||||
async def _show_error(self, message: str) -> None:
|
||||
await self.push_screen_wait(ErrorScreen(message))
|
||||
|
||||
# -- language switching & help -------------------------------------------
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.i18n.add_language_change_listener(self._on_language_changed)
|
||||
|
||||
def on_unmount(self) -> None:
|
||||
self.i18n.remove_language_change_listener(self._on_language_changed)
|
||||
|
||||
def _on_language_changed(self, code: str) -> None:
|
||||
self._refresh_translations()
|
||||
|
||||
def _refresh_translations(self) -> None:
|
||||
"""Re-apply translated text to every static/translated widget.
|
||||
|
||||
Called once after `I18n.set_language` notifies its listeners, since
|
||||
widgets built during `compose()` don't re-translate themselves.
|
||||
"""
|
||||
t = self.i18n.t
|
||||
self.title = t("app.title", default="yt-dlp Download Manager")
|
||||
|
||||
self.query_one("#url-label", Label).update(t("label.url", default="Url"))
|
||||
self.query_one("#url-input", Input).placeholder = t(
|
||||
"placeholder.url", default="https://..."
|
||||
)
|
||||
self.query_one("#name-label", Label).update(t("label.name", default="Name"))
|
||||
self.query_one("#name-input", Input).placeholder = t(
|
||||
"placeholder.name", default="destination filename"
|
||||
)
|
||||
|
||||
self.query_one("#audio-label", Static).update(
|
||||
self._format_selection_line("audio", self.selected_audio)
|
||||
)
|
||||
self.query_one("#video-label", Static).update(
|
||||
self._format_selection_line("video", self.selected_video)
|
||||
)
|
||||
|
||||
self.query_one("#select-parts-btn", Button).label = t(
|
||||
"button.select_parts", default="Select parts"
|
||||
)
|
||||
self.query_one("#start-download-btn", Button).label = t(
|
||||
"button.start_download", default="Start download"
|
||||
)
|
||||
self.query_one("#exit-btn", Button).label = t("button.exit", default="Exit")
|
||||
|
||||
# Only clobber the progress text with "Idle" while nothing is
|
||||
# running; an in-progress download keeps updating it anyway.
|
||||
if not self.query_one("#select-parts-btn", Button).disabled:
|
||||
self.query_one("#progress-text", Static).update(t("status.idle", default="Idle"))
|
||||
|
||||
def action_switch_language(self) -> None:
|
||||
# Bindings are dispatched outside of a worker context, but
|
||||
# `push_screen_wait` requires one, so the real work runs in a
|
||||
# worker.
|
||||
self.run_worker(self._switch_language(), exclusive=True, group="language")
|
||||
|
||||
async def _switch_language(self) -> None:
|
||||
chosen = await self.push_screen_wait(LanguageSelectScreen())
|
||||
if chosen is not None and chosen != self.i18n.get_language():
|
||||
self.i18n.set_language(chosen)
|
||||
|
||||
def action_show_help(self) -> None:
|
||||
self.run_worker(self._show_help(), exclusive=True, group="help")
|
||||
|
||||
async def _show_help(self) -> None:
|
||||
await self.push_screen_wait(HelpScreen())
|
||||
|
||||
# -- button wiring ------------------------------------------------------
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
|
||||
Reference in New Issue
Block a user