Skip to content

Commit 2197a70

Browse files
committed
polyfill for orig_argv
1 parent 93c099b commit 2197a70

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

examples/argv.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import sys
2+
print(sys.orig_argv)

trogon/detect_run_string.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
from __future__ import annotations
2+
13
import os
24
import shlex
35
import sys
46

57

6-
def detect_run_string(path=None, _main=sys.modules["__main__"]):
7-
"""This is a slightly modified version of a function from Click.
8-
"""
8+
def get_orig_argv() -> list[str]:
9+
"""Polyfil for orig_argv"""
10+
if hasattr(sys, "orig_argv"):
11+
return sys.orig_argv
12+
import ctypes
13+
14+
_argv = ctypes.POINTER(ctypes.c_wchar_p)()
15+
_argc = ctypes.c_int()
16+
17+
ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(_argc), ctypes.byref(_argv))
18+
19+
argv = _argv[: _argc.value]
20+
return argv
21+
22+
23+
def detect_run_string(path=None, _main=sys.modules["__main__"]) -> str:
24+
"""This is a slightly modified version of a function from Click."""
925
if not path:
1026
path = sys.argv[0]
1127

@@ -20,8 +36,9 @@ def detect_run_string(path=None, _main=sys.modules["__main__"]):
2036
):
2137
# Executed a file, like "python app.py".
2238
file_path = shlex.quote(os.path.basename(path))
23-
if sys.orig_argv[0] == "python":
24-
prefix = f"{sys.orig_argv[0]} "
39+
argv = get_orig_argv()
40+
if argv[0] == "python":
41+
prefix = f"{argv[0]} "
2542
else:
2643
prefix = ""
2744
return f"{prefix}{file_path}"

trogon/trogon.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class CommandBuilder(Screen):
5252
),
5353
Binding(key="ctrl+o", action="show_command_info", description="Command Info"),
5454
Binding(key="f1", action="about", description="About Trogon"),
55+
Binding(
56+
key="ctrl+s",
57+
action="focus('search')",
58+
description="Search",
59+
),
5560
]
5661

5762
def __init__(

trogon/widgets/form.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,19 @@ def compose(self) -> ComposeResult:
104104
with VerticalScroll() as vs:
105105
vs.can_focus = False
106106

107-
yield Input(placeholder="Filter parameters...", classes="command-form-filter-input")
107+
yield Input(
108+
placeholder="Search...",
109+
classes="command-form-filter-input",
110+
id="search",
111+
)
108112

109113
while command_node is not None:
110114
options = command_node.options
111115
arguments = command_node.arguments
112116
if options or arguments:
113-
with Vertical(classes="command-form-command-group", id=command_node.key) as v:
117+
with Vertical(
118+
classes="command-form-command-group", id=command_node.key
119+
) as v:
114120
is_inherited = command_node is not self.command_schema
115121
v.border_title = (
116122
f"{'↪ ' if is_inherited else ''}{command_node.name}"
@@ -168,9 +174,7 @@ def _form_changed(self) -> None:
168174
# For each of the options in the schema for this command,
169175
# lets grab the values the user has supplied for them in the form.
170176
for option in command.options:
171-
parameter_control = self.query_one(
172-
f"#{option.key}", ParameterControls
173-
)
177+
parameter_control = self.query_one(f"#{option.key}", ParameterControls)
174178
value = parameter_control.get_values()
175179
for v in value.values:
176180
assert isinstance(v, tuple)
@@ -191,9 +195,7 @@ def _form_changed(self) -> None:
191195
argument_datas.append(argument_data)
192196

193197
assert all(isinstance(option.value, tuple) for option in option_datas)
194-
assert all(
195-
isinstance(argument.value, tuple) for argument in argument_datas
196-
)
198+
assert all(isinstance(argument.value, tuple) for argument in argument_datas)
197199
command_data = UserCommandData(
198200
name=command.name,
199201
options=option_datas,

0 commit comments

Comments
 (0)