functions.zsh 6.4 KB

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