functions.zsh 6.5 KB

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