From cb9782b74db038aef04ce750a5938a7e84a57a23 Mon Sep 17 00:00:00 2001 From: Farkhod Sadykov Date: Mon, 5 Jun 2023 01:38:28 +0200 Subject: [PATCH] Simple shell Integrations (#267) --- README.md | 14 +++++++++++++- sgpt/__init__.py | 2 +- sgpt/app.py | 13 ++++++++++++- sgpt/utils.py | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 177692a6..07f77754 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ https://user-images.githubusercontent.com/16740832/231569156-a3a9f9d4-18b1-4fff- ## Installation ```shell -pip install shell-gpt==0.9.1 +pip install shell-gpt==0.9.2 ``` You'll need an OpenAI API key, you can generate one [here](https://beta.openai.com/account/api-keys). @@ -92,6 +92,18 @@ sgpt -s "using ffmpeg combine multiple videos into one without audio. Video file # -> [E]xecute, [D]escribe, [A]bort: e ... ``` +### Shell integration +Shell integration allows you to use Shell-GPT in your terminal with hotkeys. It is currently available for bash and zsh. It will allow you to have sgpt completions in your shell history, and also edit suggested commands right away. + +https://github.com/TheR1D/shell_gpt/assets/16740832/bead0dab-0dd9-436d-88b7-6abfb2c556c1 + +To install shell integration, run: +```shell +sgpt --install-integration +# Restart your terminal to apply changes. +``` +This will add few lines to your `.bashrc` or `.zshrc` file. After that, you can use `Ctrl+l` (by default) to invoke Shell-GPT. When you press `Ctrl+l` it will replace you current input line (buffer) with suggested command. You can then edit it and press `Enter` to execute. + ### Generating code With `--code` parameters we can query only code as output, for example: ```shell diff --git a/sgpt/__init__.py b/sgpt/__init__.py index 27f0b527..7cc0b00b 100644 --- a/sgpt/__init__.py +++ b/sgpt/__init__.py @@ -1,4 +1,4 @@ from .app import main as main from .app import entry_point as cli # noqa: F401 -__version__ = "0.9.1" +__version__ = "0.9.2" diff --git a/sgpt/app.py b/sgpt/app.py index 7dd8680c..1362d7bc 100644 --- a/sgpt/app.py +++ b/sgpt/app.py @@ -18,7 +18,12 @@ from sgpt.handlers.default_handler import DefaultHandler from sgpt.handlers.repl_handler import ReplHandler from sgpt.role import DefaultRoles, SystemRole -from sgpt.utils import ModelOptions, get_edited_prompt, run_command +from sgpt.utils import ( + ModelOptions, + get_edited_prompt, + install_shell_integration, + run_command, +) def main( @@ -115,6 +120,12 @@ def main( callback=SystemRole.list, rich_help_panel="Role Options", ), + install_integration: bool = typer.Option( + False, + help="Install shell integration (ZSH and Bash only)", + callback=install_shell_integration, + hidden=True, # Hiding since should be used only once. + ), ) -> None: stdin_passed = not sys.stdin.isatty() diff --git a/sgpt/utils.py b/sgpt/utils.py index 89ebf3e8..c40d821b 100644 --- a/sgpt/utils.py +++ b/sgpt/utils.py @@ -65,3 +65,19 @@ def wrapper(cls: Any, value: str) -> None: raise typer.Exit() return wrapper + + +@option_callback +def install_shell_integration(*_args: Any) -> None: + """ + Installs shell integration. Currently only supports Linux. + Allows user to get shell completions in terminal by using hotkey. + Allows user to edit shell command right away in terminal. + """ + # TODO: Add support for Windows. + # TODO: Implement updates. + if platform.system() == "Windows": + typer.echo("Windows is not supported yet.") + else: + url = "https://raw.githubusercontent.com/TheR1D/shell_gpt/shell-integrations/install.sh" + os.system(f'sh -c "$(curl -fsSL {url})"')