e65bce849580fbb96c436b1a2a3739cb8c27c299

Author
Pavel N. Krivitsky <pavel-github@krivitsky.name>
Committer
GitHub <noreply@github.com>
Date

Message

shrink-path: add minimum length and ellipsis symbol options (#7382)

* Added minimum length and ellipsis symbol options for the shrink-path plugin.

* Replaced tab indentation with space indentation for the shrink-path plugin.

* Added handling of multicharacter ellipsis, and updated documentation and README for the shrink-path plugin.

Diff

  1diff --git a/plugins/shrink-path/README.md b/plugins/shrink-path/README.md
  2index b990aea91b7a1760c71173110d0fd01e6a60db38..54bc351d3566a0112c530c7fa265a21d273cf44a 100644
  3--- a/plugins/shrink-path/README.md
  4+++ b/plugins/shrink-path/README.md
  5@@ -20,6 +20,8 @@ here are the results of calling `shrink_path <option> /home/me/foo/bar/quux`:
  6     -s|--short    /h/m/f/b/q
  7     -t|--tilde    ~/f/ba/q
  8     -f|--fish     ~/f/b/quux
  9+    -3            /hom/me/foo/bar/quu
 10+    -e '$' -3     /ho$/me/foo/bar/qu$
 11 ```
 12 
 13 
 14@@ -38,10 +40,13 @@ The following options are available:
 15 ```
 16     -f, --fish       fish simulation, equivalent to -l -s -t.
 17     -l, --last       Print the last directory's full name.
 18-    -s, --short      Truncate directory names to the first character. Without
 19+    -s, --short      Truncate directory names to the number of characters given by -. Without
 20                      -s, names are truncated without making them ambiguous.
 21     -t, --tilde      Substitute ~ for the home directory.
 22     -T, --nameddirs  Substitute named directories as well.
 23+    -#               Truncate each directly to at least this many characters inclusive of the
 24+                     ellipsis character(s) (defaulting to 1).
 25+    -e SYMBOL        Postfix symbol(s) to indicate that a directory name had been truncated.
 26 ```
 27 
 28 The long options can also be set via zstyle, like
 29@@ -56,6 +61,7 @@ supported.
 30 ## License
 31 
 32 Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
 33+Copyright (C) 2018-2020 by Pavel N. Krivitsky
 34 
 35 License: WTFPL <http://www.wtfpl.net>
 36 
 37diff --git a/plugins/shrink-path/shrink-path.plugin.zsh b/plugins/shrink-path/shrink-path.plugin.zsh
 38index 86102e6512fed68219a097d7d189728749558506..19dc19c2aefa03df7745851193b72f2d5a9f9c73 100644
 39--- a/plugins/shrink-path/shrink-path.plugin.zsh
 40+++ b/plugins/shrink-path/shrink-path.plugin.zsh
 41@@ -10,10 +10,13 @@
 42 #
 43 #   -f, --fish       fish simulation, equivalent to -l -s -t.
 44 #   -l, --last       Print the last directory's full name.
 45-#   -s, --short      Truncate directory names to the first character. Without
 46+#   -s, --short      Truncate directory names to the number of characters given by -#. Without
 47 #                    -s, names are truncated without making them ambiguous.
 48 #   -t, --tilde      Substitute ~ for the home directory.
 49 #   -T, --nameddirs  Substitute named directories as well.
 50+#   -#               Truncate each directly to at least this many characters inclusive of the
 51+#                    ellipsis character(s) (defaulting to 1).
 52+#   -e SYMBOL        Postfix symbol(s) to indicate that a directory name had been truncated.
 53 #
 54 # The long options can also be set via zstyle, like
 55 #   zstyle :prompt:shrink_path fish yes
 56@@ -37,6 +40,8 @@ shrink_path () {
 57         typeset -i short=0
 58         typeset -i tilde=0
 59         typeset -i named=0
 60+        typeset -i length=1
 61+        typeset ellipsis=""
 62 
 63         if zstyle -t ':prompt:shrink_path' fish; then
 64                 lastfull=1
 65@@ -53,6 +58,10 @@ shrink_path () {
 66 
 67         while [[ $1 == -* ]]; do
 68                 case $1 in
 69+                        --)
 70+                                shift
 71+                                break
 72+                        ;;
 73                         -f|--fish)
 74                                 lastfull=1
 75                                 short=1
 76@@ -62,9 +71,13 @@ shrink_path () {
 77                                 print 'Usage: shrink_path [-f -l -s -t] [directory]'
 78                                 print ' -f, --fish      fish-simulation, like -l -s -t'
 79                                 print ' -l, --last      Print the last directory''s full name'
 80-                                print ' -s, --short     Truncate directory names to the first character'
 81+                                print ' -s, --short     Truncate directory names to the number of characters given by -#. Without'
 82+                                print '                 -s, names are truncated without making them ambiguous.'
 83                                 print ' -t, --tilde     Substitute ~ for the home directory'
 84                                 print ' -T, --nameddirs Substitute named directories as well'
 85+                                print ' -#              Truncate each directly to at least this many characters inclusive of the'
 86+                                print '                 ellipsis character(s) (defaulting to 1).'
 87+                                print ' -e SYMBOL       Postfix symbol(s) to indicate that a directory name had been truncated.'
 88                                 print 'The long options can also be set via zstyle, like'
 89                                 print '  zstyle :prompt:shrink_path fish yes'
 90                                 return 0
 91@@ -76,10 +89,18 @@ shrink_path () {
 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                 esac
103                 shift
104         done
105 
106+        typeset -i elllen=${#ellipsis}
107         typeset -a tree expn
108         typeset result part dir=${1-$PWD}
109         typeset -i i
110@@ -109,12 +130,20 @@ shrink_path () {
111                         expn=(a b)
112                         part=''
113                         i=0
114-                        until [[ (( ${#expn} == 1 )) || $dir = $expn || $i -gt 99 ]]  do
115+                        until [[ $i -gt 99 || ( $i -ge $((length - ellen)) || $dir == $part ) && ( (( ${#expn} == 1 )) || $dir = $expn ) ]]; do
116                                 (( i++ ))
117                                 part+=$dir[$i]
118                                 expn=($(echo ${part}*(-/)))
119-                                (( short )) && break
120+                                (( short )) && [[ $i -ge $((length - ellen)) ]] && break
121                         done
122+
123+                        typeset -i dif=$(( ${#dir} - ${#part} - ellen ))
124+                        if [[ $dif -gt 0 ]]
125+                        then
126+                            part+="$ellipsis"
127+                        else
128+                            part="$dir"
129+                        fi
130                         result+="/$part"
131                         cd -q $dir
132                         shift tree