functions.zsh 6.1 KB

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