Skip to content

Commit

Permalink
feat(shell): add bash support for smart autocomplete and shell history (
Browse files Browse the repository at this point in the history
#184)

Fixes #180
  • Loading branch information
alexpasmantier authored Dec 30, 2024
1 parent 0b5facc commit 7614fbc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions .config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ toggle_preview = "ctrl-o"
# ----------------------------------------------------------------------------
#
# 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
# with a list of branches to choose from.
# your shell commands (as well as your shell history with <CTRL-R>).
# E.g. typing `git checkout <CTRL-T>` will open television with a list of
# branches to choose from.

[shell_integration.commands]
# Add your commands here. Each key is a command that will trigger tv with the
Expand All @@ -160,9 +161,6 @@ toggle_preview = "ctrl-o"
# "cat" = "files"
# ```

# shell history (according to your shell)
"" = "zsh-history"

# environment variables
"export" = "env"
"unset" = "env"
Expand Down
24 changes: 24 additions & 0 deletions crates/television-utils/shell/completion.bash
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 2 additions & 0 deletions crates/television-utils/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ pub enum Shell {
}

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> {
match shell {
Shell::Bash => Ok(COMPLETION_BASH),
Shell::Zsh => Ok(COMPLETION_ZSH),
_ => color_eyre::eyre::bail!(
"This shell is not yet supported: {:?}",
Expand Down

0 comments on commit 7614fbc

Please sign in to comment.