Skip to content

Commit

Permalink
Add fish integration
Browse files Browse the repository at this point in the history
  • Loading branch information
turn1a committed Jun 11, 2024
1 parent 20405b2 commit b73e0d5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ShellGPT
A command-line productivity tool powered by AI large language models (LLM). This command-line tool offers streamlined generation of **shell commands, code snippets, documentation**, eliminating the need for external resources (like Google search). Supports Linux, macOS, Windows and compatible with all major Shells like PowerShell, CMD, Bash, Zsh, etc.
A command-line productivity tool powered by AI large language models (LLM). This command-line tool offers streamlined generation of **shell commands, code snippets, documentation**, eliminating the need for external resources (like Google search). Supports Linux, macOS, Windows and compatible with all major Shells like PowerShell, CMD, Bash, Zsh, fish etc.

https://github.com/TheR1D/shell_gpt/assets/16740832/9197283c-db6a-4b46-bfea-3eb776dd9093

Expand Down Expand Up @@ -104,11 +104,11 @@ sgpt -s "find all json files in current folder" --no-interaction | pbcopy


### Shell integration
This is a **very handy feature**, which allows you to use `sgpt` shell completions directly in your terminal, without the need to type `sgpt` with prompt and arguments. Shell integration enables the use of ShellGPT with hotkeys in your terminal, supported by both Bash and ZSH shells. This feature puts `sgpt` completions directly into terminal buffer (input line), allowing for immediate editing of suggested commands.
This is a **very handy feature**, which allows you to use `sgpt` shell completions directly in your terminal, without the need to type `sgpt` with prompt and arguments. Shell integration enables the use of ShellGPT with hotkeys in your terminal, supported by Bash, ZSH and fish shells. This feature puts `sgpt` completions directly into terminal buffer (input line), allowing for immediate editing of suggested commands.

https://github.com/TheR1D/shell_gpt/assets/16740832/bead0dab-0dd9-436d-88b7-6abfb2c556c1

To install shell integration, run `sgpt --install-integration` and 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 ShellGPT. When you press `Ctrl+l` it will replace you current input line (buffer) with suggested command. You can then edit it and just press `Enter` to execute.
To install shell integration, run `sgpt --install-integration` and restart your terminal to apply changes. This will add few lines to your `.bashrc`, `.zshrc` or `config.fish` file. After that, you can use `Ctrl+l` (default for ZSH and Bash) or `Ctrl+/` (default for fish) to invoke ShellGPT. When you press `Ctrl+l` or `Ctrl+/` it will replace you current input line (buffer) with suggested command. You can then edit it and just press `Enter` to execute.

### Generating code
By using the `--code` or `-c` parameter, you can specifically request pure code output, for instance:
Expand All @@ -127,7 +127,7 @@ for i in range(1, 101):
else:
print(i)
```
Since it is valid python code, we can redirect the output to a file:
Since it is valid python code, we can redirect the output to a file:
```shell
sgpt --code "solve classic fizz buzz problem using Python" > fizz_buzz.py
python fizz_buzz.py
Expand Down Expand Up @@ -163,7 +163,7 @@ for i in range(1, 101):
print(i)
```

### Chat Mode
### Chat Mode
Often it is important to preserve and recall a conversation. `sgpt` creates conversational dialogue with each LLM completion requested. The dialogue can develop one-by-one (chat mode) or interactively, in a REPL loop (REPL mode). Both ways rely on the same underlying object, called a chat session. The session is located at the [configurable](#runtime-configuration-file) `CHAT_CACHE_PATH`.

To start a conversation, use the `--chat` option followed by a unique session name and a prompt.
Expand Down Expand Up @@ -212,10 +212,10 @@ sgpt --chat conversation_3 "Convert the resulting file into an MP3"
# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3
```

To list all the sessions from either conversational mode, use the `--list-chats` or `-lc` option:
To list all the sessions from either conversational mode, use the `--list-chats` or `-lc` option:
```shell
sgpt --list-chats
# .../shell_gpt/chat_cache/conversation_1
# .../shell_gpt/chat_cache/conversation_1
# .../shell_gpt/chat_cache/conversation_2
```

Expand All @@ -228,7 +228,7 @@ sgpt --show-chat conversation_1
# assistant: Your favorite number is 4, so if we add 4 to it, the result would be 8.
```

### REPL Mode
### REPL Mode
There is very handy REPL (read–eval–print loop) mode, which allows you to interactively chat with GPT models. To start a chat session in REPL mode, use the `--repl` option followed by a unique session name. You can also use "temp" as a session name to start a temporary REPL session. Note that `--chat` and `--repl` are using same underlying object, so you can use `--chat` to start a chat session and then pick it up with `--repl` to continue the conversation in REPL mode.

<p align="center">
Expand Down Expand Up @@ -284,7 +284,7 @@ The snippet of code you've provided is written in Python. It prompts the user...
>>> Follow up questions...
```

### Function calling
### Function calling
[Function calls](https://platform.openai.com/docs/guides/function-calling) is a powerful feature OpenAI provides. It allows LLM to execute functions in your system, which can be used to accomplish a variety of tasks. To install [default functions](https://github.com/TheR1D/shell_gpt/tree/main/sgpt/default_functions/) run:
```shell
sgpt --install-functions
Expand Down Expand Up @@ -341,7 +341,7 @@ sgpt "Play music and open hacker news"
# -> Music is now playing, and Hacker News has been opened in your browser. Enjoy!
```

This is just a simple example of how you can use function calls. It is truly a powerful feature that can be used to accomplish a variety of complex tasks. We have dedicated [category](https://github.com/TheR1D/shell_gpt/discussions/categories/functions) in GitHub Discussions for sharing and discussing functions.
This is just a simple example of how you can use function calls. It is truly a powerful feature that can be used to accomplish a variety of complex tasks. We have dedicated [category](https://github.com/TheR1D/shell_gpt/discussions/categories/functions) in GitHub Discussions for sharing and discussing functions.
LLM might execute destructive commands, so please use it at your own risk❗️

### Roles
Expand Down
2 changes: 1 addition & 1 deletion sgpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def main(
),
install_integration: bool = typer.Option(
False,
help="Install shell integration (ZSH and Bash only)",
help="Install shell integration (ZSH, Bash and fish only)",
callback=install_shell_integration,
hidden=True, # Hiding since should be used only once.
),
Expand Down
35 changes: 35 additions & 0 deletions sgpt/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,38 @@
bindkey ^l _sgpt_zsh
# Shell-GPT integration ZSH v0.2
"""

fish_integration = """
# Shell-GPT integration fish v0.1
function _sgpt_fish
# Get the current command line content
set -l _sgpt_prev_cmd (commandline)
# Only proceed if there is a previous command
if test -n "$_sgpt_prev_cmd"
# Append an hourglass to the current command and repaint
commandline -a "⌛"
commandline -f repaint
# Get the output of the sgpt command
set -l _sgpt_output (echo "$_sgpt_prev_cmd" | sgpt --shell --no-interaction 2>/dev/null)
# Check if the sgpt command was successful
if test $status -eq 0
# Replace the command line with the output from sgpt
commandline -r -- (string trim "$_sgpt_output")
commandline -f end-of-line
commandline -f repaint
else
# If the sgpt command failed, remove the hourglass and display an error message
commandline -f backward-delete-char
commandline -f repaint
echo "sgpt command failed. Please check your sgpt installation and configuration."
end
end
end
# Bind command search using sgpt to CTRL+/
bind \\c_ _sgpt_fish
# Shell-GPT integration fish v0.1
"""
10 changes: 7 additions & 3 deletions sgpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from click import BadParameter, UsageError

from sgpt.__version__ import __version__
from sgpt.integration import bash_integration, zsh_integration
from sgpt.integration import bash_integration, fish_integration, zsh_integration


def get_edited_prompt() -> str:
Expand Down Expand Up @@ -66,7 +66,7 @@ def wrapper(cls: Any, value: str) -> None:
@option_callback
def install_shell_integration(*_args: Any) -> None:
"""
Installs shell integration. Currently only supports ZSH and Bash.
Installs shell integration. Currently only supports ZSH, bash and fish.
Allows user to get shell completions in terminal by using hotkey.
Replaces current "buffer" of the shell with the completion.
"""
Expand All @@ -81,8 +81,12 @@ def install_shell_integration(*_args: Any) -> None:
typer.echo("Installing Bash integration...")
with open(os.path.expanduser("~/.bashrc"), "a", encoding="utf-8") as file:
file.write(bash_integration)
elif "fish" in shell:
typer.echo("Installing Fish integration...")
with open(os.path.expanduser("~/.config/fish/config.fish"), "a", encoding="utf-8") as file:
file.write(fish_integration)
else:
raise UsageError("ShellGPT integrations only available for ZSH and Bash.")
raise UsageError("ShellGPT integrations only available for ZSH, Bash and fish.")

typer.echo("Done! Restart your shell to apply changes.")

Expand Down

0 comments on commit b73e0d5

Please sign in to comment.