Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: trim simple/double quotes from abbr/alias #24

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions conf.d/abbr_tips.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ end

set -g __abbr_tips_used 0

# Trim simple/double quotes from args
function trim_value
echo "$argv" | string trim --left --right --chars '"\'' | string join ' '
end

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 Down Expand Up @@ -37,7 +42,7 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the
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]"
set -a __ABBR_TIPS_VALUES (trim_value "$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]
Expand All @@ -63,12 +68,14 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the
set alias_value $argv[3..-1]
end

set alias_value (trim_value "$alias_value")

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 ' ')
set __ABBR_TIPS_VALUES[$abb] $alias_value
else
set -a __ABBR_TIPS_KEYS $alias_key
set -a __ABBR_TIPS_VALUES (string trim -c '\'"' -- $alias_value | string join ' ')
set -a __ABBR_TIPS_VALUES $alias_value
end
else if test "$command[1]" = "functions"
# Parse args as `functions` options
Expand Down
46 changes: 41 additions & 5 deletions test/fish-abbreviation-tips.fish
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,38 @@ setup
# Add abbreviation
@test "add abbreviation tip key" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test ps'
__abbr_tips 'abbr -a __abbr_test grep -q'
contains "__abbr_test" $__ABBR_TIPS_KEYS
) "$status" = 0

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

@test "add abbreviation tip key with simple quotes" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test \'grep -q\''
contains "__abbr_test" $__ABBR_TIPS_KEYS
) "$status" = 0

@test "add abbreviation tip value with simple quotes" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test \'grep -q\''
contains "grep -q" $__ABBR_TIPS_VALUES
) "$status" = 0

@test "add abbreviation tip key with double quotes" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test "grep -q"'
contains "__abbr_test" $__ABBR_TIPS_KEYS
) "$status" = 0

@test "add abbreviation tip value with double quotes" (
clear_test_var
__abbr_tips 'abbr -a __abbr_test "grep -q"'
contains "grep -q" $__ABBR_TIPS_VALUES
) "$status" = 0


Expand All @@ -81,13 +105,25 @@ setup


# Add alias
@test "add alias tip key" (
@test "add alias tip key simple quotes" (
clear_test_var
__abbr_tips 'alias __abbr_test_alias \'grep -q\''
contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS
) "$status" = 0

@test "add alias tip value simple quotes" (
clear_test_var
__abbr_tips 'alias __abbr_test_alias \'grep -q\''
contains "grep -q" $__ABBR_TIPS_VALUES
) "$status" = 0

@test "add alias tip key double quotes" (
clear_test_var
__abbr_tips 'alias __abbr_test_alias "grep -q"'
contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS
) "$status" = 0

@test "add alias tip value" (
@test "add alias tip value double quotes" (
clear_test_var
__abbr_tips 'alias __abbr_test_alias "grep -q"'
contains "grep -q" $__ABBR_TIPS_VALUES
Expand Down