mirror of
https://github.com/fish-shell/fish-shell
synced 2024-09-23 07:52:06 +00:00
Rewrite abbr.fish to not call seq
This speeds up adding new abbrs by about 50 to 60% - from 2.3s to 1s for 100 abbrs.
This commit is contained in:
parent
585bdf45c8
commit
eb0d18add4
1 changed files with 28 additions and 41 deletions
|
@ -77,7 +77,9 @@ function abbr --description "Manage abbreviations"
|
||||||
contains -- $mode_arg $fish_user_abbreviations; and return 0
|
contains -- $mode_arg $fish_user_abbreviations; and return 0
|
||||||
set -l key
|
set -l key
|
||||||
set -l value
|
set -l value
|
||||||
__fish_abbr_parse_entry $mode_arg key value
|
set -l kv (__fish_abbr_split $mode_arg)
|
||||||
|
set key $kv[1]
|
||||||
|
set value $kv[2]
|
||||||
# ensure the key contains at least one non-space character
|
# ensure the key contains at least one non-space character
|
||||||
if not string match -qr "\w" -- $key
|
if not string match -qr "\w" -- $key
|
||||||
printf ( _ "%s: abbreviation must have a non-empty key\n" ) abbr >&2
|
printf ( _ "%s: abbreviation must have a non-empty key\n" ) abbr >&2
|
||||||
|
@ -100,8 +102,7 @@ function abbr --description "Manage abbreviations"
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
case 'erase'
|
case 'erase'
|
||||||
set -l key
|
set -l key (__fish_abbr_split $mode_arg)[1]
|
||||||
__fish_abbr_parse_entry $mode_arg key
|
|
||||||
if set -l idx (__fish_abbr_get_by_key $key)
|
if set -l idx (__fish_abbr_get_by_key $key)
|
||||||
set -e fish_user_abbreviations[$idx]
|
set -e fish_user_abbreviations[$idx]
|
||||||
return 0
|
return 0
|
||||||
|
@ -112,21 +113,20 @@ function abbr --description "Manage abbreviations"
|
||||||
|
|
||||||
case 'show'
|
case 'show'
|
||||||
for i in $fish_user_abbreviations
|
for i in $fish_user_abbreviations
|
||||||
__fish_abbr_parse_entry $i key value
|
set -l kv (__fish_abbr_split $i)
|
||||||
|
set -l key $kv[1]
|
||||||
|
set -l value $kv[2]
|
||||||
|
|
||||||
# Check to see if either key or value has a leading dash
|
# Check to see if either key or value has a leading dash
|
||||||
# If so, we need to write --
|
# If so, we need to write --
|
||||||
set -l opt_double_dash ''
|
string match -q -- '-*' $key $value; and set -l opt_double_dash '--'
|
||||||
switch $key ; case '-*'; set opt_double_dash ' --'; end
|
echo abbr $opt_double_dash (string escape -- $key $value)
|
||||||
switch $value ; case '-*'; set opt_double_dash ' --'; end
|
|
||||||
echo abbr$opt_double_dash (string escape -- $key $value)
|
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
case 'list'
|
case 'list'
|
||||||
for i in $fish_user_abbreviations
|
for i in $fish_user_abbreviations
|
||||||
set -l key
|
set -l key (__fish_abbr_split $i)[1]
|
||||||
__fish_abbr_parse_entry $i key
|
|
||||||
printf "%s\n" $key
|
printf "%s\n" $key
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
|
@ -138,42 +138,29 @@ function __fish_abbr_get_by_key
|
||||||
echo "__fish_abbr_get_by_key: expected one argument, got none" >&2
|
echo "__fish_abbr_get_by_key: expected one argument, got none" >&2
|
||||||
return 2
|
return 2
|
||||||
end
|
end
|
||||||
set -l count (count $fish_user_abbreviations)
|
# Going through all entries is still quicker than calling `seq`
|
||||||
if test $count -gt 0
|
set -l keys
|
||||||
set -l key $argv[1] # This assumes the key is valid and pre-parsed
|
for kv in $fish_user_abbreviations
|
||||||
for i in (seq $count)
|
if string match -qr '^[^ ]+=' -- $kv
|
||||||
set -l key_i
|
# No need for bounds-checking because we already matched before
|
||||||
__fish_abbr_parse_entry $fish_user_abbreviations[$i] key_i
|
set keys $keys (string split "=" -m 1 -- $kv)[1]
|
||||||
if test "$key" = "$key_i"
|
else if string match -qr '^[^ ]+ .*' -- $kv
|
||||||
echo $i
|
set keys $keys (string split " " -m 1 -- $kv)[1]
|
||||||
return 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if set -l idx (contains -i -- $argv[1] $keys)
|
||||||
|
echo $idx
|
||||||
|
return 0
|
||||||
|
end
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fish_abbr_parse_entry -S -a __input __key __value
|
function __fish_abbr_split -a input
|
||||||
if test -z "$__key"
|
if string match -qr '^[^ ]+=' -- $input
|
||||||
set __key __
|
string split "=" -m 1 -- $input
|
||||||
end
|
else if string match -qr '^[^ ]+ .*' -- $input
|
||||||
if test -z "$__value"
|
string split " " -m 1 -- $input
|
||||||
set __value __
|
|
||||||
end
|
|
||||||
# A "=" _before_ any space - we only read the first possible separator
|
|
||||||
# because the key can contain neither spaces nor "="
|
|
||||||
if string match -qr '^[^ ]+=' -- $__input
|
|
||||||
# No need for bounds-checking because we already matched before
|
|
||||||
set -l KV (string split "=" -m 1 -- $__input)
|
|
||||||
set $__key $KV[1]
|
|
||||||
set $__value $KV[2]
|
|
||||||
else if string match -qr '^[^ ]+ .*' -- $__input
|
|
||||||
set -l KV (string split " " -m 1 -- $__input)
|
|
||||||
set $__key $KV[1]
|
|
||||||
set $__value $KV[2]
|
|
||||||
else
|
else
|
||||||
# This is needed for `erase` et al, where we want to allow passing a value
|
echo $input
|
||||||
set $__key $__input
|
|
||||||
end
|
end
|
||||||
return 0
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue