functions.zsh 6.1 KB

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