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:
2026-09-19 14:28:40 +02:00
parent 701ff3a6d3
commit fccd2def59
20 changed files with 1118 additions and 107 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
from .error_screen import ErrorScreen
from .format_select_screen import FormatSelectScreen
from .help_screen import HelpScreen
from .language_select_screen import LanguageSelectScreen
__all__ = ["ErrorScreen", "FormatSelectScreen"]
__all__ = ["ErrorScreen", "FormatSelectScreen", "HelpScreen", "LanguageSelectScreen"]
+2 -29
View File
@@ -13,35 +13,8 @@ from i18n import I18n
class ErrorScreen(ModalScreen[None]):
"""Modal dialog showing a title and a message, dismissed via OK."""
DEFAULT_CSS = """
ErrorScreen {
align: center middle;
}
ErrorScreen > Container {
width: 60%;
max-width: 70;
height: auto;
border: thick $error 80%;
background: $surface;
padding: 1 2;
}
ErrorScreen #error-title {
text-style: bold;
color: $error;
margin-bottom: 1;
}
ErrorScreen #error-message {
margin-bottom: 1;
}
ErrorScreen Horizontal {
align-horizontal: center;
height: auto;
}
"""
# Styling for this screen lives in the app's shared external stylesheet
# (app/styles.tcss), loaded via DownloadManagerApp.CSS_PATH.
def __init__(self, message: str, *, title: str | None = None) -> None:
super().__init__()
+2 -31
View File
@@ -24,37 +24,8 @@ class FormatSelectScreen(ModalScreen[FormatEntry | None]):
can treat that as "no valid entry selected".
"""
DEFAULT_CSS = """
FormatSelectScreen {
align: center middle;
}
FormatSelectScreen > Container {
width: 90%;
max-width: 120;
height: auto;
max-height: 80%;
border: thick $primary 80%;
background: $surface;
padding: 1 2;
}
FormatSelectScreen #dialog-title {
text-style: bold;
margin-bottom: 1;
}
FormatSelectScreen DataTable {
height: auto;
max-height: 20;
margin-bottom: 1;
}
FormatSelectScreen Horizontal {
align-horizontal: center;
height: auto;
}
"""
# Styling for this screen lives in the app's shared external stylesheet
# (app/styles.tcss), loaded via DownloadManagerApp.CSS_PATH.
BINDINGS = [("escape", "cancel", "Cancel")]
+38
View File
@@ -0,0 +1,38 @@
"""Modal dialog showing the current language's help text as Markdown."""
from __future__ import annotations
from textual.app import ComposeResult
from textual.containers import Container, Horizontal
from textual.screen import ModalScreen
from textual.widgets import Button, Label, MarkdownViewer
from i18n import I18n
class HelpScreen(ModalScreen[None]):
"""Renders ``I18n.get_help()`` in a scrollable Markdown viewer."""
# Styling for this screen lives in the app's shared external stylesheet
# (app/styles.tcss), loaded via DownloadManagerApp.CSS_PATH.
BINDINGS = [("escape", "close", "Close")]
def compose(self) -> ComposeResult:
i18n = I18n()
try:
help_text = i18n.get_help()
except FileNotFoundError:
help_text = i18n.t("dialog.help_missing", default="No help is available.")
with Container():
yield Label(i18n.t("dialog.help_title", default="Help"), id="help-title")
yield MarkdownViewer(help_text, show_table_of_contents=False)
with Horizontal():
yield Button(i18n.t("button.close", default="Close"), id="help-close")
def on_button_pressed(self, event: Button.Pressed) -> None:
self.dismiss(None)
def action_close(self) -> None:
self.dismiss(None)
+48
View File
@@ -0,0 +1,48 @@
"""Modal dialog for picking one of the application's available languages."""
from __future__ import annotations
from textual.app import ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.screen import ModalScreen
from textual.widgets import Button, Label
from i18n import I18n
class LanguageSelectScreen(ModalScreen[str | None]):
"""Shows one button per available language; returns the chosen code.
Returns ``None`` if the dialog is cancelled (Escape or the Cancel
button), leaving the current language unchanged.
"""
# Styling for this screen lives in the app's shared external stylesheet
# (app/styles.tcss), loaded via DownloadManagerApp.CSS_PATH.
BINDINGS = [("escape", "cancel", "Cancel")]
def compose(self) -> ComposeResult:
i18n = I18n()
with Container():
yield Label(
i18n.t("dialog.select_language", default="Select language"),
id="language-title",
)
with Vertical(id="language-options"):
for code, display_name in i18n.get_available_languages().items():
variant = "primary" if code == i18n.get_language() else "default"
yield Button(display_name, id=f"lang-{code}", variant=variant)
with Horizontal():
yield Button(i18n.t("button.cancel", default="Cancel"), id="language-cancel")
def on_button_pressed(self, event: Button.Pressed) -> None:
button_id = event.button.id or ""
if button_id == "language-cancel":
self.dismiss(None)
return
if button_id.startswith("lang-"):
self.dismiss(button_id.removeprefix("lang-"))
def action_cancel(self) -> None:
self.dismiss(None)
+180
View File
@@ -0,0 +1,180 @@
/* ---------------------------------------------------------------------
* Styles for DownloadManagerApp (main screen)
* ------------------------------------------------------------------- */
#form Input {
margin-bottom: 1;
}
.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;
}
/* ---------------------------------------------------------------------
* ErrorScreen
* ------------------------------------------------------------------- */
ErrorScreen {
align: center middle;
}
ErrorScreen > Container {
width: 60%;
max-width: 70;
height: auto;
border: thick $error 80%;
background: $surface;
padding: 1 2;
}
ErrorScreen #error-title {
text-style: bold;
color: $error;
margin-bottom: 1;
}
ErrorScreen #error-message {
margin-bottom: 1;
}
ErrorScreen Horizontal {
align-horizontal: center;
height: auto;
}
/* ---------------------------------------------------------------------
* FormatSelectScreen
* ------------------------------------------------------------------- */
FormatSelectScreen {
align: center middle;
}
FormatSelectScreen > Container {
width: 90%;
max-width: 120;
height: auto;
max-height: 80%;
border: thick $primary 80%;
background: $surface;
padding: 1 2;
}
FormatSelectScreen #dialog-title {
text-style: bold;
margin-bottom: 1;
}
FormatSelectScreen DataTable {
height: auto;
max-height: 20;
margin-bottom: 1;
}
FormatSelectScreen Horizontal {
align-horizontal: center;
height: auto;
}
/* ---------------------------------------------------------------------
* LanguageSelectScreen
* ------------------------------------------------------------------- */
LanguageSelectScreen {
align: center middle;
}
LanguageSelectScreen > Container {
width: 50%;
max-width: 50;
height: auto;
border: thick $primary 80%;
background: $surface;
padding: 1 2;
}
LanguageSelectScreen #language-title {
text-style: bold;
margin-bottom: 1;
}
LanguageSelectScreen Vertical#language-options {
height: auto;
margin-bottom: 1;
}
LanguageSelectScreen Button {
width: 100%;
margin-bottom: 1;
}
LanguageSelectScreen Horizontal {
align-horizontal: center;
height: auto;
}
/* ---------------------------------------------------------------------
* HelpScreen
* ------------------------------------------------------------------- */
HelpScreen {
align: center middle;
}
HelpScreen > Container {
width: 80%;
max-width: 100;
height: 80%;
border: thick $primary 80%;
background: $surface;
padding: 1 2;
}
HelpScreen #help-title {
text-style: bold;
margin-bottom: 1;
height: auto;
}
HelpScreen MarkdownViewer {
height: 1fr;
margin-bottom: 1;
}
HelpScreen Horizontal {
align-horizontal: center;
height: auto;
}
+80 -44
View File
@@ -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: