Skip to content

Add fish shell support #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Upgrading to the latest version of ShellOracle is just as simple!

## Usage

ShellOracle is designed to be used as a BASH/ZSH widget activated by the CTRL+F keyboard shortcut.
ShellOracle is designed to be used as a BASH/ZSH/fish widget activated by the CTRL+F keyboard shortcut.

1. Press CTRL+F
2. Describe your command
Expand Down Expand Up @@ -122,7 +122,7 @@ behavior.

### Software

ShellOracle supports BASH and ZSH on macOS and Linux.
ShellOracle supports BASH, ZSH and fish on macOS and Linux.

### Hardware

Expand Down
26 changes: 17 additions & 9 deletions src/shelloracle/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import shutil
from pathlib import Path
import os
from typing import TYPE_CHECKING, Any

import tomlkit
Expand Down Expand Up @@ -38,7 +39,7 @@ def replace_home_with_tilde(path: Path) -> Path:
return Path("~") / relative_path


supported_shells = ("zsh", "bash")
supported_shells = ("zsh", "bash", "fish")


def get_installed_shells() -> list[str]:
Expand All @@ -49,18 +50,24 @@ def get_bundled_script_path(shell: str) -> Path:
shell_dir = Path(__file__).parent / "shell"
if shell == "zsh":
return shell_dir / "shelloracle.zsh"
if shell == "fish":
return shell_dir / "shelloracle.fish"
return shell_dir / "shelloracle.bash"


def get_script_path(shell: str) -> Path:
if shell == "zsh":
return Path.home() / ".shelloracle.zsh"
if shell == "fish":
return Path.home() / ".shelloracle.fish"
return Path.home() / ".shelloracle.bash"


def get_rc_path(shell: str) -> Path:
if shell == "zsh":
return Path.home() / ".zshrc"
if shell == "fish":
return Path.home() / ".config/fish/config.fish"
return Path.home() / ".bashrc"


Expand All @@ -75,10 +82,13 @@ def update_rc(shell: str) -> None:
rc_path = get_rc_path(shell)
rc_path.touch(exist_ok=True)
with rc_path.open("r") as file:
zshrc = file.read()
shelloracle_script = get_script_path(shell)
line = f"[ -f {shelloracle_script} ] && source {shelloracle_script}"
if line not in zshrc:
rc_content = file.read()
if shell == "fish":
line = f"source {get_script_path(shell)}"
else:
shelloracle_script = get_script_path(shell)
line = f"[ -f {shelloracle_script} ] && source {shelloracle_script}"
if line not in rc_content:
with rc_path.open("a") as file:
file.write("\n")
file.write(line)
Expand Down Expand Up @@ -119,16 +129,14 @@ def install_keybindings() -> None:
if not (shells := get_installed_shells()):
print_warning(
"Cannot install keybindings: no compatible shells found. "
f"Supported shells: {', '.join(supported_shells)}"
f"Supported shells: {' '.join(supported_shells)}"
)
return
if confirm("Enable terminal keybindings and update rc?", suffix=" ([y]/n) ") is False:
return
for shell in shells:
for shell in get_installed_shells():
write_script_home(shell)
update_rc(shell)


def user_configure_settings(provider: type[Provider]) -> dict[str, Any]:
settings = {}
for name, setting in get_settings(provider):
Expand Down
4 changes: 2 additions & 2 deletions src/shelloracle/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from collections.abc import AsyncIterator

system_prompt = (
"Based on the following user description, generate a corresponding Bash command. Focus solely "
"Based on the following user description, generate a corresponding shell command. Focus solely "
"on interpreting the requirements and translating them into a single, executable Bash command. "
"Ensure accuracy and relevance to the user's description. The output should be a valid Bash "
"Ensure accuracy and relevance to the user's description. The output should be a valid shell "
"command that directly aligns with the user's intent, ready for execution in a command-line "
"environment. Do not output anything except for the command. No code block, no English explanation, "
"no newlines, and no start/end tags."
Expand Down
9 changes: 9 additions & 0 deletions src/shelloracle/shell/shelloracle.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function __shelloracle__
set -l output (shor)
if test $status -ne 0
return $status
end
commandline -r -- $output
end

bind \cf __shelloracle__
Loading