functions.zsh 5.8 KB

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