From bd81f8be940cbdb07443e314dfba7a5d910a44c9 Mon Sep 17 00:00:00 2001 From: Eaad Date: Mon, 23 Sep 2024 16:50:47 +0300 Subject: [PATCH 1/4] Add modify option to prompt and update get_edited_prompt function to accept initial prompt text --- sgpt/app.py | 7 +++++-- sgpt/utils.py | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sgpt/app.py b/sgpt/app.py index 733b7ce4..fa78fb77 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -237,8 +237,8 @@ def main( while shell and interaction: option = typer.prompt( - text="[E]xecute, [D]escribe, [A]bort", - type=Choice(("e", "d", "a", "y"), case_sensitive=False), + text="[E]xecute, [D]escribe, [A]bort [M]odify", + type=Choice(("e", "d", "a", "y", "m"), case_sensitive=False), default="e" if cfg.get("DEFAULT_EXECUTE_SHELL_CMD") == "true" else "a", show_choices=False, show_default=False, @@ -256,6 +256,9 @@ def main( functions=function_schemas, ) continue + elif option == "m": + prompt = get_edited_prompt(full_completion) + run_command(prompt) break diff --git a/sgpt/utils.py b/sgpt/utils.py index d49af9a3..daf27f00 100644 --- a/sgpt/utils.py +++ b/sgpt/utils.py @@ -11,7 +11,7 @@ from sgpt.integration import bash_integration, zsh_integration -def get_edited_prompt() -> str: +def get_edited_prompt(prompt: str = "") -> str: """ Opens the user's default editor to let them input a prompt, and returns the edited text. @@ -21,6 +21,9 @@ def get_edited_prompt() -> str: with NamedTemporaryFile(suffix=".txt", delete=False) as file: # Create file and store path. file_path = file.name + if prompt: + file.write(prompt.encode("utf-8")) + file.close() editor = os.environ.get("EDITOR", "vim") # This will write text to file using $EDITOR. os.system(f"{editor} {file_path}") From 9d89d06f032d26c2224ff64de1bcd5740e77ade6 Mon Sep 17 00:00:00 2001 From: Eaad Date: Mon, 23 Sep 2024 16:52:31 +0300 Subject: [PATCH 2/4] Fix prompt text order in main function --- sgpt/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sgpt/app.py b/sgpt/app.py index fa78fb77..33ac3193 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -237,7 +237,7 @@ def main( while shell and interaction: option = typer.prompt( - text="[E]xecute, [D]escribe, [A]bort [M]odify", + text="[E]xecute, [D]escribe, [M]odify, [A]bort ", type=Choice(("e", "d", "a", "y", "m"), case_sensitive=False), default="e" if cfg.get("DEFAULT_EXECUTE_SHELL_CMD") == "true" else "a", show_choices=False, From 81fd730a9eab6fb6bd9c302f22c280652763a3ca Mon Sep 17 00:00:00 2001 From: Eaad Date: Mon, 23 Sep 2024 16:53:05 +0300 Subject: [PATCH 3/4] Fix prompt text spacing in main function --- sgpt/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sgpt/app.py b/sgpt/app.py index 33ac3193..874f6718 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -237,7 +237,7 @@ def main( while shell and interaction: option = typer.prompt( - text="[E]xecute, [D]escribe, [M]odify, [A]bort ", + text="[E]xecute, [D]escribe, [M]odify, [A]bort", type=Choice(("e", "d", "a", "y", "m"), case_sensitive=False), default="e" if cfg.get("DEFAULT_EXECUTE_SHELL_CMD") == "true" else "a", show_choices=False, From ea53be3423e0751047070085a3d2ea2e74e94269 Mon Sep 17 00:00:00 2001 From: Eaad Date: Fri, 11 Apr 2025 14:20:58 +0300 Subject: [PATCH 4/4] Add 'Run' command for Modify and Execute additional changes in tests to reflect the new command --- sgpt/app.py | 15 ++++++++++----- tests/test_shell.py | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/sgpt/app.py b/sgpt/app.py index 874f6718..3225ed78 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -237,8 +237,8 @@ def main( while shell and interaction: option = typer.prompt( - text="[E]xecute, [D]escribe, [M]odify, [A]bort", - type=Choice(("e", "d", "a", "y", "m"), case_sensitive=False), + text="[E]xecute, [D]escribe, [M]odify, [R]un (for Modify & Execute), [A]bort", + type=Choice(("e", "d", "a", "y", "m", "r"), case_sensitive=False), default="e" if cfg.get("DEFAULT_EXECUTE_SHELL_CMD") == "true" else "a", show_choices=False, show_default=False, @@ -256,9 +256,14 @@ def main( functions=function_schemas, ) continue - elif option == "m": - prompt = get_edited_prompt(full_completion) - run_command(prompt) + elif option == "m" or option == "r": + # modifying full_completion allows iterative manipulation. + full_completion = get_edited_prompt(full_completion) + DefaultHandler(role_class, md).printer(full_completion, False) + if option == "r": + run_command(full_completion) + break + continue break diff --git a/tests/test_shell.py b/tests/test_shell.py index b78e2c96..f226ab6b 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -19,7 +19,7 @@ def test_shell(completion): completion.assert_called_once_with(**comp_args(role, args["prompt"])) assert result.exit_code == 0 assert "git commit" in result.stdout - assert "[E]xecute, [D]escribe, [A]bort:" in result.stdout + assert "[E]xecute, [D]escribe, [M]odify, [R]un (for Modify & Execute), [A]bort:" in result.stdout @patch("sgpt.printer.TextPrinter.live_print") @@ -50,7 +50,7 @@ def test_shell_stdin(completion): completion.assert_called_once_with(**comp_args(role, expected_prompt)) assert result.exit_code == 0 assert "ls -l | sort" in result.stdout - assert "[E]xecute, [D]escribe, [A]bort:" in result.stdout + assert "[E]xecute, [D]escribe, [M]odify, [R]un (for Modify & Execute), [A]bort:" in result.stdout @patch("sgpt.handlers.handler.completion")