2009-08-31 22:00:15 +00:00
|
|
|
function zsh_stats() {
|
2020-09-26 10:17:12 +00:00
|
|
|
fc -l 1 \
|
|
|
|
| awk '{ CMD[$2]++; count++; } END { for (a in CMD) print CMD[a] " " CMD[a]*100/count "% " a }' \
|
|
|
|
| grep -v "./" | sort -nr | head -n20 | column -c3 -s " " -t | nl
|
2009-08-31 22:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function uninstall_oh_my_zsh() {
|
2020-02-24 19:25:27 +00:00
|
|
|
env ZSH="$ZSH" sh "$ZSH/tools/uninstall.sh"
|
2009-08-31 22:09:34 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 22:48:09 +00:00
|
|
|
function upgrade_oh_my_zsh() {
|
2020-08-17 20:11:02 +00:00
|
|
|
if (( $+functions[_omz::update] )); then
|
|
|
|
echo >&2 "${fg[yellow]}Note: \`$0\` is deprecated. Use \`omz update\` instead.$reset_color"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run update script
|
2020-12-07 18:53:07 +00:00
|
|
|
env ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive
|
2020-11-02 10:17:41 +00:00
|
|
|
local ret=$?
|
2020-08-17 20:11:02 +00:00
|
|
|
# Update last updated file
|
|
|
|
zmodload zsh/datetime
|
|
|
|
echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
|
|
|
|
# Remove update lock if it exists
|
|
|
|
command rm -rf "$ZSH/log/update.lock"
|
2020-11-02 10:17:41 +00:00
|
|
|
# Restart the zsh session
|
|
|
|
if [[ $ret -eq 0 ]]; then
|
|
|
|
# Check whether to run a login shell
|
|
|
|
[[ "$ZSH_ARGZERO" = -* ]] && exec -l "${ZSH_ARGZERO#-}" || exec "$ZSH_ARGZERO"
|
|
|
|
fi
|
2009-08-31 22:48:09 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 13:03:56 +00:00
|
|
|
function take() {
|
2018-07-02 15:05:24 +00:00
|
|
|
mkdir -p $@ && cd ${@:$#}
|
2009-08-31 13:03:56 +00:00
|
|
|
}
|
2010-12-24 22:20:57 +00:00
|
|
|
|
2015-08-04 22:27:51 +00:00
|
|
|
function open_command() {
|
|
|
|
local open_cmd
|
|
|
|
|
|
|
|
# define the open command
|
|
|
|
case "$OSTYPE" in
|
2015-11-30 20:18:27 +00:00
|
|
|
darwin*) open_cmd='open' ;;
|
|
|
|
cygwin*) open_cmd='cygstart' ;;
|
2019-09-19 14:20:02 +00:00
|
|
|
linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
|
2018-06-29 18:20:56 +00:00
|
|
|
open_cmd='cmd.exe /c start ""'
|
|
|
|
[[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
|
|
|
|
} ;;
|
2015-11-30 20:18:27 +00:00
|
|
|
msys*) open_cmd='start ""' ;;
|
2015-08-04 22:27:51 +00:00
|
|
|
*) echo "Platform $OSTYPE not supported"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2019-09-19 14:20:02 +00:00
|
|
|
${=open_cmd} "$@" &>/dev/null
|
2015-08-04 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 08:39:05 +00:00
|
|
|
#
|
|
|
|
# Get the value of an alias.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# 1. alias - The alias to get its value from
|
|
|
|
# STDOUT:
|
|
|
|
# The value of alias $1 (if it has one).
|
|
|
|
# Return value:
|
|
|
|
# 0 if the alias was found,
|
|
|
|
# 1 if it does not exist
|
|
|
|
#
|
|
|
|
function alias_value() {
|
2018-08-09 17:49:02 +00:00
|
|
|
(( $+aliases[$1] )) && echo $aliases[$1]
|
2012-05-05 08:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Try to get the value of an alias,
|
|
|
|
# otherwise return the input.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# 1. alias - The alias to get its value from
|
|
|
|
# STDOUT:
|
|
|
|
# The value of alias $1, or $1 if there is no alias $1.
|
|
|
|
# Return value:
|
|
|
|
# Always 0
|
|
|
|
#
|
|
|
|
function try_alias_value() {
|
|
|
|
alias_value "$1" || echo "$1"
|
2012-05-05 08:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set variable "$1" to default value "$2" if "$1" is not yet defined.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# 1. name - The variable to set
|
2015-10-20 20:01:14 +00:00
|
|
|
# 2. val - The default value
|
2012-05-05 08:39:31 +00:00
|
|
|
# Return value:
|
|
|
|
# 0 if the variable exists, 3 if it was set
|
|
|
|
#
|
|
|
|
function default() {
|
2019-02-25 22:20:47 +00:00
|
|
|
(( $+parameters[$1] )) && return 0
|
2012-05-05 08:39:31 +00:00
|
|
|
typeset -g "$1"="$2" && return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2016-12-30 12:34:16 +00:00
|
|
|
# Set environment variable "$1" to default value "$2" if "$1" is not yet defined.
|
2012-05-05 08:39:31 +00:00
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# 1. name - The env variable to set
|
2015-10-20 20:01:14 +00:00
|
|
|
# 2. val - The default value
|
2012-05-05 08:39:31 +00:00
|
|
|
# Return value:
|
|
|
|
# 0 if the env variable exists, 3 if it was set
|
|
|
|
#
|
|
|
|
function env_default() {
|
2020-04-05 19:34:53 +00:00
|
|
|
[[ ${parameters[$1]} = *-export* ]] && return 0
|
2019-02-25 22:20:47 +00:00
|
|
|
export "$1=$2" && return 3
|
2013-12-03 07:47:21 +00:00
|
|
|
}
|
2015-08-09 20:28:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Required for $langinfo
|
|
|
|
zmodload zsh/langinfo
|
|
|
|
|
|
|
|
# URL-encode a string
|
|
|
|
#
|
|
|
|
# Encodes a string using RFC 2396 URL-encoding (%-escaped).
|
|
|
|
# See: https://www.ietf.org/rfc/rfc2396.txt
|
|
|
|
#
|
|
|
|
# By default, reserved characters and unreserved "mark" characters are
|
|
|
|
# not escaped by this function. This allows the common usage of passing
|
2015-10-20 20:01:14 +00:00
|
|
|
# an entire URL in, and encoding just special characters in it, with
|
2015-08-09 20:28:47 +00:00
|
|
|
# the expectation that reserved and mark characters are used appropriately.
|
|
|
|
# The -r and -m options turn on escaping of the reserved and mark characters,
|
|
|
|
# respectively, which allows arbitrary strings to be fully escaped for
|
|
|
|
# embedding inside URLs, where reserved characters might be misinterpreted.
|
|
|
|
#
|
|
|
|
# Prints the encoded string on stdout.
|
|
|
|
# Returns nonzero if encoding failed.
|
|
|
|
#
|
|
|
|
# Usage:
|
2015-10-31 18:53:41 +00:00
|
|
|
# omz_urlencode [-r] [-m] [-P] <string>
|
2015-10-20 20:01:14 +00:00
|
|
|
#
|
2015-08-09 20:28:47 +00:00
|
|
|
# -r causes reserved characters (;/?:@&=+$,) to be escaped
|
|
|
|
#
|
|
|
|
# -m causes "mark" characters (_.!~*''()-) to be escaped
|
|
|
|
#
|
|
|
|
# -P causes spaces to be encoded as '%20' instead of '+'
|
|
|
|
function omz_urlencode() {
|
|
|
|
emulate -L zsh
|
2020-11-09 17:33:59 +00:00
|
|
|
local -a opts
|
2015-08-09 20:28:47 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
# URLs must use UTF-8 encoding; convert str to UTF-8 if required
|
|
|
|
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
|
|
|
|
|
|
|
|
# Use LC_CTYPE=C to process text byte-by-byte
|
|
|
|
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
|
|
|
|
# $mark must be last because of the "-"
|
|
|
|
if [[ -z $opts[(r)-m] ]]; then
|
|
|
|
dont_escape+=$mark
|
|
|
|
fi
|
|
|
|
dont_escape+="]"
|
|
|
|
|
|
|
|
# Implemented to use a single printf call and avoid subshells in the loop,
|
|
|
|
# for performance (primarily on Windows).
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
# URL-decode a string
|
|
|
|
#
|
|
|
|
# Decodes a RFC 2396 URL-encoded (%-escaped) string.
|
2015-10-20 20:01:14 +00:00
|
|
|
# This decodes the '+' and '%' escapes in the input string, and leaves
|
|
|
|
# other characters unchanged. Does not enforce that the input is a
|
2015-08-09 20:28:47 +00:00
|
|
|
# valid URL-encoded string. This is a convenience to allow callers to
|
|
|
|
# pass in a full URL or similar strings and decode them for human
|
|
|
|
# presentation.
|
|
|
|
#
|
|
|
|
# Outputs the encoded string on stdout.
|
|
|
|
# Returns nonzero if encoding failed.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# omz_urldecode <urlstring> - prints decoded string followed by a newline
|
|
|
|
function omz_urldecode {
|
|
|
|
emulate -L zsh
|
|
|
|
local encoded_url=$1
|
|
|
|
|
|
|
|
# Work bytewise, since URLs escape UTF-8 octets
|
|
|
|
local caller_encoding=$langinfo[CODESET]
|
|
|
|
local LC_ALL=C
|
|
|
|
export LC_ALL
|
2015-10-20 20:01:14 +00:00
|
|
|
|
2015-08-09 20:28:47 +00:00
|
|
|
# Change + back to ' '
|
|
|
|
local tmp=${encoded_url:gs/+/ /}
|
|
|
|
# Protect other escapes to pass through the printf unchanged
|
|
|
|
tmp=${tmp:gs/\\/\\\\/}
|
|
|
|
# Handle %-escapes by turning them into `\xXX` printf escapes
|
|
|
|
tmp=${tmp:gs/%/\\x/}
|
|
|
|
local decoded
|
|
|
|
eval "decoded=\$'$tmp'"
|
|
|
|
|
|
|
|
# Now we have a UTF-8 encoded string in the variable. We need to re-encode
|
|
|
|
# it if caller is in a non-UTF-8 locale.
|
|
|
|
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"
|
|
|
|
}
|