shrink-path.plugin.zsh 7.0 KB

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