Skip to content

Commit 20a26bc

Browse files
committed
feat: ctrl+y to copy command
1 parent 13c9188 commit 20a26bc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

trogon/trogon.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from trogon.widgets.command_tree import CommandTree
3434
from trogon.widgets.form import CommandForm
3535
from trogon.widgets.multiple_choice import NonFocusableVerticalScroll
36+
from subprocess import run
3637

3738
if sys.version_info >= (3, 8):
3839
from importlib import metadata
@@ -44,7 +45,8 @@ class CommandBuilder(Screen):
4445
COMPONENT_CLASSES = {"version-string", "prompt", "command-name-syntax"}
4546

4647
BINDINGS = [
47-
Binding(key="ctrl+r", action="close_and_run", description="Close & Run"),
48+
Binding(key="ctrl+r", action="close_and_run", description="Run Command"),
49+
Binding(key="ctrl+y", action="copy_command_string", description="Copy Command"),
4850
Binding(
4951
key="ctrl+t,escape", action="focus_command_tree", description="Focus Command Tree"
5052
),
@@ -131,6 +133,26 @@ def action_close_and_run(self) -> None:
131133
self.app.execute_on_exit = True
132134
self.app.exit()
133135

136+
def action_copy_command_string(self) -> None:
137+
cmd: list[str] = (
138+
["copy"]
139+
if sys.platform == 'win32'
140+
else ["pbcopy"]
141+
if sys.platform == 'darwin'
142+
else ["xclip", "-selection", "clipboard"]
143+
# if linux
144+
)
145+
146+
run(
147+
cmd,
148+
input=self.app_name + " " + " ".join(
149+
shlex.quote(str(x))
150+
for x in self.command_data.to_cli_args(redact_secret=True)
151+
),
152+
text=True,
153+
check=False,
154+
)
155+
134156
def action_exit(self) -> None:
135157
self.app.exit()
136158

0 commit comments

Comments
 (0)