summaryrefslogtreecommitdiff
path: root/home/.oh-my-zsh/plugins/alias-finder
diff options
context:
space:
mode:
Diffstat (limited to 'home/.oh-my-zsh/plugins/alias-finder')
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/.zunit.yml9
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/README.md68
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/alias-finder.plugin.zsh66
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/tests/_output/.gitkeep0
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/tests/_support/.gitkeep0
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/tests/_support/bootstrap2
-rw-r--r--home/.oh-my-zsh/plugins/alias-finder/tests/test_run.sh107
7 files changed, 252 insertions, 0 deletions
diff --git a/home/.oh-my-zsh/plugins/alias-finder/.zunit.yml b/home/.oh-my-zsh/plugins/alias-finder/.zunit.yml
new file mode 100644
index 0000000..ae65f8e
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/.zunit.yml
@@ -0,0 +1,9 @@
+tap: false
+directories:
+ tests: tests
+ output: tests/_output
+ support: tests/_support
+time_limit: 0
+fail_fast: false
+allow_risky: false
+verbose: true
diff --git a/home/.oh-my-zsh/plugins/alias-finder/README.md b/home/.oh-my-zsh/plugins/alias-finder/README.md
new file mode 100644
index 0000000..a9bbd08
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/README.md
@@ -0,0 +1,68 @@
+# alias-finder plugin
+
+This plugin searches the defined aliases and outputs any that match the command inputted. This makes learning new aliases easier.
+
+## Setup
+
+To use it, add `alias-finder` to the `plugins` array of your zshrc file:
+```
+plugins=(... alias-finder)
+```
+
+To enable it for every single command, set zstyle in your `~/.zshrc`.
+
+```zsh
+# ~/.zshrc
+
+zstyle ':omz:plugins:alias-finder' autoload yes # disabled by default
+zstyle ':omz:plugins:alias-finder' longer yes # disabled by default
+zstyle ':omz:plugins:alias-finder' exact yes # disabled by default
+zstyle ':omz:plugins:alias-finder' cheaper yes # disabled by default
+```
+
+As you can see, options are also available with zstyle.
+
+## Usage
+
+When you execute a command alias finder will look at your defined aliases and suggest shorter aliases you could have used, for example:
+
+Running the un-aliased `git status` command:
+```sh
+╭─tim@fox ~/repo/gitopolis ‹main›
+╰─$ git status
+
+gst='git status' # <=== shorter suggestion from alias-finder
+
+On branch main
+Your branch is up-to-date with 'origin/main'.
+nothing to commit, working tree clean
+```
+
+Running a shorter `git st` alias from `.gitconfig` that it suggested :
+```sh
+╭─tim@fox ~/repo/gitopolis ‹main›
+╰─$ git st
+gs='git st' # <=== shorter suggestion from alias-finder
+## main...origin/main
+```
+
+Running the shortest `gs` shell alias that it found:
+```sh
+╭─tim@fox ~/repo/gitopolis ‹main›
+╰─$ gs
+ # <=== no suggestions alias-finder because this is the shortest
+## main...origin/main
+```
+
+![image](https://github.com/ohmyzsh/ohmyzsh/assets/19378/39642750-fb10-4f1a-b7f9-f36789eeb01b)
+
+
+### Options
+
+> In order to clarify, let's say `alias a=abc` has source 'abc' and destination 'a'.
+
+- Use `--longer` or `-l` to include aliases where the source is longer than the input (in other words, the source could contain the whole input).
+- Use `--exact` or `-e` to avoid aliases where the source is shorter than the input (in other words, the source must be the same with the input).
+- Use `--cheaper` or `-c` to avoid aliases where the destination is longer than the input (in other words, the destination must be the shorter than the input).
+
+
diff --git a/home/.oh-my-zsh/plugins/alias-finder/alias-finder.plugin.zsh b/home/.oh-my-zsh/plugins/alias-finder/alias-finder.plugin.zsh
new file mode 100644
index 0000000..2351a43
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/alias-finder.plugin.zsh
@@ -0,0 +1,66 @@
+alias-finder() {
+ local cmd=" " exact="" longer="" cheaper="" wordEnd="'{0,1}$" finder="" filter=""
+
+ # build command and options
+ for c in "$@"; do
+ case $c in
+ # TODO: Remove backward compatibility (other than zstyle form)
+ # set options if exist
+ -e|--exact) exact=true;;
+ -l|--longer) longer=true;;
+ -c|--cheaper) cheaper=true;;
+ # concatenate cmd
+ *) cmd="$cmd$c " ;;
+ esac
+ done
+
+ zstyle -t ':omz:plugins:alias-finder' longer && longer=true
+ zstyle -t ':omz:plugins:alias-finder' exact && exact=true
+ zstyle -t ':omz:plugins:alias-finder' cheaper && cheaper=true
+
+ # format cmd for grep
+ ## - replace newlines with spaces
+ ## - trim both ends
+ ## - replace multiple spaces with one space
+ ## - add escaping character to special characters
+ cmd=$(echo -n "$cmd" | tr '\n' ' ' | xargs | tr -s '[:space:]' | sed 's/[].\|$(){}?+*^[]/\\&/g')
+
+ if [[ $longer == true ]]; then
+ wordEnd="" # remove wordEnd to find longer aliases
+ fi
+
+ # find with alias and grep, removing last word each time until no more words
+ while [[ $cmd != "" ]]; do
+ finder="'{0,1}$cmd$wordEnd"
+
+ # make filter to find only shorter results than current cmd
+ if [[ $cheaper == true ]]; then
+ cmdLen=$(echo -n "$cmd" | wc -c)
+ if [[ $cmdLen -le 1 ]]; then
+ return
+ fi
+
+ filter="^'?.{1,$((cmdLen - 1))}'?=" # some aliases is surrounded by single quotes
+ fi
+
+ alias | grep -E "$filter" | grep -E "=$finder"
+
+ if [[ $exact == true ]]; then
+ break # because exact case is only one
+ elif [[ $longer == true ]]; then
+ break # because above grep command already found every longer aliases during first cycle
+ fi
+
+ cmd=$(sed -E 's/ {0,}[^ ]*$//' <<< "$cmd") # remove last word
+ done
+}
+
+preexec_alias-finder() {
+ # TODO: Remove backward compatibility (other than zstyle form)
+ zstyle -t ':omz:plugins:alias-finder' autoload && alias-finder $1 || if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
+ alias-finder $1
+ fi
+}
+
+autoload -U add-zsh-hook
+add-zsh-hook preexec preexec_alias-finder
diff --git a/home/.oh-my-zsh/plugins/alias-finder/tests/_output/.gitkeep b/home/.oh-my-zsh/plugins/alias-finder/tests/_output/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/tests/_output/.gitkeep
diff --git a/home/.oh-my-zsh/plugins/alias-finder/tests/_support/.gitkeep b/home/.oh-my-zsh/plugins/alias-finder/tests/_support/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/tests/_support/.gitkeep
diff --git a/home/.oh-my-zsh/plugins/alias-finder/tests/_support/bootstrap b/home/.oh-my-zsh/plugins/alias-finder/tests/_support/bootstrap
new file mode 100644
index 0000000..0107661
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/tests/_support/bootstrap
@@ -0,0 +1,2 @@
+#!/usr/bin/env zsh
+# Write your bootstrap code here
diff --git a/home/.oh-my-zsh/plugins/alias-finder/tests/test_run.sh b/home/.oh-my-zsh/plugins/alias-finder/tests/test_run.sh
new file mode 100644
index 0000000..6b7abeb
--- /dev/null
+++ b/home/.oh-my-zsh/plugins/alias-finder/tests/test_run.sh
@@ -0,0 +1,107 @@
+#!/usr/bin/env zunit
+
+@setup {
+ load ../alias-finder.plugin.zsh
+
+ set_git_aliases() {
+ unalias -a # all
+ alias g="git"
+ alias gc="git commit"
+ alias gcv="git commit -v"
+ alias gcvs="git commit -v -S"
+ }
+}
+
+@test 'find aliases that contain input' {
+ set_git_aliases
+
+ run alias-finder "git"
+
+ assert "${#lines[@]}" equals 1
+ assert "${lines[1]}" same_as "g=git"
+}
+
+@test 'find aliases that contain input with whitespaces at ends' {
+ set_git_aliases
+
+ run alias-finder " git "
+
+ assert "${#lines[@]}" equals 1
+ assert "${lines[1]}" same_as "g=git"
+}
+
+@test 'find aliases that contain multiple words' {
+ set_git_aliases
+
+ run alias-finder "git commit -v"
+
+ assert "${#lines[@]}" equals 3
+ assert "${lines[1]}" same_as "gcv='git commit -v'"
+ assert "${lines[2]}" same_as "gc='git commit'"
+ assert "${lines[3]}" same_as "g=git"
+}
+
+@test 'find alias that is the same with input when --exact option is set' {
+ set_git_aliases
+
+ run alias-finder -e "git"
+
+ assert "${#lines[@]}" equals 1
+ assert "${lines[1]}" same_as "g=git"
+}
+
+@test 'find alias that is the same with multiple words input when --exact option is set' {
+ set_git_aliases
+
+ run alias-finder -e "git commit -v"
+
+ assert "${#lines[@]}" equals 1
+ assert "${lines[1]}" same_as "gcv='git commit -v'"
+}
+
+@test 'find alias that is the same with or longer than input when --longer option is set' {
+ set_git_aliases
+
+ run alias-finder -l "git"
+
+ assert "${#lines[@]}" equals 4
+ assert "${lines[1]}" same_as "g=git"
+ assert "${lines[2]}" same_as "gc='git commit'"
+ assert "${lines[3]}" same_as "gcv='git commit -v'"
+ assert "${lines[4]}" same_as "gcvs='git commit -v -S'"
+}
+
+@test 'find alias that is the same with or longer than multiple words input when --longer option is set' {
+ set_git_aliases
+
+ run alias-finder -l "git commit -v"
+
+ assert "${#lines[@]}" equals 2
+ assert "${lines[1]}" same_as "gcv='git commit -v'"
+ assert "${lines[2]}" same_as "gcvs='git commit -v -S'"
+}
+
+@test 'find aliases including expensive (longer) than input' {
+ set_git_aliases
+ alias expensiveCommands="git commit"
+
+ run alias-finder "git commit -v"
+
+ assert "${#lines[@]}" equals 4
+ assert "${lines[1]}" same_as "gcv='git commit -v'"
+ assert "${lines[2]}" same_as "expensiveCommands='git commit'"
+ assert "${lines[3]}" same_as "gc='git commit'"
+ assert "${lines[4]}" same_as "g=git"
+}
+
+@test 'find aliases excluding expensive (longer) than input when --cheap option is set' {
+ set_git_aliases
+ alias expensiveCommands="git commit"
+
+ run alias-finder -c "git commit -v"
+
+ assert "${#lines[@]}" equals 3
+ assert "${lines[1]}" same_as "gcv='git commit -v'"
+ assert "${lines[2]}" same_as "gc='git commit'"
+ assert "${lines[3]}" same_as "g=git"
+}