shrink-path.plugin.zsh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # -l, --last Print the last directory's full name.
  13. # -s, --short Truncate directory names to the first character. Without
  14. # -s, names are truncated without making them ambiguous.
  15. # -t, --tilde Substitute ~ for the home directory.
  16. # -T, --nameddirs Substitute named directories as well.
  17. #
  18. # The long options can also be set via zstyle, like
  19. # zstyle :prompt:shrink_path fish yes
  20. #
  21. # Note: Directory names containing two or more consecutive spaces are not yet
  22. # supported.
  23. #
  24. # Keywords: prompt directory truncate shrink collapse fish
  25. #
  26. # Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
  27. # License: WTFPL <http://www.wtfpl.net>
  28. #
  29. # Ref: https://www.zsh.org/mla/workers/2009/msg00415.html
  30. # https://www.zsh.org/mla/workers/2009/msg00419.html
  31. shrink_path () {
  32. setopt localoptions
  33. setopt rc_quotes null_glob
  34. typeset -i lastfull=0
  35. typeset -i short=0
  36. typeset -i tilde=0
  37. typeset -i named=0
  38. if zstyle -t ':prompt:shrink_path' fish; then
  39. lastfull=1
  40. short=1
  41. tilde=1
  42. fi
  43. if zstyle -t ':prompt:shrink_path' nameddirs; then
  44. tilde=1
  45. named=1
  46. fi
  47. zstyle -t ':prompt:shrink_path' last && lastfull=1
  48. zstyle -t ':prompt:shrink_path' short && short=1
  49. zstyle -t ':prompt:shrink_path' tilde && tilde=1
  50. while [[ $1 == -* ]]; do
  51. case $1 in
  52. -f|--fish)
  53. lastfull=1
  54. short=1
  55. tilde=1
  56. ;;
  57. -h|--help)
  58. print 'Usage: shrink_path [-f -l -s -t] [directory]'
  59. print ' -f, --fish fish-simulation, like -l -s -t'
  60. print ' -l, --last Print the last directory''s full name'
  61. print ' -s, --short Truncate directory names to the first character'
  62. print ' -t, --tilde Substitute ~ for the home directory'
  63. print ' -T, --nameddirs Substitute named directories as well'
  64. print 'The long options can also be set via zstyle, like'
  65. print ' zstyle :prompt:shrink_path fish yes'
  66. return 0
  67. ;;
  68. -l|--last) lastfull=1 ;;
  69. -s|--short) short=1 ;;
  70. -t|--tilde) tilde=1 ;;
  71. -T|--nameddirs)
  72. tilde=1
  73. named=1
  74. ;;
  75. esac
  76. shift
  77. done
  78. typeset -a tree expn
  79. typeset result part dir=${1-$PWD}
  80. typeset -i i
  81. [[ -d $dir ]] || return 0
  82. if (( named )) {
  83. for part in ${(k)nameddirs}; {
  84. [[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part}
  85. }
  86. }
  87. (( tilde )) && dir=${dir/#$HOME/\~}
  88. tree=(${(s:/:)dir})
  89. (
  90. if [[ $tree[1] == \~* ]] {
  91. cd -q ${~tree[1]}
  92. result=$tree[1]
  93. shift tree
  94. } else {
  95. cd -q /
  96. }
  97. for dir in $tree; {
  98. if (( lastfull && $#tree == 1 )) {
  99. result+="/$tree"
  100. break
  101. }
  102. expn=(a b)
  103. part=''
  104. i=0
  105. until [[ (( ${#expn} == 1 )) || $dir = $expn || $i -gt 99 ]] do
  106. (( i++ ))
  107. part+=$dir[$i]
  108. expn=($(echo ${part}*(-/)))
  109. (( short )) && break
  110. done
  111. result+="/$part"
  112. cd -q $dir
  113. shift tree
  114. }
  115. echo ${result:-/}
  116. )
  117. }
  118. ## vim:ft=zsh