functions.zsh 6.1 KB

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