mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-06 19:45:23 +00:00
feat(shell): support completion in the middle of a prompt (#450)
I think it's better to insert our output at the cursor so this can be invoked in the middle of a prompt. Previously we would assume the cursor is at the end of the prompt and invocation in the middle of one would discard everything after the cursor. --------- Co-authored-by: Alexandre Pasmantier <47638216+alexpasmantier@users.noreply.github.com>
This commit is contained in:
parent
e23c307649
commit
de6200e45d
@ -1,14 +1,18 @@
|
|||||||
function tv_smart_autocomplete() {
|
function tv_smart_autocomplete() {
|
||||||
|
# prefix (lhs of cursor)
|
||||||
local current_prompt="${READLINE_LINE:0:$READLINE_POINT}"
|
local current_prompt="${READLINE_LINE:0:$READLINE_POINT}"
|
||||||
|
|
||||||
local output=$(tv --autocomplete-prompt "$current_prompt")
|
local output=$(tv --autocomplete-prompt "$current_prompt" | tr '\n' ' ')
|
||||||
|
|
||||||
if [[ -n $output ]]; then
|
if [[ -n $output ]]; then
|
||||||
|
# suffix (rhs of cursor)
|
||||||
|
local rhs="${READLINE_LINE:$READLINE_POINT}"
|
||||||
# add a space if the prompt does not end with one
|
# add a space if the prompt does not end with one
|
||||||
[[ "${current_prompt}" != *" " ]] && current_prompt="${current_prompt} "
|
[[ "${current_prompt}" != *" " ]] && current_prompt="${current_prompt} "
|
||||||
|
|
||||||
READLINE_LINE=$current_prompt$output
|
local lhs=$current_prompt$output
|
||||||
READLINE_POINT=${#READLINE_LINE}
|
READLINE_LINE=$lhs$rhs
|
||||||
|
READLINE_POINT=${#lhs}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
function tv_smart_autocomplete
|
function tv_smart_autocomplete
|
||||||
|
# prefix (lhs of cursor)
|
||||||
set -l current_prompt (commandline -cp)
|
set -l current_prompt (commandline -cp)
|
||||||
|
|
||||||
set -l output (tv --autocomplete-prompt "$current_prompt")
|
set -l output (tv --autocomplete-prompt "$current_prompt")
|
||||||
|
|
||||||
if test -n "$output"
|
if test -n "$output"
|
||||||
# add a space if the prompt does not end with one (unless the prompt is an implicit cd, e.g. '\.')
|
# add a space if the prompt does not end with one (unless the prompt is an implicit cd, e.g. '\.')
|
||||||
string match -q -r '.*( |./)$' -- "$current_prompt" || set current_prompt "$current_prompt "
|
string match -q -r '.*( |./)$' -- "$current_prompt" || set output " $output"
|
||||||
commandline -r "$current_prompt$output"
|
commandline -i "$output"
|
||||||
commandline -f repaint
|
commandline -f repaint
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,21 +2,24 @@ _tv_smart_autocomplete() {
|
|||||||
emulate -L zsh
|
emulate -L zsh
|
||||||
zle -I
|
zle -I
|
||||||
|
|
||||||
|
# prefix (lhs of cursor)
|
||||||
local current_prompt
|
local current_prompt
|
||||||
current_prompt=$LBUFFER
|
current_prompt=$LBUFFER
|
||||||
|
|
||||||
local output
|
local output
|
||||||
|
output=$(tv --autocomplete-prompt "$current_prompt" $* | tr '\n' ' ')
|
||||||
output=$(tv --autocomplete-prompt "$current_prompt" $*)
|
|
||||||
|
|
||||||
|
|
||||||
if [[ -n $output ]]; then
|
if [[ -n $output ]]; then
|
||||||
zle reset-prompt
|
# suffix (rhs of cursor)
|
||||||
RBUFFER=""
|
local rhs=$RBUFFER
|
||||||
# add a space if the prompt does not end with one
|
# add a space if the prompt does not end with one
|
||||||
[[ "${current_prompt}" != *" " ]] && current_prompt="${current_prompt} "
|
[[ "${current_prompt}" != *" " ]] && current_prompt="${current_prompt} "
|
||||||
LBUFFER=$current_prompt$output
|
|
||||||
|
|
||||||
|
LBUFFER=$current_prompt$output
|
||||||
|
CURSOR=${#LBUFFER}
|
||||||
|
RBUFFER=$rhs
|
||||||
|
|
||||||
|
zle reset-prompt
|
||||||
# uncomment this to automatically accept the line
|
# uncomment this to automatically accept the line
|
||||||
# (i.e. run the command without having to press enter twice)
|
# (i.e. run the command without having to press enter twice)
|
||||||
# zle accept-line
|
# zle accept-line
|
||||||
@ -52,4 +55,3 @@ zle -N tv-shell-history _tv_shell_history
|
|||||||
|
|
||||||
bindkey '{tv_smart_autocomplete_keybinding}' tv-smart-autocomplete
|
bindkey '{tv_smart_autocomplete_keybinding}' tv-smart-autocomplete
|
||||||
bindkey '{tv_shell_history_keybinding}' tv-shell-history
|
bindkey '{tv_shell_history_keybinding}' tv-shell-history
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user