2f345a3d0c9a94891c909a9ae6f6e2e2dc6df5b2

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

shrink-path: added glob and quote options (#7694)

* Added -g option to shrink-path plugin.

The new optins adds an asterix to the uniquely shortened name, such
that, if copy'n'pasted, zsh globbing will expand to the full path name
of the shortened path:

shrink-path -g:
"/net/software/modules/modulefiles" -> "/n*/s*/modules/m*"

shrink-path -l -g:
"/net/software/modules/modulefiles" -> "/n*/s*/modules/modulefiles"

* Updated also the README file with the new -g option.

* shrink-path: improved handling of special cases

This commits improves the handling of special cases in path strings:

- handling of white space in path names
- handling of single-letter path names

* Update plugins/shrink-path/README.md

Co-Authored-By: Jacob Tomaw <jacob.tomaw@gmail.com>

* Fix typo "Asterix" and replace with "asterisk"

* Add optional quoting support to shrink_path

Co-authored-by: Jacob Tomaw <jacob.tomaw@gmail.com>

Diff

  1diff --git a/plugins/shrink-path/README.md b/plugins/shrink-path/README.md
  2index 54bc351d3566a0112c530c7fa265a21d273cf44a..33dc3f8ae2a288b483311dde905019058b9e3f54 100644
  3--- a/plugins/shrink-path/README.md
  4+++ b/plugins/shrink-path/README.md
  5@@ -7,21 +7,26 @@ For this directory tree:
  6 ```
  7     /home/
  8       me/
  9-        foo/
 10+        f o o/     # The prefix f is ambiguous between "f o o" and "f i g".
 11           bar/
 12             quux/
 13           biz/     # The prefix b is ambiguous between bar and biz.
 14+        f i g/
 15+          baz/
 16 ```
 17 here are the results of calling `shrink_path <option> /home/me/foo/bar/quux`:
 18 ```
 19     Option        Result
 20-    <none>        /h/m/f/ba/q
 21-    -l|--last     /h/m/f/ba/quux
 22+    <none>        /h/m/f o/ba/q
 23+    -l|--last     /h/m/f o/ba/q
 24     -s|--short    /h/m/f/b/q
 25-    -t|--tilde    ~/f/ba/q
 26+    -t|--tilde    ~/f o/ba/q
 27     -f|--fish     ~/f/b/quux
 28-    -3            /hom/me/foo/bar/quu
 29-    -e '$' -3     /ho$/me/foo/bar/qu$
 30+    -g|--glob     /h*/m*/f o*/ba*/q*
 31+    -3            /hom/me/f o/bar/quu
 32+    -e '$' -3     /hom$/me/f o$/bar/quu$
 33+    -q            /h/m/f\ o/ba/q
 34+    -g -q         /h*/m*/f\ o*/ba*/q*
 35 ```
 36 
 37 
 38@@ -39,6 +44,7 @@ The following options are available:
 39 
 40 ```
 41     -f, --fish       fish simulation, equivalent to -l -s -t.
 42+    -g, --glob       Add asterisk to allow globbing of shrunk path (equivalent to -e "*")
 43     -l, --last       Print the last directory's full name.
 44     -s, --short      Truncate directory names to the number of characters given by -. Without
 45                      -s, names are truncated without making them ambiguous.
 46@@ -47,6 +53,7 @@ The following options are available:
 47     -#               Truncate each directly to at least this many characters inclusive of the
 48                      ellipsis character(s) (defaulting to 1).
 49     -e SYMBOL        Postfix symbol(s) to indicate that a directory name had been truncated.
 50+    -q, --quote      Quote special characters in the shrunk path
 51 ```
 52 
 53 The long options can also be set via zstyle, like
 54diff --git a/plugins/shrink-path/shrink-path.plugin.zsh b/plugins/shrink-path/shrink-path.plugin.zsh
 55index 19dc19c2aefa03df7745851193b72f2d5a9f9c73..f0ed6513839dbb53b1611b3cf0d39017a0cbc2cf 100644
 56--- a/plugins/shrink-path/shrink-path.plugin.zsh
 57+++ b/plugins/shrink-path/shrink-path.plugin.zsh
 58@@ -9,6 +9,7 @@
 59 # The following options are available:
 60 #
 61 #   -f, --fish       fish simulation, equivalent to -l -s -t.
 62+#   -g, --glob       Add asterisk to allow globbing of shrunk path (equivalent to -e "*")
 63 #   -l, --last       Print the last directory's full name.
 64 #   -s, --short      Truncate directory names to the number of characters given by -#. Without
 65 #                    -s, names are truncated without making them ambiguous.
 66@@ -17,6 +18,7 @@
 67 #   -#               Truncate each directly to at least this many characters inclusive of the
 68 #                    ellipsis character(s) (defaulting to 1).
 69 #   -e SYMBOL        Postfix symbol(s) to indicate that a directory name had been truncated.
 70+#   -q, --quote      Quote special characters in the shrunk path
 71 #
 72 # The long options can also be set via zstyle, like
 73 #   zstyle :prompt:shrink_path fish yes
 74@@ -42,6 +44,7 @@ shrink_path () {
 75         typeset -i named=0
 76         typeset -i length=1
 77         typeset ellipsis=""
 78+        typeset -i quote=0
 79 
 80         if zstyle -t ':prompt:shrink_path' fish; then
 81                 lastfull=1
 82@@ -55,6 +58,8 @@ shrink_path () {
 83         zstyle -t ':prompt:shrink_path' last && lastfull=1
 84         zstyle -t ':prompt:shrink_path' short && short=1
 85         zstyle -t ':prompt:shrink_path' tilde && tilde=1
 86+        zstyle -t ':prompt:shrink_path' glob && ellipsis='*'
 87+        zstyle -t ':prompt:shrink_path' quote && quote=1
 88 
 89         while [[ $1 == -* ]]; do
 90                 case $1 in
 91@@ -70,6 +75,7 @@ shrink_path () {
 92                         -h|--help)
 93                                 print 'Usage: shrink_path [-f -l -s -t] [directory]'
 94                                 print ' -f, --fish      fish-simulation, like -l -s -t'
 95+                                print ' -g, --glob      Add asterisk to allow globbing of shrunk path (equivalent to -e "*")'
 96                                 print ' -l, --last      Print the last directory''s full name'
 97                                 print ' -s, --short     Truncate directory names to the number of characters given by -#. Without'
 98                                 print '                 -s, names are truncated without making them ambiguous.'
 99@@ -78,6 +84,7 @@ shrink_path () {
100                                 print ' -#              Truncate each directly to at least this many characters inclusive of the'
101                                 print '                 ellipsis character(s) (defaulting to 1).'
102                                 print ' -e SYMBOL       Postfix symbol(s) to indicate that a directory name had been truncated.'
103+                                print ' -q, --quote     Quote special characters in the shrunk path'
104                                 print 'The long options can also be set via zstyle, like'
105                                 print '  zstyle :prompt:shrink_path fish yes'
106                                 return 0
107@@ -96,6 +103,12 @@ shrink_path () {
108                                 shift
109                                 ellipsis="$1"
110                         ;;
111+                        -g|--glob)
112+                                ellipsis='*'
113+                        ;;
114+                        -q|--quote)
115+                                quote=1
116+                        ;;
117                 esac
118                 shift
119         done
120@@ -140,9 +153,11 @@ shrink_path () {
121                         typeset -i dif=$(( ${#dir} - ${#part} - ellen ))
122                         if [[ $dif -gt 0 ]]
123                         then
124+                            (( quote )) && part=${(q)part}
125                             part+="$ellipsis"
126                         else
127                             part="$dir"
128+                            (( quote )) && part=${(q)part}
129                         fi
130                         result+="/$part"
131                         cd -q $dir