Skip to content

Commit

Permalink
Simple shell Integrations (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheR1D committed Jun 4, 2023
1 parent 0129e59 commit cb9782b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sgpt/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 12 additions & 1 deletion sgpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()

Expand Down
16 changes: 16 additions & 0 deletions sgpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"')

0 comments on commit cb9782b

Please sign in to comment.