1
0
mirror of https://github.com/dcarrillo/prezto.git synced 2024-12-22 18:38:00 +00:00
prezto/modules/explainshell/init.zsh

64 lines
1.4 KiB
Bash
Raw Normal View History

2014-06-18 16:29:26 +00:00
#
# Adds a explain function and explainshell widget
#
# Authors
# C Lentfort
#
2014-06-21 17:13:02 +00:00
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
}
2014-06-18 16:29:26 +00:00
function explain {
2014-06-21 17:13:02 +00:00
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})
2014-06-18 16:29:26 +00:00
# base url with first command already injected
# $ explain tar
# => http://explainshel.com/explain/tar?args=
2014-06-21 17:13:02 +00:00
url="http://explainshell.com/explain?cmd="
2014-06-18 16:29:26 +00:00
# iterates over remaining args and adds builds the rest of the url
2014-06-21 17:13:02 +00:00
for i in "$_explainshell_expanded_buffer"; do
2014-06-18 16:29:26 +00:00
url=$url"$i""+"
done
2014-06-21 17:13:02 +00:00
unset _explainshell_expanded_buffer
2014-06-18 16:29:26 +00:00
# opens url in browser
$BROWSER -t $url &> /dev/null
}
function explainshell {
2014-06-21 17:13:02 +00:00
explain ${(z)BUFFER}
2014-06-18 16:29:26 +00:00
}
zle -N explainshell
zstyle -s ':prezto:module:explainshell' key-binding 'key_binding'
if [[ -n "$key_binding" ]]; then
bindkey "$key_binding" explainshell
fi
unset key_binding