Blame view
lib/functions.zsh
5.99 KB
|
093a6c34b
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function zsh_stats() {
fc -l 1 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n20
}
function uninstall_oh_my_zsh() {
env ZSH=$ZSH /bin/sh $ZSH/tools/uninstall.sh
}
function upgrade_oh_my_zsh() {
env ZSH=$ZSH /bin/sh $ZSH/tools/upgrade.sh
}
function take() {
mkdir -p $1
cd $1
}
|
|
dcebc9e8f
|
17 |
function open_command() {
|
|
7378b55de
|
18 19 |
emulate -L zsh
setopt shwordsplit
|
|
dcebc9e8f
|
20 21 22 23 |
local open_cmd
# define the open command
case "$OSTYPE" in
|
|
7378b55de
|
24 25 26 27 |
darwin*) open_cmd='open' ;;
cygwin*) open_cmd='cygstart' ;;
linux*) open_cmd='xdg-open' ;;
msys*) open_cmd='start ""' ;;
|
|
dcebc9e8f
|
28 29 30 31 |
*) echo "Platform $OSTYPE not supported"
return 1
;;
esac
|
|
7378b55de
|
32 33 34 35 36 37 |
# don't use nohup on OSX
if [[ "$OSTYPE" == darwin* ]]; then
$open_cmd "$@" &>/dev/null
else
nohup $open_cmd "$@" &>/dev/null
fi
|
|
dcebc9e8f
|
38 |
} |
|
093a6c34b
|
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#
# 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() {
alias "$1" | sed "s/^$1='\(.*\)'$/\1/"
test $(alias "$1")
}
#
# 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"
}
#
# Set variable "$1" to default value "$2" if "$1" is not yet defined.
#
# Arguments:
# 1. name - The variable to set
|
|
7378b55de
|
75 |
# 2. val - The default value |
|
093a6c34b
|
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# Return value:
# 0 if the variable exists, 3 if it was set
#
function default() {
test `typeset +m "$1"` && return 0
typeset -g "$1"="$2" && return 3
}
#
# Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined.
#
# Arguments:
# 1. name - The env variable to set
|
|
7378b55de
|
89 |
# 2. val - The default value |
|
093a6c34b
|
90 91 92 93 |
# Return value:
# 0 if the env variable exists, 3 if it was set
#
function env_default() {
|
|
7378b55de
|
94 |
env | grep -q "^$1=" && return 0 |
|
093a6c34b
|
95 96 |
export "$1=$2" && return 3 } |
|
ed37aae5b
|
97 98 99 100 101 102 103 104 105 106 107 108 |
# 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 |
|
7378b55de
|
109 |
# an entire URL in, and encoding just special characters in it, with |
|
ed37aae5b
|
110 111 112 113 114 115 116 117 118 |
# 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: |
|
7378b55de
|
119 120 |
# omz_urlencode [-r] [-m] [-P] <string> # |
|
ed37aae5b
|
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# -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
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.
|
|
7378b55de
|
185 186 |
# This decodes the '+' and '%' escapes in the input string, and leaves # other characters unchanged. Does not enforce that the input is a |
|
ed37aae5b
|
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# 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
|
|
7378b55de
|
204 |
|
|
ed37aae5b
|
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# 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"
}
|