121 lines
2.7 KiB
Bash
121 lines
2.7 KiB
Bash
function take() {
|
|
mkdir -p $@ && cd ${@:$#}
|
|
}
|
|
|
|
function open_command() {
|
|
local open_cmd
|
|
|
|
case "$OSTYPE" in
|
|
darwin*) open_cmd='open' ;;
|
|
cygwin*) open_cmd='cygstart' ;;
|
|
linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
|
|
open_cmd='cmd.exe /c start ""'
|
|
[[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
|
|
} ;;
|
|
msys*) open_cmd='start ""' ;;
|
|
*) echo "Platform $OSTYPE not supported"
|
|
return 1
|
|
;;
|
|
esac
|
|
|
|
${=open_cmd} "$@" &>/dev/null
|
|
}
|
|
|
|
function alias_value() {
|
|
(( $+aliases[$1] )) && echo $aliases[$1]
|
|
}
|
|
|
|
function try_alias_value() {
|
|
alias_value "$1" || echo "$1"
|
|
}
|
|
|
|
function default() {
|
|
(( $+parameters[$1] )) && return 0
|
|
typeset -g "$1"="$2" && return 3
|
|
}
|
|
|
|
function env_default() {
|
|
(( ${${(@f):-$(typeset +xg)}[(I)$1]} )) && return 0
|
|
export "$1=$2" && return 3
|
|
}
|
|
|
|
zmodload zsh/langinfo
|
|
|
|
function omz_urlencode() {
|
|
emulate -L zsh
|
|
zparseopts -D -E -a opts r m P
|
|
|
|
local in_str=$1
|
|
local url_str=""
|
|
local spaces_as_plus
|
|
if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
|
|
local str="$in_str"
|
|
|
|
local encoding=$langinfo[CODESET]
|
|
local safe_encodings
|
|
safe_encodings=(UTF-8 utf8 US-ASCII)
|
|
if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
|
|
str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
|
|
if [[ $? != 0 ]]; then
|
|
echo "Error converting string from $encoding to UTF-8" >&2
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
local i byte ord LC_ALL=C
|
|
export LC_ALL
|
|
local reserved=';/?:@&=+$,'
|
|
local mark='_.!~*''()-'
|
|
local dont_escape="[A-Za-z0-9"
|
|
if [[ -z $opts[(r)-r] ]]; then
|
|
dont_escape+=$reserved
|
|
fi
|
|
|
|
if [[ -z $opts[(r)-m] ]]; then
|
|
dont_escape+=$mark
|
|
fi
|
|
dont_escape+="]"
|
|
|
|
local url_str=""
|
|
for (( i = 1; i <= ${#str}; ++i )); do
|
|
byte="$str[i]"
|
|
if [[ "$byte" =~ "$dont_escape" ]]; then
|
|
url_str+="$byte"
|
|
else
|
|
if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
|
|
url_str+="+"
|
|
else
|
|
ord=$(( [##16] #byte ))
|
|
url_str+="%$ord"
|
|
fi
|
|
fi
|
|
done
|
|
echo -E "$url_str"
|
|
}
|
|
|
|
function omz_urldecode {
|
|
emulate -L zsh
|
|
local encoded_url=$1
|
|
|
|
local caller_encoding=$langinfo[CODESET]
|
|
local LC_ALL=C
|
|
export LC_ALL
|
|
|
|
local tmp=${encoded_url:gs/+/ /}
|
|
tmp=${tmp:gs/\\/\\\\/}
|
|
tmp=${tmp:gs/%/\\x/}
|
|
local decoded
|
|
eval "decoded=\$'$tmp'"
|
|
|
|
local safe_encodings
|
|
safe_encodings=(UTF-8 utf8 US-ASCII)
|
|
if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
|
|
decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
|
|
if [[ $? != 0 ]]; then
|
|
echo "Error converting string from UTF-8 to $caller_encoding" >&2
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
echo -E "$decoded"
|
|
}
|