functions.zsh 6.6 KB

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