Tweak and add tests for abbr

1. When run with no arguments, make abbr do the equivalent
   of `abbr --show`
2. Enable "implicit add", e.g. `abbr gco git checkout`
3. Teach `abbr --show` to not use quotes for simple cases
4. Teach abbr to output -- when the abbreviation has
   leading dashes

Add some basic tests to abbr too.
This commit is contained in:
ridiculousfish 2015-06-14 14:08:10 -07:00
parent 064ad7b981
commit 781bbe217a
2 changed files with 32 additions and 8 deletions

View File

@ -1,9 +1,4 @@
function abbr --description "Manage abbreviations"
if not set -q argv[1]
__fish_print_help abbr
return 1
end
# parse arguments
set -l mode
set -l mode_flag # the flag that was specified, for better errors
@ -56,6 +51,18 @@ function abbr --description "Manage abbreviations"
printf ( _ "%s: option requires an argument -- %s\n" ) abbr $mode_flag >&2
return 1
end
# If run with no options, treat it like --add if we have an argument, or
# --show if we do not have an argument
if test -z "$mode"
if set -q argv[1]
set mode 'add'
set mode_arg "$argv"
set -e argv
else
set mode 'show'
end
end
# none of our modes want any excess arguments
if set -q argv[1]
@ -106,7 +113,14 @@ function abbr --description "Manage abbreviations"
for i in $fish_user_abbreviations
# Disable newline splitting
set -lx IFS ''
echo abbr -a \'(__fish_abbr_escape $i)\'
__fish_abbr_parse_entry $i key value
# Check to see if either key or value has a leading dash
# If so, we need to write --
set -l opt_double_dash ''
switch $key ; 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")
end
return 0
@ -121,7 +135,17 @@ function abbr --description "Manage abbreviations"
end
function __fish_abbr_escape
echo $argv | sed -e s,\\\\,\\\\\\\\,g -e s,\',\\\\\',g
# 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

View File

@ -709,7 +709,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
return True
def do_save_abbreviation(self, abbreviation):
out, err = run_fish_cmd('abbr --add \'%s %s\'' % (abbreviation['word'], abbreviation['phrase']))
out, err = run_fish_cmd('abbr --add -- \'%s %s\'' % (abbreviation['word'], abbreviation['phrase']))
if err:
return err
else: