Skip to content

Commit

Permalink
Merge pull request #14 from evanmags/main
Browse files Browse the repository at this point in the history
feat: Add support for Typer
  • Loading branch information
daneah authored Sep 20, 2024
2 parents d788db6 + 1f90e81 commit b2f36dd
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 61 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ pip install trogon

## Quickstart

### Click
1. Import `from trogon import tui`
2. Add the `@tui` decorator above your click app. e.g.
2. Add the `@tui` decorator above your click app, e.g.
```python
from trogon import tui

Expand All @@ -102,6 +103,15 @@ pip install trogon
```
3. Your click app will have a new `tui` command available.

### Typer
1. Import `from trogon.typer import init_tui`
2. Pass your Typer CLI app into the `init_tui` function, e.g.
```python
cli = typer.Typer(...)
init_tui(cli)
```
3. Your Typer app will have a new `tui` command available.

See also the `examples` folder for two example apps.

## Custom command name and custom help
Expand Down
151 changes: 91 additions & 60 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ python = "^3.8"
textual = ">=0.61.0"
click = ">=8.0.0"

typer = {version = ">=0.9.0", optional = true}

[tool.poetry.extras]
typer = ["typer"]


[tool.poetry.group.dev.dependencies]
mypy = "^1.2.0"
Expand Down
23 changes: 23 additions & 0 deletions trogon/typer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import click

try:
import typer
except ImportError:
raise ImportError(
"The extra `trogon[typer]` is required to enable tui generation from Typer apps."
)

from trogon.trogon import Trogon


def init_tui(app: typer.Typer, name: str | None = None):
def wrapped_tui():
Trogon(
typer.main.get_group(app),
app_name=name,
click_context=click.get_current_context(),
).run()

app.command("tui", help="Open Textual TUI.")(wrapped_tui)

return app

0 comments on commit b2f36dd

Please sign in to comment.