diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2015-06-14 14:08:10 -0700 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2015-06-14 14:12:29 -0700 |
commit | 4115a2f2d115d97d77f2bd81609c6756acbeab2e (patch) | |
tree | e00f50d047f3a0b6f0a410ab5573292bba326b3b /share/functions/abbr.fish | |
parent | 767742c7e7ee53c8aa7950f928d551c63d69448f (diff) |
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.
Diffstat (limited to 'share/functions/abbr.fish')
-rw-r--r-- | share/functions/abbr.fish | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/share/functions/abbr.fish b/share/functions/abbr.fish index dd67b898..87cf3184 100644 --- a/share/functions/abbr.fish +++ b/share/functions/abbr.fish @@ -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 |