2009-09-01 00:00:15 +02:00
|
|
|
function zsh_stats() {
|
2020-09-26 12:17:12 +02:00
|
|
|
fc -l 1 \
|
|
|
|
| awk '{ CMD[$2]++; count++; } END { for (a in CMD) print CMD[a] " " CMD[a]*100/count "% " a }' \
|
2021-11-09 09:04:10 +01:00
|
|
|
| grep -v "./" | sort -nr | head -n 20 | column -c3 -s " " -t | nl
|
2009-09-01 00:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function uninstall_oh_my_zsh() {
|
2023-05-23 08:43:47 +02:00
|
|
|
command env ZSH="$ZSH" sh "$ZSH/tools/uninstall.sh"
|
2009-09-01 00:09:34 +02:00
|
|
|
}
|
|
|
|
|
2009-09-01 00:48:09 +02:00
|
|
|
function upgrade_oh_my_zsh() {
|
2020-12-07 19:57:35 +01:00
|
|
|
echo >&2 "${fg[yellow]}Note: \`$0\` is deprecated. Use \`omz update\` instead.$reset_color"
|
|
|
|
omz update
|
2009-09-01 00:48:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:27:51 +02:00
|
|
|
function open_command() {
|
|
|
|
local open_cmd
|
|
|
|
|
|
|
|
# define the open command
|
|
|
|
case "$OSTYPE" in
|
2015-11-30 21:18:27 +01:00
|
|
|
darwin*) open_cmd='open' ;;
|
|
|
|
cygwin*) open_cmd='cygstart' ;;
|
2019-09-19 16:20:02 +02:00
|
|
|
linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
|
2018-06-29 20:20:56 +02:00
|
|
|
open_cmd='cmd.exe /c start ""'
|
|
|
|
[[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
|
|
|
|
} ;;
|
2015-11-30 21:18:27 +01:00
|
|
|
msys*) open_cmd='start ""' ;;
|
2015-08-05 00:27:51 +02:00
|
|
|
*) echo "Platform $OSTYPE not supported"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2023-03-03 12:34:31 +01:00
|
|
|
# If a URL is passed, $BROWSER might be set to a local browser within SSH.
|
|
|
|
# See https://github.com/ohmyzsh/ohmyzsh/issues/11098
|
|
|
|
if [[ -n "$BROWSER" && "$1" = (http|https)://* ]]; then
|
|
|
|
"$BROWSER" "$@"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2019-09-19 16:20:02 +02:00
|
|
|
${=open_cmd} "$@" &>/dev/null
|
2015-08-05 00:27:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 21:05:00 +02:00
|
|
|
# take functions
|
|
|
|
|
|
|
|
# mkcd is equivalent to takedir
|
|
|
|
function mkcd takedir() {
|
|
|
|
mkdir -p $@ && cd ${@:$#}
|
|
|
|
}
|
|
|
|
|
2021-06-12 06:09:08 +02:00
|
|
|
function takeurl() {
|
2021-08-10 21:09:21 +02:00
|
|
|
local data thedir
|
|
|
|
data="$(mktemp)"
|
|
|
|
curl -L "$1" > "$data"
|
|
|
|
tar xf "$data"
|
2021-11-09 09:04:10 +01:00
|
|
|
thedir="$(tar tf "$data" | head -n 1)"
|
2021-08-10 21:09:21 +02:00
|
|
|
rm "$data"
|
|
|
|
cd "$thedir"
|
2021-06-12 06:09:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-18 20:26:38 +02:00
|
|
|
function takezip() {
|
|
|
|
local data thedir
|
|
|
|
data="$(mktemp)"
|
|
|
|
curl -L "$1" > "$data"
|
|
|
|
unzip "$data" -d "./"
|
|
|
|
thedir="$(unzip -l "$data" | awk 'NR==4 {print $4}' | sed 's/\/.*//')"
|
|
|
|
rm "$data"
|
|
|
|
cd "$thedir"
|
|
|
|
}
|
|
|
|
|
2021-06-12 06:09:08 +02:00
|
|
|
function takegit() {
|
2021-08-10 21:09:21 +02:00
|
|
|
git clone "$1"
|
|
|
|
cd "$(basename ${1%%.git})"
|
2021-06-12 06:09:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function take() {
|
2023-01-20 19:45:14 +01:00
|
|
|
if [[ $1 =~ ^(https?|ftp).*\.(tar\.(gz|bz2|xz)|tgz)$ ]]; then
|
2021-08-10 21:09:21 +02:00
|
|
|
takeurl "$1"
|
2024-09-18 20:26:38 +02:00
|
|
|
elif [[ $1 =~ ^(https?|ftp).*\.(zip)$ ]]; then
|
|
|
|
takezip "$1"
|
2021-06-12 06:09:08 +02:00
|
|
|
elif [[ $1 =~ ^([A-Za-z0-9]\+@|https?|git|ssh|ftps?|rsync).*\.git/?$ ]]; then
|
2021-08-10 21:09:21 +02:00
|
|
|
takegit "$1"
|
2021-06-12 06:09:08 +02:00
|
|
|
else
|
2021-08-10 21:09:21 +02:00
|
|
|
takedir "$@"
|
2021-06-12 06:09:08 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-05-05 10:39:05 +02: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 19:49:02 +02:00
|
|
|
(( $+aliases[$1] )) && echo $aliases[$1]
|
2012-05-05 10:39:05 +02: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 10:39:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set variable "$1" to default value "$2" if "$1" is not yet defined.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# 1. name - The variable to set
|
2015-10-20 22:01:14 +02:00
|
|
|
# 2. val - The default value
|
2012-05-05 10:39:31 +02:00
|
|
|
# Return value:
|
|
|
|
# 0 if the variable exists, 3 if it was set
|
|
|
|
#
|
|
|
|
function default() {
|
2019-02-25 23:20:47 +01:00
|
|
|
(( $+parameters[$1] )) && return 0
|
2012-05-05 10:39:31 +02:00
|
|
|
typeset -g "$1"="$2" && return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2016-12-30 13:34:16 +01:00
|
|
|
# Set environment variable "$1" to default value "$2" if "$1" is not yet defined.
|
2012-05-05 10:39:31 +02:00
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# 1. name - The env variable to set
|
2015-10-20 22:01:14 +02:00
|
|
|
# 2. val - The default value
|
2012-05-05 10:39:31 +02:00
|
|
|
# Return value:
|
|
|
|
# 0 if the env variable exists, 3 if it was set
|
|
|
|
#
|
|
|
|
function env_default() {
|
2020-04-05 21:34:53 +02:00
|
|
|
[[ ${parameters[$1]} = *-export* ]] && return 0
|
2019-02-25 23:20:47 +01:00
|
|
|
export "$1=$2" && return 3
|
2013-12-03 08:47:21 +01:00
|
|
|
}
|
2015-08-09 22:28:47 +02: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 22:01:14 +02:00
|
|
|
# an entire URL in, and encoding just special characters in it, with
|
2015-08-09 22:28:47 +02: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:
|
2022-05-15 13:20:33 +02:00
|
|
|
# omz_urlencode [-r] [-m] [-P] <string> [<string> ...]
|
2015-10-20 22:01:14 +02:00
|
|
|
#
|
2015-08-09 22:28:47 +02: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
|
2024-07-27 14:28:24 +02:00
|
|
|
setopt norematchpcre
|
|
|
|
|
2020-11-09 18:33:59 +01:00
|
|
|
local -a opts
|
2015-08-09 22:28:47 +02:00
|
|
|
zparseopts -D -E -a opts r m P
|
|
|
|
|
2022-05-15 13:20:33 +02:00
|
|
|
local in_str="$@"
|
2015-08-09 22:28:47 +02:00
|
|
|
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
|
2023-12-06 08:09:45 +01:00
|
|
|
# Note that this doesn't work in Termux, as it only has UTF-8 locale.
|
|
|
|
# Characters will be processed as UTF-8, which is fine for URLs.
|
2015-08-09 22:28:47 +02:00
|
|
|
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+="+"
|
2023-12-06 08:09:45 +01:00
|
|
|
elif [[ "$PREFIX" = *com.termux* ]]; then
|
|
|
|
# Termux does not have non-UTF8 locales, so just send the UTF-8 character directly
|
|
|
|
url_str+="$byte"
|
2015-08-09 22:28:47 +02:00
|
|
|
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 22:01:14 +02: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 22:28:47 +02: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 22:01:14 +02:00
|
|
|
|
2015-08-09 22:28:47 +02: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/}
|
2021-11-08 17:46:14 +01:00
|
|
|
local decoded="$(printf -- "$tmp")"
|
2015-08-09 22:28:47 +02:00
|
|
|
|
|
|
|
# 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.
|
2021-11-08 17:46:14 +01:00
|
|
|
local -a safe_encodings
|
2015-08-09 22:28:47 +02:00
|
|
|
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"
|
|
|
|
}
|