Speed up default ^C action considerably

No more shelling out to external commands, no more loops, and no
conditionals past the initial check.
This commit is contained in:
Mahmoud Al-Qudsi 2020-06-29 19:39:25 -05:00
parent 7c5b19ec2c
commit 614c494859

View File

@ -1,27 +1,30 @@
function __fish_setup_cancel_text -v fish_color_cancel -v fish_color_normal
set -g __fish_cancel_text "^C"
if set -q fish_color_cancel
set __fish_cancel_text (echo -sn (set_color $fish_color_cancel) $__fish_cancel_text (set_color normal))
end
if command -sq tput
# Clear to EOL (to erase any autosuggestions)
set __fish_cancel_text (echo -sn $__fish_cancel_text (tput el; or tput ce))
end
end
__fish_setup_cancel_text
# This is meant to be bound to something like \cC. # This is meant to be bound to something like \cC.
function __fish_cancel_commandline function __fish_cancel_commandline
# Close the pager if it's open (#4298)
commandline -f cancel
set -l cmd (commandline) set -l cmd (commandline)
if test -n "$cmd" if test -n "$cmd"
commandline -C 1000000 echo -sn $__fish_cancel_text
if set -q fish_color_cancel # `commandline -L` prints the line the cursor is on (starting from the prompt), so move the cursor
echo -ns (set_color $fish_color_cancel) "^C" (set_color normal) # "to the end" then call `commandline -L` to get the total number of lines typed in at the prompt.
else commandline -C 10000000
echo -ns "^C" printf (string repeat -n (commandline -L) "\n")
end
if command -sq tput
# Clear to EOL (to erase any autosuggestion).
echo -n (tput el; or tput ce)
end
for i in (seq (commandline -L))
echo ""
end
commandline "" commandline ""
emit fish_cancel emit fish_cancel
end end
# Repaint even if we haven't cancelled anything,
# so the prompt refreshes and the terminal scrolls to it. # cancel: Close the pager if it's open (#4298)
commandline -f repaint # repaint: Repaint even if we haven't cancelled anything so the prompt refreshes
# and the terminal scrolls to it.
commandline -f cancel -f repaint
end end