From 533c2f0ff8cad34540a125d92cec7b8b0b4e545f Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier Date: Mon, 16 Dec 2024 00:15:14 +0100 Subject: [PATCH] feat(shell): a first attempt --- shell/completion.zsh | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 shell/completion.zsh diff --git a/shell/completion.zsh b/shell/completion.zsh new file mode 100644 index 0000000..839f359 --- /dev/null +++ b/shell/completion.zsh @@ -0,0 +1,75 @@ +# tv-completion.zsh - A fuzzy finder script using `tv` +# This script provides fuzzy autocompletion for commands, paths, hosts, processes, and more. + +# Set the completion trigger (default: '**') +: "${TV_COMPLETION_TRIGGER=**}" + +# Options for `tv` during completion +: "${TV_COMPLETION_OPTS=--interactive}" +: "${TV_COMPLETION_PATH_OPTS=}" # Customize for file path search +: "${TV_COMPLETION_DIR_OPTS=}" # Customize for directory search + +# Function to restore Zsh options +_tv_restore_options() { + setopt "$@" 2>/dev/null +} + +# Custom file path generator +_tv_compgen_path() { + find . -type f 2>/dev/null +} + +# Custom directory generator +_tv_compgen_dir() { + find . -type d 2>/dev/null +} + +# General file and directory completion +_tv_path_completion() { + local result + result=$(tv $TV_COMPLETION_PATH_OPTS) + [[ -n $result ]] && zle -I && LBUFFER+="$result" +} + +_tv_dir_completion() { + local result + result=$(tv 'dirs' $TV_COMPLETION_DIR_OPTS) + echo $result + [[ -n $result ]] && zle -I && LBUFFER+="$result" +} + +# Generic fuzzy completion function +_tv_completion() { + local cmd_word="$1" + local result + + case "$cmd_word" in + export|unset) + result=$(printenv | cut -d= -f1 | tv $TV_COMPLETION_OPTS) + ;; + *) + if [[ -d ${LBUFFER##* } ]]; then + _tv_dir_completion + else + _tv_path_completion + fi + ;; + esac + + [[ -n $result ]] && zle -I && LBUFFER+="$result" +} + +# Hook into Zsh completion system +zle -C tv-completion complete-word _tv_completion +zle -N _tv_path_completion +zle -N _tv_dir_completion +bindkey '^I' tv-completion + +# Initialize tv completion trigger +zstyle ':completion:*' completer _tv_completion + +# Cleanup options when done +{ setopt localoptions; local _tv_restore_options } always { + _tv_restore_options "$@" +} +