functions.zsh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. local -a opts
  124. zparseopts -D -E -a opts r m P
  125. local in_str=$1
  126. local url_str=""
  127. local spaces_as_plus
  128. if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
  129. local str="$in_str"
  130. # URLs must use UTF-8 encoding; convert str to UTF-8 if required
  131. local encoding=$langinfo[CODESET]
  132. local safe_encodings
  133. safe_encodings=(UTF-8 utf8 US-ASCII)
  134. if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
  135. str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
  136. if [[ $? != 0 ]]; then
  137. echo "Error converting string from $encoding to UTF-8" >&2
  138. return 1
  139. fi
  140. fi
  141. # Use LC_CTYPE=C to process text byte-by-byte
  142. local i byte ord LC_ALL=C
  143. export LC_ALL
  144. local reserved=';/?:@&=+$,'
  145. local mark='_.!~*''()-'
  146. local dont_escape="[A-Za-z0-9"
  147. if [[ -z $opts[(r)-r] ]]; then
  148. dont_escape+=$reserved
  149. fi
  150. # $mark must be last because of the "-"
  151. if [[ -z $opts[(r)-m] ]]; then
  152. dont_escape+=$mark
  153. fi
  154. dont_escape+="]"
  155. # Implemented to use a single printf call and avoid subshells in the loop,
  156. # for performance (primarily on Windows).
  157. local url_str=""
  158. for (( i = 1; i <= ${#str}; ++i )); do
  159. byte="$str[i]"
  160. if [[ "$byte" =~ "$dont_escape" ]]; then
  161. url_str+="$byte"
  162. else
  163. if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
  164. url_str+="+"
  165. else
  166. ord=$(( [##16] #byte ))
  167. url_str+="%$ord"
  168. fi
  169. fi
  170. done
  171. echo -E "$url_str"
  172. }
  173. # URL-decode a string
  174. #
  175. # Decodes a RFC 2396 URL-encoded (%-escaped) string.
  176. # This decodes the '+' and '%' escapes in the input string, and leaves
  177. # other characters unchanged. Does not enforce that the input is a
  178. # valid URL-encoded string. This is a convenience to allow callers to
  179. # pass in a full URL or similar strings and decode them for human
  180. # presentation.
  181. #
  182. # Outputs the encoded string on stdout.
  183. # Returns nonzero if encoding failed.
  184. #
  185. # Usage:
  186. # omz_urldecode <urlstring> - prints decoded string followed by a newline
  187. function omz_urldecode {
  188. emulate -L zsh
  189. local encoded_url=$1
  190. # Work bytewise, since URLs escape UTF-8 octets
  191. local caller_encoding=$langinfo[CODESET]
  192. local LC_ALL=C
  193. export LC_ALL
  194. # Change + back to ' '
  195. local tmp=${encoded_url:gs/+/ /}
  196. # Protect other escapes to pass through the printf unchanged
  197. tmp=${tmp:gs/\\/\\\\/}
  198. # Handle %-escapes by turning them into `\xXX` printf escapes
  199. tmp=${tmp:gs/%/\\x/}
  200. local decoded
  201. eval "decoded=\$'$tmp'"
  202. # Now we have a UTF-8 encoded string in the variable. We need to re-encode
  203. # it if caller is in a non-UTF-8 locale.
  204. local safe_encodings
  205. safe_encodings=(UTF-8 utf8 US-ASCII)
  206. if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
  207. decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
  208. if [[ $? != 0 ]]; then
  209. echo "Error converting string from UTF-8 to $caller_encoding" >&2
  210. return 1
  211. fi
  212. fi
  213. echo -E "$decoded"
  214. }