mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
Optimize and modernize abbr
- Replace __fish_abbr_escape with `string escape` - Don't double-parse the key - Replace IFS magic with string Together, this seems to speed it up by a factor of about 2.
This commit is contained in:
parent
7a8cbcda06
commit
ac545940de
1 changed files with 15 additions and 36 deletions
|
@ -76,13 +76,11 @@ function abbr --description "Manage abbreviations"
|
||||||
set -l value
|
set -l value
|
||||||
__fish_abbr_parse_entry $mode_arg key value
|
__fish_abbr_parse_entry $mode_arg key value
|
||||||
# ensure the key contains at least one non-space character
|
# ensure the key contains at least one non-space character
|
||||||
set -l IFS \n\ \t
|
if not string match -qr "\w" -- $key
|
||||||
printf '%s' $key | read -lz key_ __
|
|
||||||
if test -z "$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
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
if test -z "$value"
|
if not string match -qr "\w" -- $value
|
||||||
printf ( _ "%s: abbreviation must have a value\n" ) abbr >&2
|
printf ( _ "%s: abbreviation must have a value\n" ) abbr >&2
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
@ -111,8 +109,6 @@ function abbr --description "Manage abbreviations"
|
||||||
|
|
||||||
case 'show'
|
case 'show'
|
||||||
for i in $fish_user_abbreviations
|
for i in $fish_user_abbreviations
|
||||||
# Disable newline splitting
|
|
||||||
set -lx IFS ''
|
|
||||||
__fish_abbr_parse_entry $i key value
|
__fish_abbr_parse_entry $i key value
|
||||||
|
|
||||||
# Check to see if either key or value has a leading dash
|
# Check to see if either key or value has a leading dash
|
||||||
|
@ -120,7 +116,7 @@ function abbr --description "Manage abbreviations"
|
||||||
set -l opt_double_dash ''
|
set -l opt_double_dash ''
|
||||||
switch $key ; case '-*'; set opt_double_dash ' --'; end
|
switch $key ; case '-*'; set opt_double_dash ' --'; end
|
||||||
switch $value ; case '-*'; set opt_double_dash ' --'; end
|
switch $value ; case '-*'; set opt_double_dash ' --'; end
|
||||||
echo abbr$opt_double_dash (__fish_abbr_escape "$key") (__fish_abbr_escape "$value")
|
echo abbr$opt_double_dash (string escape -- $key $value)
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -134,20 +130,6 @@ function abbr --description "Manage abbreviations"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function __fish_abbr_escape
|
|
||||||
# Prettify the common case: if everything is alphanumeric,
|
|
||||||
# we do not need escapes.
|
|
||||||
# Do this by deleting alnum characters, and check if there's anything left.
|
|
||||||
# Note we need to preserve spaces, so spaces are not considered alnum
|
|
||||||
if test -z (echo -n "$argv" | tr -d '[:alnum:]_')
|
|
||||||
echo $argv
|
|
||||||
else
|
|
||||||
# Escape via single quotes
|
|
||||||
# printf is nice for stripping the newline that sed outputs
|
|
||||||
printf "'%s'" (echo -n $argv | sed -e s,\\\\,\\\\\\\\,g -e s,\',\\\\\',g)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function __fish_abbr_get_by_key
|
function __fish_abbr_get_by_key
|
||||||
if not set -q argv[1]
|
if not set -q argv[1]
|
||||||
echo "__fish_abbr_get_by_key: expected one argument, got none" >&2
|
echo "__fish_abbr_get_by_key: expected one argument, got none" >&2
|
||||||
|
@ -155,9 +137,7 @@ function __fish_abbr_get_by_key
|
||||||
end
|
end
|
||||||
set -l count (count $fish_user_abbreviations)
|
set -l count (count $fish_user_abbreviations)
|
||||||
if test $count -gt 0
|
if test $count -gt 0
|
||||||
set -l key
|
set -l key $argv[1] # This assumes the key is valid and pre-parsed
|
||||||
__fish_abbr_parse_entry $argv[1] key
|
|
||||||
set -l IFS \n # ensure newline splitting is enabled
|
|
||||||
for i in (seq $count)
|
for i in (seq $count)
|
||||||
set -l key_i
|
set -l key_i
|
||||||
__fish_abbr_parse_entry $fish_user_abbreviations[$i] key_i
|
__fish_abbr_parse_entry $fish_user_abbreviations[$i] key_i
|
||||||
|
@ -177,19 +157,18 @@ function __fish_abbr_parse_entry -S -a __input __key __value
|
||||||
if test -z "$__value"
|
if test -z "$__value"
|
||||||
set __value __
|
set __value __
|
||||||
end
|
end
|
||||||
set -l IFS '= '
|
|
||||||
switch $__input
|
switch $__input
|
||||||
case '=*'
|
case "*=*"
|
||||||
# read will skip any leading ='s, but we don't want that
|
# No need for bounds-checking because we already matched before
|
||||||
set __input " $__input"
|
set -l KV (string split "=" -m 1 -- $__input)
|
||||||
set __key _
|
set $__key $KV[1]
|
||||||
set IFS '='
|
set $__value $KV[2]
|
||||||
case ' =*'
|
case "* *"
|
||||||
set __key _
|
set -l KV (string split " " -m 1 -- $__input)
|
||||||
set IFS '='
|
set $__key $KV[1]
|
||||||
|
set $__value $KV[2]
|
||||||
|
case "*"
|
||||||
|
set $__key $__input
|
||||||
end
|
end
|
||||||
# use read -z to avoid splitting on newlines
|
|
||||||
# I think we can safely assume there will be no NULs in the input
|
|
||||||
printf "%s" $__input | read -z $__key $__value
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue