functions.zsh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 /bin/sh $ZSH/tools/uninstall.sh
  6. }
  7. function upgrade_oh_my_zsh() {
  8. env ZSH=$ZSH /bin/sh $ZSH/tools/upgrade.sh
  9. }
  10. function take() {
  11. mkdir -p $1
  12. cd $1
  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*) open_cmd="xdg-open" ;;
  21. *) echo "Platform $OSTYPE not supported"
  22. return 1
  23. ;;
  24. esac
  25. # don't use nohup on OSX
  26. if [[ "$OSTYPE" == darwin* ]]; then
  27. $open_cmd "$@" &>/dev/null
  28. else
  29. nohup $open_cmd "$@" &>/dev/null
  30. fi
  31. }
  32. #
  33. # Get the value of an alias.
  34. #
  35. # Arguments:
  36. # 1. alias - The alias to get its value from
  37. # STDOUT:
  38. # The value of alias $1 (if it has one).
  39. # Return value:
  40. # 0 if the alias was found,
  41. # 1 if it does not exist
  42. #
  43. function alias_value() {
  44. alias "$1" | sed "s/^$1='\(.*\)'$/\1/"
  45. test $(alias "$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. test `typeset +m "$1"` && return 0
  72. typeset -g "$1"="$2" && return 3
  73. }
  74. #
  75. # Set enviroment 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. env | grep -q "^$1=" && 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. zparseopts -D -E -a opts r m P
  116. local in_str=$1
  117. local url_str=""
  118. local spaces_as_plus
  119. if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
  120. local str="$in_str"
  121. # URLs must use UTF-8 encoding; convert str to UTF-8 if required
  122. local encoding=$langinfo[CODESET]
  123. local safe_encodings
  124. safe_encodings=(UTF-8 utf8 US-ASCII)
  125. if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
  126. str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
  127. if [[ $? != 0 ]]; then
  128. echo "Error converting string from $encoding to UTF-8" >&2
  129. return 1
  130. fi
  131. fi
  132. # Use LC_CTYPE=C to process text byte-by-byte
  133. local i byte ord LC_ALL=C
  134. export LC_ALL
  135. local reserved=';/?:@&=+$,'
  136. local mark='_.!~*''()-'
  137. local dont_escape="[A-Za-z0-9"
  138. if [[ -z $opts[(r)-r] ]]; then
  139. dont_escape+=$reserved
  140. fi
  141. # $mark must be last because of the "-"
  142. if [[ -z $opts[(r)-m] ]]; then
  143. dont_escape+=$mark
  144. fi
  145. dont_escape+="]"
  146. # Implemented to use a single printf call and avoid subshells in the loop,
  147. # for performance (primarily on Windows).
  148. local url_str=""
  149. for (( i = 1; i <= ${#str}; ++i )); do
  150. byte="$str[i]"
  151. if [[ "$byte" =~ "$dont_escape" ]]; then
  152. url_str+="$byte"
  153. else
  154. if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
  155. url_str+="+"
  156. else
  157. ord=$(( [##16] #byte ))
  158. url_str+="%$ord"
  159. fi
  160. fi
  161. done
  162. echo -E "$url_str"
  163. }
  164. # URL-decode a string
  165. #
  166. # Decodes a RFC 2396 URL-encoded (%-escaped) string.
  167. # This decodes the '+' and '%' escapes in the input string, and leaves
  168. # other characters unchanged. Does not enforce that the input is a
  169. # valid URL-encoded string. This is a convenience to allow callers to
  170. # pass in a full URL or similar strings and decode them for human
  171. # presentation.
  172. #
  173. # Outputs the encoded string on stdout.
  174. # Returns nonzero if encoding failed.
  175. #
  176. # Usage:
  177. # omz_urldecode <urlstring> - prints decoded string followed by a newline
  178. function omz_urldecode {
  179. emulate -L zsh
  180. local encoded_url=$1
  181. # Work bytewise, since URLs escape UTF-8 octets
  182. local caller_encoding=$langinfo[CODESET]
  183. local LC_ALL=C
  184. export LC_ALL
  185. # Change + back to ' '
  186. local tmp=${encoded_url:gs/+/ /}
  187. # Protect other escapes to pass through the printf unchanged
  188. tmp=${tmp:gs/\\/\\\\/}
  189. # Handle %-escapes by turning them into `\xXX` printf escapes
  190. tmp=${tmp:gs/%/\\x/}
  191. local decoded
  192. eval "decoded=\$'$tmp'"
  193. # Now we have a UTF-8 encoded string in the variable. We need to re-encode
  194. # it if caller is in a non-UTF-8 locale.
  195. local safe_encodings
  196. safe_encodings=(UTF-8 utf8 US-ASCII)
  197. if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
  198. decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
  199. if [[ $? != 0 ]]; then
  200. echo "Error converting string from UTF-8 to $caller_encoding" >&2
  201. return 1
  202. fi
  203. fi
  204. echo -E "$decoded"
  205. }