diff --git a/lib/cli.zsh b/lib/cli.zsh index 59873368b..fe6f7667f 100644 --- a/lib/cli.zsh +++ b/lib/cli.zsh @@ -295,17 +295,23 @@ function _omz::plugin::disable { fi # Remove plugins substitution awk script + # A match consumes the whitespace around the plugin, so adjacent plugins + # can't match in the same pass: repeat until the line stops changing. local awk_subst_plugins="\ - gsub(/[ \t]+(${(j:|:)dis_plugins})[ \t]+/, \" \") # with spaces before or after - gsub(/[ \t]+(${(j:|:)dis_plugins})$/, \"\") # with spaces before and EOL - gsub(/^(${(j:|:)dis_plugins})[ \t]+/, \"\") # with BOL and spaces after + do { + prev = \$0 - gsub(/\((${(j:|:)dis_plugins})[ \t]+/, \"(\") # with parenthesis before and spaces after - gsub(/[ \t]+(${(j:|:)dis_plugins})\)/, \")\") # with spaces before or parenthesis after - gsub(/\((${(j:|:)dis_plugins})\)/, \"()\") # with only parentheses + gsub(/[ \t]+(${(j:|:)dis_plugins})[ \t]+/, \" \") # with spaces before or after + gsub(/[ \t]+(${(j:|:)dis_plugins})$/, \"\") # with spaces before and EOL + gsub(/^(${(j:|:)dis_plugins})[ \t]+/, \"\") # with BOL and spaces after - gsub(/^(${(j:|:)dis_plugins})\)/, \")\") # with BOL and closing parenthesis - gsub(/\((${(j:|:)dis_plugins})$/, \"(\") # with opening parenthesis and EOL + gsub(/\((${(j:|:)dis_plugins})[ \t]+/, \"(\") # with parenthesis before and spaces after + gsub(/[ \t]+(${(j:|:)dis_plugins})\)/, \")\") # with spaces before or parenthesis after + gsub(/\((${(j:|:)dis_plugins})\)/, \"()\") # with only parentheses + + gsub(/^(${(j:|:)dis_plugins})\)/, \")\") # with BOL and closing parenthesis + gsub(/\((${(j:|:)dis_plugins})$/, \"(\") # with opening parenthesis and EOL + } while (\$0 != prev) " # Disable plugins awk script diff --git a/lib/tests/cli.test.zsh b/lib/tests/cli.test.zsh index 9ee5cd219..0dca1e65e 100644 --- a/lib/tests/cli.test.zsh +++ b/lib/tests/cli.test.zsh @@ -7,16 +7,20 @@ run_awk() { (( ! DEBUG )) || set -xv local awk_subst_plugins="\ - gsub(/[ \t]+(${(j:|:)dis_plugins})[ \t]+/, \" \") # with spaces before or after - gsub(/[ \t]+(${(j:|:)dis_plugins})$/, \"\") # with spaces before and EOL - gsub(/^(${(j:|:)dis_plugins})[ \t]+/, \"\") # with BOL and spaces after + do { + prev = \$0 - gsub(/\((${(j:|:)dis_plugins})[ \t]+/, \"(\") # with parenthesis before and spaces after - gsub(/[ \t]+(${(j:|:)dis_plugins})\)/, \")\") # with spaces before or parenthesis after - gsub(/\((${(j:|:)dis_plugins})\)/, \"()\") # with only parentheses + gsub(/[ \t]+(${(j:|:)dis_plugins})[ \t]+/, \" \") # with spaces before or after + gsub(/[ \t]+(${(j:|:)dis_plugins})$/, \"\") # with spaces before and EOL + gsub(/^(${(j:|:)dis_plugins})[ \t]+/, \"\") # with BOL and spaces after - gsub(/^(${(j:|:)dis_plugins})\)/, \")\") # with BOL and closing parenthesis - gsub(/\((${(j:|:)dis_plugins})$/, \"(\") # with opening parenthesis and EOL + gsub(/\((${(j:|:)dis_plugins})[ \t]+/, \"(\") # with parenthesis before and spaces after + gsub(/[ \t]+(${(j:|:)dis_plugins})\)/, \")\") # with spaces before or parenthesis after + gsub(/\((${(j:|:)dis_plugins})\)/, \"()\") # with only parentheses + + gsub(/^(${(j:|:)dis_plugins})\)/, \")\") # with BOL and closing parenthesis + gsub(/\((${(j:|:)dis_plugins})$/, \"(\") # with opening parenthesis and EOL + } while (\$0 != prev) " # Disable plugins awk script local awk_script=" @@ -167,3 +171,34 @@ z)" \ "plugins=( git )" + +run_awk_and_test \ + "it should delete two adjacent plugins among others in oneline format" \ + "docker kubectl" \ + "plugins=(git docker kubectl npm)" \ + "plugins=(git npm)" + +run_awk_and_test \ + "it should delete three adjacent plugins among others in oneline format" \ + "docker kubectl aws" \ + "plugins=(git docker kubectl aws npm)" \ + "plugins=(git npm)" + +run_awk_and_test \ + "it should delete every plugin of a long oneline list" \ + "git docker kubectl aws npm z" \ + "plugins=(git docker kubectl aws npm z)" \ + "plugins=()" + +run_awk_and_test \ + "it should delete adjacent plugins sharing a line in multiline format" \ + "docker kubectl aws" \ +"plugins=( + git + docker kubectl aws + npm +)" \ +"plugins=( + git + npm +)"