feat: add shell integration (#323)

This commit is contained in:
sigoden 2024-02-26 11:28:25 +08:00 committed by GitHub
parent aec1b707af
commit ff0ec15e06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 53 additions and 0 deletions

View File

@ -394,6 +394,22 @@ We can also pipe the output of aichat which will disable interactive mode.
aichat -e find all json files in current folder | pbcopy aichat -e find all json files in current folder | pbcopy
``` ```
### Shell integration
This is a **very handy feature**, which allows you to use `aichat` shell completions directly in your terminal, without the need to type `aichat` with prompt and arguments. This feature puts `aichat` completions directly into terminal buffer (input line), allowing for immediate editing of suggested commands.
![aichat-integration](https://github.com/sigoden/aichat/assets/4012553/9a9b17ea-977a-4bd8-8182-a0a96627573c)
To install shell integration, run the following code:
```sh
sh_ext=bash # possible values: bash, fish, zsh, ps1
curl -o aichat-integration.$sh_ext https://raw.githubusercontent.com/sigoden/aichat/main/scripts/integration.$sh_ext
. aichat-integration.$sh_ext
```
After that restart your shell. You can invoke the completion with `alt+e` hotkey.
## License ## License
Copyright (c) 2023-2024 aichat-developers. Copyright (c) 2023-2024 aichat-developers.

7
scripts/integration.bash Normal file
View File

@ -0,0 +1,7 @@
_aichat_bash() {
if [[ -n "$READLINE_LINE" ]]; then
READLINE_LINE=$(aichat -e "$READLINE_LINE")
READLINE_POINT=${#READLINE_LINE}
fi
}
bind -x '"\ee": _aichat_bash'

9
scripts/integration.fish Normal file
View File

@ -0,0 +1,9 @@
function _aichat_fish
set -l _old (commandline)
if test -n $_old
echo -n "⌛"
commandline -f repaint
commandline (aichat -e $_old)
end
end
bind \ee _aichat_fish

10
scripts/integration.ps1 Normal file
View File

@ -0,0 +1,10 @@
Set-PSReadLineKeyHandler -Chord "alt+e" -ScriptBlock {
$_old = $null
[Microsoft.PowerShell.PSConsoleReadline]::GetBufferState([ref]$_old, [ref]$null)
if ($_old) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert('⌛')
$_new = (aichat -e $_old)
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
[Microsoft.PowerShell.PSConsoleReadline]::Insert($_new)
}
}

11
scripts/integration.zsh Normal file
View File

@ -0,0 +1,11 @@
_aichat_zsh() {
if [[ -n "$BUFFER" ]]; then
local _old=$BUFFER
BUFFER+="⌛"
zle -I && zle redisplay
BUFFER=$(aichat -e "$_old")
zle end-of-line
fi
}
zle -N _aichat_zsh
bindkey '\ee' _aichat_zsh