functions.zsh 6.0 KB

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