functions.zsh 6.1 KB

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