shrink-path.plugin.zsh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. zstyle -t ':prompt:shrink_path' last && lastfull=1
  57. zstyle -t ':prompt:shrink_path' short && short=1
  58. zstyle -t ':prompt:shrink_path' tilde && tilde=1
  59. zstyle -t ':prompt:shrink_path' glob && ellipsis='*'
  60. zstyle -t ':prompt:shrink_path' quote && quote=1
  61. zstyle -t ':prompt:shrink_path' expand && expand=1
  62. while [[ $1 == -* ]]; do
  63. case $1 in
  64. --)
  65. shift
  66. break
  67. ;;
  68. -f|--fish)
  69. lastfull=1
  70. short=1
  71. tilde=1
  72. ;;
  73. -h|--help)
  74. print 'Usage: shrink_path [-f -l -s -t] [directory]'
  75. print ' -f, --fish fish-simulation, like -l -s -t'
  76. print ' -g, --glob Add asterisk to allow globbing of shrunk path (equivalent to -e "*")'
  77. print ' -l, --last Print the last directory''s full name'
  78. print ' -s, --short Truncate directory names to the number of characters given by -#. Without'
  79. print ' -s, names are truncated without making them ambiguous.'
  80. print ' -t, --tilde Substitute ~ for the home directory'
  81. print ' -T, --nameddirs Substitute named directories as well'
  82. print ' -# Truncate each directly to at least this many characters inclusive of the'
  83. print ' ellipsis character(s) (defaulting to 1).'
  84. print ' -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.'
  85. print ' -q, --quote Quote special characters in the shrunk path'
  86. print ' -x, --expand Print the full path. This takes precedence over the other options'
  87. print ''
  88. print 'The long options can also be set via zstyle, like'
  89. print ' zstyle :prompt:shrink_path fish yes'
  90. return 0
  91. ;;
  92. -l|--last) lastfull=1 ;;
  93. -s|--short) short=1 ;;
  94. -t|--tilde) tilde=1 ;;
  95. -T|--nameddirs)
  96. tilde=1
  97. named=1
  98. ;;
  99. -[0-9]|-[0-9][0-9])
  100. length=${1/-/}
  101. ;;
  102. -e)
  103. shift
  104. ellipsis="$1"
  105. ;;
  106. -g|--glob)
  107. ellipsis='*'
  108. ;;
  109. -q|--quote)
  110. quote=1
  111. ;;
  112. -x|--expand)
  113. expand=1
  114. ;;
  115. esac
  116. shift
  117. done
  118. typeset -i elllen=${#ellipsis}
  119. typeset -a tree expn
  120. typeset result part dir=${1-$PWD}
  121. typeset -i i
  122. [[ -d $dir ]] || return 0
  123. if (( expand )) {
  124. echo "$dir"
  125. return 0
  126. }
  127. if (( named )) {
  128. for part in ${(k)nameddirs}; {
  129. [[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part}
  130. }
  131. }
  132. (( tilde )) && dir=${dir/#$HOME/\~}
  133. tree=(${(s:/:)dir})
  134. (
  135. if [[ $tree[1] == \~* ]] {
  136. cd -q ${~tree[1]}
  137. result=$tree[1]
  138. shift tree
  139. } else {
  140. cd -q /
  141. }
  142. for dir in $tree; {
  143. if (( lastfull && $#tree == 1 )) {
  144. result+="/$tree"
  145. break
  146. }
  147. expn=(a b)
  148. part=''
  149. i=0
  150. until [[ $i -gt 99 || ( $i -ge $((length - ellen)) || $dir == $part ) && ( (( ${#expn} == 1 )) || $dir = $expn ) ]]; do
  151. (( i++ ))
  152. part+=$dir[$i]
  153. expn=($(echo ${part}*(-/)))
  154. (( short )) && [[ $i -ge $((length - ellen)) ]] && break
  155. done
  156. typeset -i dif=$(( ${#dir} - ${#part} - ellen ))
  157. if [[ $dif -gt 0 ]]
  158. then
  159. (( quote )) && part=${(q)part}
  160. part+="$ellipsis"
  161. else
  162. part="$dir"
  163. (( quote )) && part=${(q)part}
  164. fi
  165. result+="/$part"
  166. cd -q $dir
  167. shift tree
  168. }
  169. echo ${result:-/}
  170. )
  171. }
  172. ## vim:ft=zsh