2
0

shrink-path.plugin.zsh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # Shrink directory paths, e.g. /home/me/foo/bar/quux -> ~/f/b/quux.
  2. #
  3. # For a fish-style working directory in your command prompt, add the following
  4. # to your theme or zshrc:
  5. #
  6. # setopt prompt_subst
  7. # PS1='%n@%m $(shrink_path -f)>'
  8. #
  9. # The following options are available:
  10. #
  11. # -f, --fish fish simulation, equivalent to -l -s -t.
  12. # -g, --glob Add asterisk to allow globbing of shrunk path (equivalent to -e "*")
  13. # -l, --last Print the last directory's full name.
  14. # -s, --short Truncate directory names to the number of characters given by -#. Without
  15. # -s, names are truncated without making them ambiguous.
  16. # -t, --tilde Substitute ~ for the home directory.
  17. # -T, --nameddirs Substitute named directories as well.
  18. # -# Truncate each directly to at least this many characters inclusive of the
  19. # ellipsis character(s) (defaulting to 1).
  20. # -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.
  21. # -q, --quote Quote special characters in the shrunk path
  22. #
  23. # The long options can also be set via zstyle, like
  24. # zstyle :prompt:shrink_path fish yes
  25. #
  26. # Note: Directory names containing two or more consecutive spaces are not yet
  27. # supported.
  28. #
  29. # Keywords: prompt directory truncate shrink collapse fish
  30. #
  31. # Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
  32. # License: WTFPL <http://www.wtfpl.net>
  33. #
  34. # Ref: https://www.zsh.org/mla/workers/2009/msg00415.html
  35. # https://www.zsh.org/mla/workers/2009/msg00419.html
  36. shrink_path () {
  37. setopt localoptions
  38. setopt rc_quotes null_glob
  39. typeset -i lastfull=0
  40. typeset -i short=0
  41. typeset -i tilde=0
  42. typeset -i named=0
  43. typeset -i length=1
  44. typeset ellipsis=""
  45. typeset -i quote=0
  46. typeset -i expand=0
  47. if zstyle -t ':prompt:shrink_path' fish; then
  48. lastfull=1
  49. short=1
  50. tilde=1
  51. fi
  52. if zstyle -t ':prompt:shrink_path' nameddirs; then
  53. tilde=1
  54. named=1
  55. fi
  56. local last
  57. zstyle -s ':prompt:shrink_path' last last
  58. case "$last" in
  59. (false|no|off|0) lastfull=0 ;;
  60. (true|yes|on|1) lastfull=1 ;;
  61. (""|*[^0-9]*) lastfull=0 ;;
  62. (*) lastfull=$last ;;
  63. esac
  64. zstyle -t ':prompt:shrink_path' short && short=1
  65. zstyle -t ':prompt:shrink_path' tilde && tilde=1
  66. zstyle -t ':prompt:shrink_path' glob && ellipsis='*'
  67. zstyle -t ':prompt:shrink_path' quote && quote=1
  68. zstyle -t ':prompt:shrink_path' expand && expand=1
  69. while [[ $1 == -* ]]; do
  70. case $1 in
  71. --)
  72. shift
  73. break
  74. ;;
  75. -f|--fish)
  76. lastfull=1
  77. short=1
  78. tilde=1
  79. ;;
  80. -h|--help)
  81. print 'Usage: shrink_path [-f -l -s -t] [directory]'
  82. print ' -f, --fish fish-simulation, like -l -s -t'
  83. print ' -g, --glob Add asterisk to allow globbing of shrunk path (equivalent to -e "*")'
  84. print ' -l, --last [#] Print the last n directory''s full name (default 1).'
  85. print ' -s, --short Truncate directory names to the number of characters given by -#. Without'
  86. print ' -s, names are truncated without making them ambiguous.'
  87. print ' -t, --tilde Substitute ~ for the home directory'
  88. print ' -T, --nameddirs Substitute named directories as well'
  89. print ' -# Truncate each directly to at least this many characters inclusive of the'
  90. print ' ellipsis character(s) (defaulting to 1).'
  91. print ' -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.'
  92. print ' -q, --quote Quote special characters in the shrunk path'
  93. print ' -x, --expand Print the full path. This takes precedence over the other options'
  94. print ''
  95. print 'The long options can also be set via zstyle, like'
  96. print ' zstyle :prompt:shrink_path fish yes'
  97. return 0
  98. ;;
  99. -l|--last)
  100. lastfull=1
  101. if [[ -n "$2" && "$2" != *[^0-9]* ]]; then
  102. shift
  103. lastfull=$1
  104. fi
  105. ;;
  106. -s|--short) short=1 ;;
  107. -t|--tilde) tilde=1 ;;
  108. -T|--nameddirs)
  109. tilde=1
  110. named=1
  111. ;;
  112. -[0-9]|-[0-9][0-9])
  113. length=${1/-/}
  114. ;;
  115. -e)
  116. shift
  117. ellipsis="$1"
  118. ;;
  119. -g|--glob)
  120. ellipsis='*'
  121. ;;
  122. -q|--quote)
  123. quote=1
  124. ;;
  125. -x|--expand)
  126. expand=1
  127. ;;
  128. esac
  129. shift
  130. done
  131. typeset -i elllen=${#ellipsis}
  132. typeset -a tree expn
  133. typeset result part dir=${1-$PWD}
  134. typeset -i i
  135. [[ -d $dir ]] || return 0
  136. if (( expand )) {
  137. echo "$dir"
  138. return 0
  139. }
  140. if (( named )) {
  141. for part in ${(k)nameddirs}; {
  142. [[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part}
  143. }
  144. }
  145. (( tilde )) && dir=${dir/#$HOME/\~}
  146. tree=(${(s:/:)dir})
  147. (
  148. if [[ $tree[1] == \~* ]] {
  149. cd -q ${~tree[1]}
  150. result=$tree[1]
  151. shift tree
  152. } else {
  153. cd -q /
  154. }
  155. for dir in $tree; {
  156. if (( lastfull && $#tree <= lastfull )) {
  157. result+="/${(j:/:)tree[@]}"
  158. break
  159. }
  160. expn=(a b)
  161. part=''
  162. i=0
  163. until [[ $i -gt 99 || ( $i -ge $((length - ellen)) || $dir == $part ) && ( (( ${#expn} == 1 )) || $dir = $expn ) ]]; do
  164. (( i++ ))
  165. part+=$dir[$i]
  166. expn=($(echo ${part}*(-/)))
  167. (( short )) && [[ $i -ge $((length - ellen)) ]] && break
  168. done
  169. typeset -i dif=$(( ${#dir} - ${#part} - ellen ))
  170. if [[ $dif -gt 0 ]]
  171. then
  172. (( quote )) && part=${(q)part}
  173. part+="$ellipsis"
  174. else
  175. part="$dir"
  176. (( quote )) && part=${(q)part}
  177. fi
  178. result+="/$part"
  179. cd -q $dir
  180. shift tree
  181. }
  182. echo ${result:-/}
  183. )
  184. }
  185. ## vim:ft=zsh