functions.zsh 6.1 KB

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