mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-06 03:25:23 +00:00
feat(shell): improve zsh completion system
This commit is contained in:
parent
dfbdd65107
commit
0b9359856d
@ -1,3 +1,6 @@
|
|||||||
|
# credits to the junegunn/fzf project
|
||||||
|
# https://github.com/junegunn/fzf/blob/d18c0bf6948b4707684fe77631aff26a17cbc4fa/shell/completion.zsh
|
||||||
|
|
||||||
_disable_bracketed_paste() {
|
_disable_bracketed_paste() {
|
||||||
# Check if bracketed paste is defined, for compatibility with older versions
|
# Check if bracketed paste is defined, for compatibility with older versions
|
||||||
if [[ -n $zle_bracketed_paste ]]; then
|
if [[ -n $zle_bracketed_paste ]]; then
|
||||||
@ -12,36 +15,70 @@ _enable_bracketed_paste() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_tv_smart_autocomplete() {
|
__tv_path_completion() {
|
||||||
emulate -L zsh
|
local base lbuf suffix tail dir leftover matches
|
||||||
zle -I
|
base=$1
|
||||||
|
lbuf=$2
|
||||||
|
suffix=""
|
||||||
|
tail=" "
|
||||||
|
|
||||||
_disable_bracketed_paste
|
eval "base=$base" 2> /dev/null || return
|
||||||
|
[[ $base = *"/"* ]] && dir="$base"
|
||||||
# prefix (lhs of cursor)
|
while [ 1 ]; do
|
||||||
local current_prompt
|
if [[ -z "$dir" || -d ${dir} ]]; then
|
||||||
current_prompt=$LBUFFER
|
leftover=${base/#"$dir"}
|
||||||
|
leftover=${leftover/#\/}
|
||||||
local output
|
[ -z "$dir" ] && dir='.'
|
||||||
output=$(tv --autocomplete-prompt "$current_prompt" $* | tr '\n' ' ')
|
[ "$dir" != "/" ] && dir="${dir/%\//}"
|
||||||
|
matches=$(
|
||||||
if [[ -n $output ]]; then
|
shift
|
||||||
# suffix (rhs of cursor)
|
tv "$dir" --autocomplete-prompt "$lbuf" --input "$leftover" < /dev/tty | while read -r item; do
|
||||||
local rhs=$RBUFFER
|
item="${item%$suffix}$suffix"
|
||||||
# add a space if the prompt does not end with one
|
dirP="$dir/"
|
||||||
[[ "${current_prompt}" != *" " ]] && current_prompt="${current_prompt} "
|
[[ $dirP = "./" ]] && dirP=""
|
||||||
|
echo -n -E "$dirP${(q)item} "
|
||||||
LBUFFER=$current_prompt$output
|
done
|
||||||
CURSOR=${#LBUFFER}
|
)
|
||||||
RBUFFER=$rhs
|
matches=${matches% }
|
||||||
|
if [ -n "$matches" ]; then
|
||||||
zle reset-prompt
|
LBUFFER="$lbuf$matches$tail"
|
||||||
# uncomment this to automatically accept the line
|
fi
|
||||||
# (i.e. run the command without having to press enter twice)
|
zle reset-prompt
|
||||||
# zle accept-line
|
break
|
||||||
fi
|
fi
|
||||||
|
dir=$(dirname "$dir")
|
||||||
|
dir=${dir%/}/
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
_enable_bracketed_paste
|
_tv_smart_autocomplete() {
|
||||||
|
_disable_bracketed_paste
|
||||||
|
|
||||||
|
local tokens prefix trigger lbuf
|
||||||
|
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
|
||||||
|
|
||||||
|
# http://zsh.sourceforge.net/FAQ/zshfaq03.html
|
||||||
|
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
|
||||||
|
tokens=(${(z)LBUFFER})
|
||||||
|
if [ ${#tokens} -lt 1 ]; then
|
||||||
|
zle ${fzf_default_completion:-expand-or-complete}
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
[[ ${LBUFFER[-1]} == ' ' ]] && tokens+=("")
|
||||||
|
|
||||||
|
if [[ ${LBUFFER} = *"${tokens[-2]-}${tokens[-1]}" ]]; then
|
||||||
|
tokens[-2]="${tokens[-2]-}${tokens[-1]}"
|
||||||
|
tokens=(${tokens[0,-2]})
|
||||||
|
fi
|
||||||
|
|
||||||
|
lbuf=$LBUFFER
|
||||||
|
prefix=${tokens[-1]}
|
||||||
|
[ -n "${tokens[-1]}" ] && lbuf=${lbuf:0:-${#tokens[-1]}}
|
||||||
|
|
||||||
|
__tv_path_completion "$prefix" "$lbuf"
|
||||||
|
|
||||||
|
_enable_bracketed_paste
|
||||||
}
|
}
|
||||||
|
|
||||||
_tv_shell_history() {
|
_tv_shell_history() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user