Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make on_mount callback customizable #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Make on_mount callback customizable
gitseti committed Dec 29, 2024
commit f704fde81ca1d375391048eb2e111eff331f4a7f
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
@@ -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
@@ -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
@@ -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)
@@ -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,
@@ -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)