my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
6.6 KiB

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