2016-08-07 10:34:29 +02:00
|
|
|
__zplug::utils::shell::remove_deadlinks()
|
|
|
|
{
|
|
|
|
local link
|
|
|
|
|
|
|
|
for link in "$@"
|
|
|
|
do
|
|
|
|
if [[ -L $link ]] && [[ ! -e $link ]]; then
|
|
|
|
rm -f "$link"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::search_commands()
|
|
|
|
{
|
|
|
|
local -a args
|
|
|
|
local arg element cmd_name
|
|
|
|
local is_verbose=true
|
|
|
|
|
|
|
|
while (( $# > 0 ))
|
|
|
|
do
|
|
|
|
arg="$1"
|
2016-08-07 18:25:27 +02:00
|
|
|
case "$arg" in
|
2016-08-07 10:34:29 +02:00
|
|
|
--verbose)
|
|
|
|
is_verbose=true
|
|
|
|
;;
|
|
|
|
--silent)
|
|
|
|
is_verbose=false
|
|
|
|
;;
|
|
|
|
-*|--*)
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
*)
|
2016-08-07 18:25:27 +02:00
|
|
|
args+=( "$arg" )
|
2016-08-07 10:34:29 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
for arg in "${args[@]}"
|
|
|
|
do
|
|
|
|
for element in "${(s.:.)arg}"
|
|
|
|
do
|
|
|
|
# Extract the first argument sparated by a space
|
|
|
|
cmd_name="${element%% *}"
|
|
|
|
|
|
|
|
# Check if cmd_name is available
|
|
|
|
if (( $+commands[$cmd_name] )); then
|
|
|
|
if $is_verbose; then
|
|
|
|
echo "$cmd_name"
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::glob2regexp()
|
|
|
|
{
|
|
|
|
local -i i=0
|
2016-08-07 13:58:35 +02:00
|
|
|
local glob="$1" char
|
|
|
|
|
2016-08-07 10:34:29 +02:00
|
|
|
printf "^"
|
|
|
|
for ((; i < $#glob; i++))
|
|
|
|
do
|
|
|
|
char="${glob:$i:1}"
|
|
|
|
case "$char" in
|
|
|
|
\*)
|
|
|
|
printf '.*'
|
|
|
|
;;
|
|
|
|
.)
|
|
|
|
printf '\.'
|
|
|
|
;;
|
|
|
|
"{")
|
|
|
|
printf '('
|
|
|
|
;;
|
|
|
|
"}")
|
|
|
|
printf ')'
|
|
|
|
;;
|
|
|
|
,)
|
|
|
|
printf '|'
|
|
|
|
;;
|
|
|
|
"?")
|
|
|
|
printf '.'
|
|
|
|
;;
|
|
|
|
\\)
|
|
|
|
printf '\\\\'
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
printf "$char"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
printf "$\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::sudo()
|
|
|
|
{
|
|
|
|
local pw="$ZPLUG_SUDO_PASSWORD"
|
|
|
|
|
|
|
|
if [[ -z $pw ]]; then
|
2016-12-14 13:02:18 +01:00
|
|
|
__zplug::log::write::error \
|
2016-08-07 10:34:29 +02:00
|
|
|
"ZPLUG_SUDO_PASSWORD: is an invalid value\n"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
sudo -k
|
|
|
|
echo "$pw" \
|
|
|
|
| sudo -S -p '' "$argv[@]"
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::unansi()
|
|
|
|
{
|
|
|
|
perl -pe 's/\e\[?.*?[\@-~]//g'
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::cd()
|
|
|
|
{
|
|
|
|
local dir arg
|
|
|
|
local -a dirs
|
|
|
|
local is_force=false
|
|
|
|
|
|
|
|
while (( $# > 0 ))
|
|
|
|
do
|
|
|
|
arg="$1"
|
|
|
|
case "$arg" in
|
|
|
|
--force)
|
|
|
|
is_force=true
|
|
|
|
;;
|
|
|
|
-*|--*)
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
"")
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
dirs+=( "$arg" )
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
for dir in "$dirs[@]"
|
|
|
|
do
|
|
|
|
if $is_force; then
|
|
|
|
[[ -d $dir ]] || mkdir -p "$dir"
|
|
|
|
fi
|
|
|
|
|
2016-12-13 13:39:12 +01:00
|
|
|
builtin cd "$dir" &>/dev/null
|
2016-08-07 10:34:29 +02:00
|
|
|
return $status
|
|
|
|
done
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::getopts()
|
|
|
|
{
|
|
|
|
printf "%s\n" "$argv[@]" \
|
|
|
|
| awk -f "$ZPLUG_ROOT/misc/contrib/getopts.awk"
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::pipestatus()
|
|
|
|
{
|
|
|
|
local _status="${pipestatus[*]-}"
|
|
|
|
|
|
|
|
[[ ${_status//0 /} == 0 ]]
|
|
|
|
return $status
|
|
|
|
}
|
2016-09-26 10:24:23 +02:00
|
|
|
|
|
|
|
__zplug::utils::shell::expand_glob()
|
|
|
|
{
|
|
|
|
local pattern="$1"
|
|
|
|
# Modifiers to use if $pattern does not include modifiers
|
2016-10-17 02:39:36 +02:00
|
|
|
local default_modifiers="${2:-(N)}"
|
2016-09-26 10:24:23 +02:00
|
|
|
local -a matches
|
|
|
|
|
|
|
|
# Modifiers not specified (by user)
|
2016-10-05 02:07:14 +02:00
|
|
|
if [[ ! $pattern =~ '[^/]\([^)]*\)$' ]]; then
|
2016-09-26 10:24:23 +02:00
|
|
|
pattern+="$default_modifiers"
|
|
|
|
fi
|
|
|
|
|
2016-10-17 02:38:40 +02:00
|
|
|
# Try expanding ~ and *
|
|
|
|
matches=( ${~pattern} )
|
|
|
|
|
|
|
|
# Use subshell for brace expansion
|
|
|
|
if (( $#matches <= 1 )); then
|
|
|
|
matches=( $( \
|
|
|
|
zsh -c "$_ZPLUG_CONFIG_SUBSHELL; echo $pattern" \
|
2016-12-14 13:02:18 +01:00
|
|
|
2> >(__zplug::log::capture::error) \
|
2016-10-17 02:38:40 +02:00
|
|
|
) )
|
|
|
|
fi
|
2016-09-26 10:24:23 +02:00
|
|
|
|
|
|
|
print -l "${matches[@]}"
|
|
|
|
}
|
2016-10-19 15:11:39 +02:00
|
|
|
|
|
|
|
__zplug::utils::shell::eval()
|
|
|
|
{
|
|
|
|
local cmd
|
|
|
|
|
|
|
|
# Report stderr to error log
|
2016-12-14 13:02:18 +01:00
|
|
|
eval "${=cmd}" 2> >(__zplug::log::capture::error) >/dev/null
|
2016-10-19 15:11:39 +02:00
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
|
|
|
__zplug::utils::shell::json_escape()
|
|
|
|
{
|
2016-12-14 11:57:57 +01:00
|
|
|
if (( $+commands[python] )); then
|
2016-12-21 11:11:12 +01:00
|
|
|
python -c '
|
|
|
|
from __future__ import print_function
|
|
|
|
import json,sys
|
|
|
|
print(json.dumps(sys.stdin.read()))' \
|
|
|
|
2> >(__zplug::log::capture::error)
|
2016-10-19 15:11:39 +02:00
|
|
|
else
|
2016-12-14 11:57:57 +01:00
|
|
|
echo "(Not available: python requires)"
|
|
|
|
fi
|
2016-10-19 15:11:39 +02:00
|
|
|
}
|
2016-12-26 13:02:59 +01:00
|
|
|
|
|
|
|
__zplug::utils::shell::is_atty()
|
|
|
|
{
|
|
|
|
if [[ -t 0 && -t 1 ]]; then
|
|
|
|
# terminal
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
# pipeline
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|