Skip to content
Open
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
17 changes: 17 additions & 0 deletions examples/change_default_theme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import click
from textual.app import App

from trogon import tui


def on_mount(app: App):
app.theme = 'tokyo-night'

@tui(on_mount=on_mount)
@click.command()
def hello_world():
"""Prints 'Hello world'."""
click.echo('Hello world.')

if __name__ == "__main__":
hello_world()
12 changes: 9 additions & 3 deletions trogon/trogon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shlex
from importlib import metadata # type: ignore
from pathlib import Path
from typing import Any
from typing import Any, Callable, Optional
from webbrowser import open as open_url

import click
Expand Down Expand Up @@ -207,6 +207,7 @@ def __init__(
app_name: str | None = None,
command_name: str = "tui",
click_context: click.Context | None = None,
on_mount_callback: Optional[Callable[[App], None]] = None
) -> None:
super().__init__()
self.cli = cli
Expand All @@ -218,6 +219,7 @@ def __init__(
else:
self.app_name = app_name or "cli"
self.command_name = command_name
self.on_mount_callback = on_mount_callback

def get_default_screen(self) -> CommandBuilder:
return CommandBuilder(self.cli, self.app_name, self.command_name)
Expand All @@ -227,6 +229,10 @@ def on_button_pressed(self):
self.execute_on_exit = True
self.exit()

def on_mount(self) -> None:
if self.on_mount_callback is not None:
self.on_mount_callback(self)

def run(
self,
*args: Any,
Expand Down Expand Up @@ -282,11 +288,11 @@ def action_visit(self, url: str) -> None:
open_url(url)


def tui(name: str | None = None, command: str = "tui", help: str = "Open Textual TUI."):
def tui(name: str | None = None, command: str = "tui", help: str = "Open Textual TUI.", on_mount: Optional[Callable[[App], None]] = None):
def decorator(app: click.Group | click.Command):
@click.pass_context
def wrapped_tui(ctx, *args, **kwargs):
Trogon(app, app_name=name, command_name=command, click_context=ctx).run()
Trogon(app, app_name=name, command_name=command, click_context=ctx, on_mount_callback=on_mount).run()

if isinstance(app, click.Group):
app.command(name=command, help=help)(wrapped_tui)
Expand Down