functions.zsh 6.0 KB

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