Added alias expansion and fixed README

This commit is contained in:
Christian Lentfort 2014-06-21 19:13:02 +02:00 committed by Sorin Ionescu
parent 0a3d29ce5e
commit 9ab6ab0d66
2 changed files with 37 additions and 11 deletions

View File

@ -1,10 +1,11 @@
#Explainshell #Explainshell
Adds a keybinding to open (explainshell.com)[http://www.explainshell.com] with the content of the command line. Adds a keybinding and a function to open
[explainshell.com](http://www.explainshell.com) with the content of the command
line.
##Settings ##Settings
###Key-Binding ###Key-Binding
The keybinding to open (explainshell.com)[http://www.explainshell.com] The keybinding to open [explainshell.com](http://www.explainshell.com)
`zstyle ':prezto:module:explainshell' key-binding '^K'` `zstyle ':prezto:module:explainshell' key-binding '^K'`

View File

@ -5,27 +5,52 @@
# C Lentfort # C Lentfort
# #
function _expand_alias_recursive {
local _alias
for word in "$@"; do
# Check if word is aliased
_alias=$aliases[$word]
if [ -n "$_alias" ]; then
# Check if found alias and given command are identical
if [[ "$_alias" != "${(j: :)@}" ]]; then
_expand_alias_recursive "${(z)_alias}"
else
_explainshell_expanded_buffer+=$1
fi
else
_explainshell_expanded_buffer+=$word
fi
done
}
function explain { function explain {
local url
# We don't explain empty buffers
if (( $# == 0 )); then
return 1;
fi
# Replace aliases with their actual expansions
_explainshell_expanded_buffer=()
_expand_alias_recursive $@
_explainshell_expanded_buffer=(${(u)_explainshell_expanded_buffer})
# base url with first command already injected # base url with first command already injected
# $ explain tar # $ explain tar
# => http://explainshel.com/explain/tar?args= # => http://explainshel.com/explain/tar?args=
url="http://explainshell.com/explain/$1?args=" url="http://explainshell.com/explain?cmd="
# removes $1 (tar) from arguments ($@)
shift;
# iterates over remaining args and adds builds the rest of the url # iterates over remaining args and adds builds the rest of the url
for i in "$@"; do for i in "$_explainshell_expanded_buffer"; do
url=$url"$i""+" url=$url"$i""+"
done done
unset _explainshell_expanded_buffer
# opens url in browser # opens url in browser
$BROWSER -t $url &> /dev/null $BROWSER -t $url &> /dev/null
} }
function explainshell { function explainshell {
explain $BUFFER explain ${(z)BUFFER}
} }
zle -N explainshell zle -N explainshell