From 5441bf790c2f524afb53b707ea3e3d8ea2778b32 Mon Sep 17 00:00:00 2001 From: gazorby Date: Sat, 18 Jun 2022 00:55:47 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20trim=20simple/double=20qu?= =?UTF-8?q?otes=20from=20abbr/alias?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf.d/abbr_tips.fish | 13 ++++++--- test/fish-abbreviation-tips.fish | 46 ++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/conf.d/abbr_tips.fish b/conf.d/abbr_tips.fish index 1e859c6..848ee6d 100644 --- a/conf.d/abbr_tips.fish +++ b/conf.d/abbr_tips.fish @@ -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 @@ -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] @@ -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 diff --git a/test/fish-abbreviation-tips.fish b/test/fish-abbreviation-tips.fish index 7b3b07c..40cee84 100644 --- a/test/fish-abbreviation-tips.fish +++ b/test/fish-abbreviation-tips.fish @@ -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 @@ -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