feat(shell): add bash support for smart autocomplete and shell history (#184)

Fixes #180
This commit is contained in:
Alex Pasmantier 2024-12-30 17:12:16 +01:00 committed by GitHub
parent 0b5facca6a
commit 7614fbc653
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 5 deletions

View File

@ -140,8 +140,9 @@ toggle_preview = "ctrl-o"
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# #
# The shell integration feature allows you to use television as a picker for # The shell integration feature allows you to use television as a picker for
# your shell commands. E.g. typing `git checkout <CTRL-T>` will open television # your shell commands (as well as your shell history with <CTRL-R>).
# with a list of branches to choose from. # E.g. typing `git checkout <CTRL-T>` will open television with a list of
# branches to choose from.
[shell_integration.commands] [shell_integration.commands]
# Add your commands here. Each key is a command that will trigger tv with the # Add your commands here. Each key is a command that will trigger tv with the
@ -160,9 +161,6 @@ toggle_preview = "ctrl-o"
# "cat" = "files" # "cat" = "files"
# ``` # ```
# shell history (according to your shell)
"" = "zsh-history"
# environment variables # environment variables
"export" = "env" "export" = "env"
"unset" = "env" "unset" = "env"

View File

@ -0,0 +1,24 @@
function tv_smart_autocomplete() {
local current_prompt="${READLINE_LINE:0:$READLINE_POINT}"
local output=$(tv --autocomplete-prompt "$current_prompt")
if [[ -n $output ]]; then
READLINE_LINE=$current_prompt$output
READLINE_POINT=${#READLINE_LINE}
fi
}
function tv_shell_history() {
local current_prompt="${READLINE_LINE:0:$READLINE_POINT}"
local output=$(tv bash-history --input "$current_prompt")
if [[ -n $output ]]; then
READLINE_LINE=$output
READLINE_POINT=${#READLINE_LINE}
fi
}
bind -x '"\C-t": tv_smart_autocomplete'
bind -x '"\C-r": tv_shell_history'

View File

@ -10,9 +10,11 @@ pub enum Shell {
} }
const COMPLETION_ZSH: &str = include_str!("../shell/completion.zsh"); const COMPLETION_ZSH: &str = include_str!("../shell/completion.zsh");
const COMPLETION_BASH: &str = include_str!("../shell/completion.bash");
pub fn completion_script(shell: Shell) -> Result<&'static str> { pub fn completion_script(shell: Shell) -> Result<&'static str> {
match shell { match shell {
Shell::Bash => Ok(COMPLETION_BASH),
Shell::Zsh => Ok(COMPLETION_ZSH), Shell::Zsh => Ok(COMPLETION_ZSH),
_ => color_eyre::eyre::bail!( _ => color_eyre::eyre::bail!(
"This shell is not yet supported: {:?}", "This shell is not yet supported: {:?}",