functions.zsh 6.4 KB

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