From e877e28835681e387e55ea2bfa5003271b036a00 Mon Sep 17 00:00:00 2001 From: gazorby Date: Thu, 21 Oct 2021 18:42:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20use=20argparse=20to=20han?= =?UTF-8?q?dle=20abbr/alias=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf.d/abbr_tips.fish | 43 +++++++++++++++++++------------- test/fish-abbreviation-tips.fish | 16 +++++++++--- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/conf.d/abbr_tips.fish b/conf.d/abbr_tips.fish index f0cfaf9..1e859c6 100644 --- a/conf.d/abbr_tips.fish +++ b/conf.d/abbr_tips.fish @@ -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 @@ -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 @@ -46,19 +48,22 @@ 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 @@ -66,11 +71,15 @@ function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the 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 : diff --git a/test/fish-abbreviation-tips.fish b/test/fish-abbreviation-tips.fish index 9b6f19c..7b3b07c 100644 --- a/test/fish-abbreviation-tips.fish +++ b/test/fish-abbreviation-tips.fish @@ -31,6 +31,8 @@ end setup # Tests + +# Initialization @test "initial tip key" ( clear_test_var abbr -a __abbr_test ps @@ -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' @@ -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' @@ -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"' @@ -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"' @@ -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' @@ -126,4 +136,4 @@ setup echo (__abbr_tips 'grep -q') ) = "__abbr_test_alias => grep -q" -teardown \ No newline at end of file +teardown