functions.zsh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. function zsh_stats() {
  2. fc -l 1 \
  3. | awk '{ CMD[$2]++; count++; } END { for (a in CMD) print CMD[a] " " CMD[a]*100/count "% " a }' \
  4. | grep -v "./" | sort -nr | head -n20 | column -c3 -s " " -t | nl
  5. }
  6. function uninstall_oh_my_zsh() {
  7. env ZSH="$ZSH" sh "$ZSH/tools/uninstall.sh"
  8. }
  9. function upgrade_oh_my_zsh() {
  10. if (( $+functions[_omz::update] )); then
  11. echo >&2 "${fg[yellow]}Note: \`$0\` is deprecated. Use \`omz update\` instead.$reset_color"
  12. fi
  13. # Run update script
  14. env ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
  15. local ret=$?
  16. # Update last updated file
  17. zmodload zsh/datetime
  18. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  19. # Remove update lock if it exists
  20. command rm -rf "$ZSH/log/update.lock"
  21. # Restart the zsh session
  22. if [[ $ret -eq 0 ]]; then
  23. # Check whether to run a login shell
  24. [[ "$ZSH_ARGZERO" = -* ]] && exec -l "${ZSH_ARGZERO#-}" || exec "$ZSH_ARGZERO"
  25. fi
  26. }
  27. function take() {
  28. mkdir -p $@ && cd ${@:$#}
  29. }
  30. function open_command() {
  31. local open_cmd
  32. # define the open command
  33. case "$OSTYPE" in
  34. darwin*) open_cmd='open' ;;
  35. cygwin*) open_cmd='cygstart' ;;
  36. linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
  37. open_cmd='cmd.exe /c start ""'
  38. [[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
  39. } ;;
  40. msys*) open_cmd='start ""' ;;
  41. *) echo "Platform $OSTYPE not supported"
  42. return 1
  43. ;;
  44. esac
  45. ${=open_cmd} "$@" &>/dev/null
  46. }
  47. #
  48. # Get the value of an alias.
  49. #
  50. # Arguments:
  51. # 1. alias - The alias to get its value from
  52. # STDOUT:
  53. # The value of alias $1 (if it has one).
  54. # Return value:
  55. # 0 if the alias was found,
  56. # 1 if it does not exist
  57. #
  58. function alias_value() {
  59. (( $+aliases[$1] )) && echo $aliases[$1]
  60. }
  61. #
  62. # Try to get the value of an alias,
  63. # otherwise return the input.
  64. #
  65. # Arguments:
  66. # 1. alias - The alias to get its value from
  67. # STDOUT:
  68. # The value of alias $1, or $1 if there is no alias $1.
  69. # Return value:
  70. # Always 0
  71. #
  72. function try_alias_value() {
  73. alias_value "$1" || echo "$1"
  74. }
  75. #
  76. # Set variable "$1" to default value "$2" if "$1" is not yet defined.
  77. #
  78. # Arguments:
  79. # 1. name - The variable to set
  80. # 2. val - The default value
  81. # Return value:
  82. # 0 if the variable exists, 3 if it was set
  83. #
  84. function default() {
  85. (( $+parameters[$1] )) && return 0
  86. typeset -g "$1"="$2" && return 3
  87. }
  88. #
  89. # Set environment variable "$1" to default value "$2" if "$1" is not yet defined.
  90. #
  91. # Arguments:
  92. # 1. name - The env variable to set
  93. # 2. val - The default value
  94. # Return value:
  95. # 0 if the env variable exists, 3 if it was set
  96. #
  97. function env_default() {
  98. [[ ${parameters[$1]} = *-export* ]] && return 0
  99. export "$1=$2" && return 3
  100. }
  101. # Required for $langinfo
  102. zmodload zsh/langinfo
  103. # URL-encode a string
  104. #
  105. # Encodes a string using RFC 2396 URL-encoding (%-escaped).
  106. # See: https://www.ietf.org/rfc/rfc2396.txt
  107. #
  108. # By default, reserved characters and unreserved "mark" characters are
  109. # not escaped by this function. This allows the common usage of passing
  110. # an entire URL in, and encoding just special characters in it, with
  111. # the expectation that reserved and mark characters are used appropriately.
  112. # The -r and -m options turn on escaping of the reserved and mark characters,
  113. # respectively, which allows arbitrary strings to be fully escaped for
  114. # embedding inside URLs, where reserved characters might be misinterpreted.
  115. #
  116. # Prints the encoded string on stdout.
  117. # Returns nonzero if encoding failed.
  118. #
  119. # Usage:
  120. # omz_urlencode [-r] [-m] [-P] <string>
  121. #
  122. # -r causes reserved characters (;/?:@&=+$,) to be escaped
  123. #
  124. # -m causes "mark" characters (_.!~*''()-) to be escaped
  125. #
  126. # -P causes spaces to be encoded as '%20' instead of '+'
  127. function omz_urlencode() {
  128. emulate -L zsh
  129. local -a opts
  130. zparseopts -D -E -a opts r m P
  131. local in_str=$1
  132. local url_str=""
  133. local spaces_as_plus
  134. if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
  135. local str="$in_str"
  136. # URLs must use UTF-8 encoding; convert str to UTF-8 if required
  137. local encoding=$langinfo[CODESET]
  138. local safe_encodings
  139. safe_encodings=(UTF-8 utf8 US-ASCII)
  140. if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
  141. str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
  142. if [[ $? != 0 ]]; then
  143. echo "Error converting string from $encoding to UTF-8" >&2
  144. return 1
  145. fi
  146. fi
  147. # Use LC_CTYPE=C to process text byte-by-byte
  148. local i byte ord LC_ALL=C
  149. export LC_ALL
  150. local reserved=';/?:@&=+$,'
  151. local mark='_.!~*''()-'
  152. local dont_escape="[A-Za-z0-9"
  153. if [[ -z $opts[(r)-r] ]]; then
  154. dont_escape+=$reserved
  155. fi
  156. # $mark must be last because of the "-"
  157. if [[ -z $opts[(r)-m] ]]; then
  158. dont_escape+=$mark
  159. fi
  160. dont_escape+="]"
  161. # Implemented to use a single printf call and avoid subshells in the loop,
  162. # for performance (primarily on Windows).
  163. local url_str=""
  164. for (( i = 1; i <= ${#str}; ++i )); do
  165. byte="$str[i]"
  166. if [[ "$byte" =~ "$dont_escape" ]]; then
  167. url_str+="$byte"
  168. else
  169. if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
  170. url_str+="+"
  171. else
  172. ord=$(( [##16] #byte ))
  173. url_str+="%$ord"
  174. fi
  175. fi
  176. done
  177. echo -E "$url_str"
  178. }
  179. # URL-decode a string
  180. #
  181. # Decodes a RFC 2396 URL-encoded (%-escaped) string.
  182. # This decodes the '+' and '%' escapes in the input string, and leaves
  183. # other characters unchanged. Does not enforce that the input is a
  184. # valid URL-encoded string. This is a convenience to allow callers to
  185. # pass in a full URL or similar strings and decode them for human
  186. # presentation.
  187. #
  188. # Outputs the encoded string on stdout.
  189. # Returns nonzero if encoding failed.
  190. #
  191. # Usage:
  192. # omz_urldecode <urlstring> - prints decoded string followed by a newline
  193. function omz_urldecode {
  194. emulate -L zsh
  195. local encoded_url=$1
  196. # Work bytewise, since URLs escape UTF-8 octets
  197. local caller_encoding=$langinfo[CODESET]
  198. local LC_ALL=C
  199. export LC_ALL
  200. # Change + back to ' '
  201. local tmp=${encoded_url:gs/+/ /}
  202. # Protect other escapes to pass through the printf unchanged
  203. tmp=${tmp:gs/\\/\\\\/}
  204. # Handle %-escapes by turning them into `\xXX` printf escapes
  205. tmp=${tmp:gs/%/\\x/}
  206. local decoded
  207. eval "decoded=\$'$tmp'"
  208. # Now we have a UTF-8 encoded string in the variable. We need to re-encode
  209. # it if caller is in a non-UTF-8 locale.
  210. local safe_encodings
  211. safe_encodings=(UTF-8 utf8 US-ASCII)
  212. if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
  213. decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
  214. if [[ $? != 0 ]]; then
  215. echo "Error converting string from UTF-8 to $caller_encoding" >&2
  216. return 1
  217. fi
  218. fi
  219. echo -E "$decoded"
  220. }