Skip to content

Commit

Permalink
🐛 fix: use argparse to handle abbr/alias options
Browse files Browse the repository at this point in the history
  • Loading branch information
gazorby committed Oct 21, 2021
1 parent ff073a9 commit e877e28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
43 changes: 26 additions & 17 deletions conf.d/abbr_tips.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ end

set -g __abbr_tips_used 0


function __abbr_tips_install --on-event abbr_tips_install
# Regexes used to find abbreviation inside command
# Only the first matching group will be tested as an abbr
Expand All @@ -32,12 +31,15 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the

# Update abbreviations lists when adding/removing abbreviations
if test "$command[1]" = "abbr"
if string match -q -r '^--add|-a$' -- "$command[2]"
and not contains -- "$command[3]" $__ABBR_TIPS_KEYS
set -a __ABBR_TIPS_KEYS "$command[3]"
set -a __ABBR_TIPS_VALUES "$command[4..-1]"
else if string match -q -r '^--erase|-e$' -- "$command[2]"
and set -l abb (contains -i -- "$command[3]" $__ABBR_TIPS_KEYS)
# Parse args as abbr options
argparse --name 'abbr' --ignore-unknown 'a/add' 'e/erase' 'g/global' 'U/universal' -- $command

if set -q _flag_a
and not contains -- "$argv[2]" $__ABBR_TIPS_KEYS
set -a __ABBR_TIPS_KEYS "$argv[2]"
set -a __ABBR_TIPS_VALUES "$argv[3..-1]"
else if set -q _flag_e
and set -l abb (contains -i -- "$argv[2]" $__ABBR_TIPS_KEYS)
set -e __ABBR_TIPS_KEYS[$abb]
set -e __ABBR_TIPS_VALUES[$abb]
end
Expand All @@ -46,31 +48,38 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the
set -l alias_key
set -l alias_value

if string match -q '*=*' -- "$command[2]"
if test (count $command) = 2
set command_split (string split '=' -- $command[2])
# Parse args as `alias` options
argparse --name 'alias' --ignore-unknown 's/save' -- $command

if string match -q '*=*' -- "$argv[2]"
if test (count $argv) = 2
set command_split (string split '=' -- $argv[2])
set alias_key "a__$command_split[1]"
set alias_value $command_split[2]
set -a alias_value $command[3..-1]
end
else
set alias_key "a__$command[2]"
set alias_value $command[3..-1]
set alias_key "a__$argv[2]"
set alias_value $argv[3..-1]
end

if set -l abb (contains -i -- "$command[3..-1]" $__ABBR_TIPS_KEYS)
if set -l abb (contains -i -- "$argv[3..-1]" $__ABBR_TIPS_KEYS)
set __ABBR_TIPS_KEYS[$abb] $alias_key
set __ABBR_TIPS_VALUES[$abb] (string trim -c '\'"' -- $alias_value | string join ' ')
else
set -a __ABBR_TIPS_KEYS $alias_key
set -a __ABBR_TIPS_VALUES (string trim -c '\'"' -- $alias_value | string join ' ')
end
else if test "$command[1]" = "functions"
# Parse args as `functions` options
argparse --name 'functions' 'e/erase' -- $command

# Update abbreviations list when removing aliases
and string match -q -r '^--erase|-e$' -- "$command[2]"
and set -l abb (contains -i -- a__{$command[3]} $__ABBR_TIPS_KEYS)
set -e __ABBR_TIPS_KEYS[$abb]
set -e __ABBR_TIPS_VALUES[$abb]
if set -q _flag_e
and set -l abb (contains -i -- a__{$argv[2]} $__ABBR_TIPS_KEYS)
set -e __ABBR_TIPS_KEYS[$abb]
set -e __ABBR_TIPS_VALUES[$abb]
end
end

# Exit in the following cases :
Expand Down
16 changes: 13 additions & 3 deletions test/fish-abbreviation-tips.fish
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ end
setup

# Tests

# Initialization
@test "initial tip key" (
clear_test_var
abbr -a __abbr_test ps
Expand All @@ -47,6 +49,8 @@ setup
contains "ps" $__ABBR_TIPS_VALUES
) "$status" = 0


# Add abbreviation
@test "add abbreviation tip key" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test ps'
Expand All @@ -55,11 +59,12 @@ setup

@test "add abbreviation tip value" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test ps'
contains "ps" $__ABBR_TIPS_VALUES
__abbr_tips 'abbr -a __abbr_test ps -h'
contains "ps -h" $__ABBR_TIPS_VALUES
) "$status" = 0


# Remove abbreviation
@test "remove abbreviation tip key" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test ps'
Expand All @@ -74,6 +79,8 @@ setup
not contains "ps" $__ABBR_TIPS_VALUES
) "$status" = 0


# Add alias
@test "add alias tip key" (
clear_test_var
__abbr_tips 'alias __abbr_test_alias "grep -q"'
Expand All @@ -99,6 +106,7 @@ setup
) "$status" = 0


# Remove alias
@test "remove alias tip key" (
clear_test_var
__abbr_tips 'alias __abbr_test_alias "grep -q"'
Expand All @@ -113,6 +121,8 @@ setup
not contains "grep -q" $__ABBR_TIPS_VALUES
) "$status" = 0


# Test tip
@test "abbreviation tip match" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test ps'
Expand All @@ -126,4 +136,4 @@ setup
echo (__abbr_tips 'grep -q')
) = "__abbr_test_alias => grep -q"

teardown
teardown

0 comments on commit e877e28

Please sign in to comment.