-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcli.py
More file actions
243 lines (186 loc) · 6.14 KB
/
cli.py
File metadata and controls
243 lines (186 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from __future__ import annotations
import platform
import shlex
import click
from importlib_metadata import version
from textual.constants import DEVTOOLS_PORT
from .tools.run import exec_command, run_app
WINDOWS = platform.system() == "Windows"
"""True if we're running on Windows."""
@click.group()
@click.version_option(version("textual"))
def run() -> None:
pass
@run.command(help="Run the Textual Devtools console.")
@click.option(
"--port",
"port",
type=int,
default=None,
metavar="PORT",
help=f"Port to use for the development mode console. Defaults to {DEVTOOLS_PORT}.",
)
@click.option("-v", "verbose", help="Enable verbose logs.", is_flag=True)
@click.option("-x", "--exclude", "exclude", help="Exclude log group(s)", multiple=True)
def console(port: int | None, verbose: bool, exclude: list[str]) -> None:
"""Launch the textual console."""
from rich.console import Console
from textual_dev.server import _run_devtools
console = Console()
console.clear()
console.show_cursor(False)
try:
_run_devtools(verbose=verbose, exclude=exclude, port=port)
finally:
console.show_cursor(True)
def _pre_run_warnings() -> None:
"""Look for and report any issues with the environment.
This is the right place to add code that looks at the terminal, or other
environmental issues, and if a problem is seen it should be printed so
the developer can see it easily.
"""
import os
from rich.console import Console
from rich.panel import Panel
console = Console()
# Add any test/warning pair here. The list contains a tuple where the
# first item is `True` if a problem situation is detected, and the
# second item is a message to show the user on exit from `textual run`.
warnings = [
(
(
platform.system() == "Darwin"
and os.environ.get("TERM_PROGRAM") == "Apple_Terminal"
),
"The default terminal app on macOS is limited to 256 colors. See our FAQ for more details:\n\n"
"https://github.com/Textualize/textual/blob/main/FAQ.md#why-doesn't-textual-look-good-on-macos",
)
]
for concerning, concern in warnings:
if concerning:
console.print(
Panel.fit(
f"⚠️ {concern}", style="yellow", border_style="red", padding=(1, 2)
)
)
@run.command(
"run",
context_settings={
"ignore_unknown_options": True,
},
)
@click.argument("import_name", metavar="FILE or FILE:APP")
@click.option("--dev", "dev", help="Enable development mode.", is_flag=True)
@click.option(
"--port",
"port",
type=int,
default=None,
metavar="PORT",
help=f"Port to use for the development mode console. Defaults to {DEVTOOLS_PORT}.",
)
@click.option(
"--press", "press", default=None, help="Comma separated keys to simulate press."
)
@click.option(
"--screenshot",
type=int,
default=None,
metavar="DELAY",
help="Take screenshot after DELAY seconds.",
)
@click.option(
"-c",
"--command",
"command",
type=bool,
default=False,
help="Run as command rather that a file / module.",
is_flag=True,
)
@click.option(
"-r",
"--show-return",
"show_return",
type=bool,
default=False,
help="Show any return value on exit.",
is_flag=True,
)
@click.argument("extra_args", nargs=-1, type=click.UNPROCESSED)
def _run_app(
import_name: str,
dev: bool,
port: int | None,
press: str | None,
screenshot: int | None,
extra_args: tuple[str],
command: bool = False,
show_return: bool = False,
) -> None:
"""Run a Textual app.
The app to run may be given as a path (ending with .py) which will be equivalent to running the
script with python, or as a Python import which will import and run an app called "app".
In the case of an import, you can import and run an alternative app by appending a colon followed
by the name of the app instance or class.
Here are some examples:
textual run foo.py
textual run module.foo
textual run module.foo:MyApp
Add the --dev switch to enable the textual console.
textual run --dev foo.py
Use the -c switch to run a command that launches a Textual app.
textual run -c textual colors
"""
import os
from textual.features import parse_features
environment = dict(os.environ)
features = set(parse_features(environment.get("TEXTUAL", "")))
if dev:
features.add("debug")
features.add("devtools")
environment["TEXTUAL"] = ",".join(sorted(features))
if port is not None:
environment["TEXTUAL_DEVTOOLS_PORT"] = str(port)
if press is not None:
environment["TEXTUAL_PRESS"] = str(press)
if screenshot is not None:
environment["TEXTUAL_SCREENSHOT"] = str(screenshot)
if show_return:
environment["TEXTUAL_SHOW_RETURN"] = "1"
_pre_run_warnings()
import_name, *args = [*shlex.split(import_name, posix=not WINDOWS), *extra_args]
if command:
exec_command(import_name, args, environment)
else:
run_app(import_name, args, environment)
@run.command("borders")
def borders() -> None:
"""Explore the border styles available in Textual."""
from textual_dev.previews import BorderApp
BorderApp().run()
@run.command("easing")
def easing() -> None:
"""Explore the animation easing functions available in Textual."""
from textual_dev.previews import EasingApp
EasingApp().run()
@run.command("colors")
def colors() -> None:
"""Explore the design system."""
from textual_dev.previews import ColorsApp
ColorsApp().run()
@run.command("widgets")
def widgets() -> None:
"""Explore possible example_widgets."""
from textual_dev.previews import WidgetsApp
WidgetsApp().run()
@run.command("keys")
def keys() -> None:
"""Show key events."""
from textual_dev.previews import KeysApp
KeysApp().run()
@run.command("diagnose")
def run_diagnose() -> None:
"""Print information about the Textual environment."""
from textual_dev.tools.diagnose import diagnose
diagnose()