1 # Shrink directory paths, e.g. /home/me/foo/bar/quux -> ~/f/b/quux.
3 # For a fish-style working directory in your command prompt, add the following
4 # to your theme or zshrc:
7 # PS1='%n@%m $(shrink_path -f)>'
9 # The following options are available:
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
23 # The long options can also be set via zstyle, like
24 # zstyle :prompt:shrink_path fish yes
26 # Note: Directory names containing two or more consecutive spaces are not yet
29 # Keywords: prompt directory truncate shrink collapse fish
31 # Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
32 # License: WTFPL <http://www.wtfpl.net>
34 # Ref: https://www.zsh.org/mla/workers/2009/msg00415.html
35 # https://www.zsh.org/mla/workers/2009/msg00419.html
39 setopt rc_quotes null_glob
50 if zstyle -t ':prompt:shrink_path' fish; then
55 if zstyle -t ':prompt:shrink_path' nameddirs; then
59 zstyle -t ':prompt:shrink_path' last && lastfull=1
60 zstyle -t ':prompt:shrink_path' short && short=1
61 zstyle -t ':prompt:shrink_path' tilde && tilde=1
62 zstyle -t ':prompt:shrink_path' glob && ellipsis='*'
63 zstyle -t ':prompt:shrink_path' quote && quote=1
64 zstyle -t ':prompt:shrink_path' expand && expand=1
66 while [[ $1 == -* ]]; do
78 print 'Usage: shrink_path [-f -l -s -t] [directory]'
79 print ' -f, --fish fish-simulation, like -l -s -t'
80 print ' -g, --glob Add asterisk to allow globbing of shrunk path (equivalent to -e "*")'
81 print ' -l, --last Print the last directory''s full name'
82 print ' -s, --short Truncate directory names to the number of characters given by -#. Without'
83 print ' -s, names are truncated without making them ambiguous.'
84 print ' -t, --tilde Substitute ~ for the home directory'
85 print ' -T, --nameddirs Substitute named directories as well'
86 print ' -# Truncate each directly to at least this many characters inclusive of the'
87 print ' ellipsis character(s) (defaulting to 1).'
88 print ' -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.'
89 print ' -q, --quote Quote special characters in the shrunk path'
90 print ' -x, --expand Print the full path. This takes precedence over the other options'
92 print 'The long options can also be set via zstyle, like'
93 print ' zstyle :prompt:shrink_path fish yes'
96 -l|--last) lastfull=1 ;;
97 -s|--short) short=1 ;;
98 -t|--tilde) tilde=1 ;;
123 typeset -i elllen=${#ellipsis}
125 typeset result part dir=${1-$PWD}
128 [[ -d $dir ]] || return 0
136 for part in ${(k)nameddirs}; {
137 [[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part}
140 (( tilde )) && dir=${dir/#$HOME/\~}
143 if [[ $tree[1] == \~* ]] {
151 if (( lastfull && $#tree == 1 )) {
158 until [[ $i -gt 99 || ( $i -ge $((length - ellen)) || $dir == $part ) && ( (( ${#expn} == 1 )) || $dir = $expn ) ]]; do
161 expn=($(echo ${part}*(-/)))
162 (( short )) && [[ $i -ge $((length - ellen)) ]] && break
165 typeset -i dif=$(( ${#dir} - ${#part} - ellen ))
168 (( quote )) && part=${(q)part}
172 (( quote )) && part=${(q)part}