c09323098acb03a0678f87036fad10bd99b283b2

Author
Shiv Venkatasubrahmanyam <shvenkat@users.noreply.github.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Add shrink-path plugin to shorten directory paths (e.g. ~/f/b/quux) (#5262)

* Add shrink-path plugin to shorten directory paths (e.g. ~/f/b/quux)

* Add a README

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/shrink-path/README.md b/plugins/shrink-path/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..51fa8a0510c2300107fa92a910a9cd8936a54f90
  4--- /dev/null
  5+++ b/plugins/shrink-path/README.md
  6@@ -0,0 +1,68 @@
  7+# A plugin to shrink directory paths for brevity and pretty-printing
  8+
  9+
 10+## Examples
 11+
 12+For this directory tree:
 13+```
 14+    /home/
 15+      me/
 16+        foo/
 17+          bar/
 18+            quux/
 19+          biz/     # The prefix b is ambiguous between bar and biz.
 20+```
 21+here are the results of calling `shrink_path <option> /home/me/foo/bar/quux`:
 22+```
 23+    Option        Result
 24+    <none>        /h/m/f/ba/q
 25+    -l|--last     /h/m/f/ba/quux
 26+    -s|--short    /h/m/f/b/q
 27+    -t|--tilde    ~/f/ba/q
 28+    -f|--fish     ~/f/b/quux
 29+```
 30+
 31+
 32+## Usage
 33+
 34+For a fish-style working directory in your command prompt, add the following to
 35+your theme or zshrc:
 36+
 37+```
 38+    setopt prompt_subst
 39+    PS1='%n@%m $(shrink_path -f)>'
 40+```
 41+
 42+The following options are available:
 43+
 44+```
 45+    -f, --fish       fish simulation, equivalent to -l -s -t.
 46+    -l, --last       Print the last directory's full name.
 47+    -s, --short      Truncate directory names to the first character. Without
 48+                     -s, names are truncated without making them ambiguous.
 49+    -t, --tilde      Substitute ~ for the home directory.
 50+    -T, --nameddirs  Substitute named directories as well.
 51+```
 52+
 53+The long options can also be set via zstyle, like
 54+```
 55+    zstyle :prompt:shrink_path fish yes
 56+```
 57+
 58+Note: Directory names containing two or more consecutive spaces are not yet
 59+supported.
 60+
 61+
 62+## License
 63+
 64+Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
 65+
 66+License: WTFPL <http://sam.zoy.org/wtfpl>
 67+
 68+Ref: http://www.zsh.org/mla/workers/2009/msg00415.html
 69+     http://www.zsh.org/mla/workers/2009/msg00419.html
 70+
 71+
 72+## Misc
 73+
 74+Keywords: prompt directory truncate shrink collapse fish
 75diff --git a/plugins/shrink-path/shrink-path.plugin.zsh b/plugins/shrink-path/shrink-path.plugin.zsh
 76new file mode 100644
 77index 0000000000000000000000000000000000000000..f111962a57aa839a8649da7a298613a90ea0982a
 78--- /dev/null
 79+++ b/plugins/shrink-path/shrink-path.plugin.zsh
 80@@ -0,0 +1,127 @@
 81+# Shrink directory paths, e.g. /home/me/foo/bar/quux -> ~/f/b/quux.
 82+#
 83+# For a fish-style working directory in your command prompt, add the following
 84+# to your theme or zshrc:
 85+#
 86+#   setopt prompt_subst
 87+#   PS1='%n@%m $(shrink_path -f)>'
 88+#
 89+# The following options are available:
 90+#
 91+#   -f, --fish       fish simulation, equivalent to -l -s -t.
 92+#   -l, --last       Print the last directory's full name.
 93+#   -s, --short      Truncate directory names to the first character. Without
 94+#                    -s, names are truncated without making them ambiguous.
 95+#   -t, --tilde      Substitute ~ for the home directory.
 96+#   -T, --nameddirs  Substitute named directories as well.
 97+#
 98+# The long options can also be set via zstyle, like
 99+#   zstyle :prompt:shrink_path fish yes
100+#
101+# Note: Directory names containing two or more consecutive spaces are not yet
102+# supported.
103+#
104+# Keywords: prompt directory truncate shrink collapse fish
105+#
106+# Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
107+# License: WTFPL <http://sam.zoy.org/wtfpl>
108+#
109+# Ref: http://www.zsh.org/mla/workers/2009/msg00415.html
110+#      http://www.zsh.org/mla/workers/2009/msg00419.html
111+
112+shrink_path () {
113+        setopt localoptions
114+        setopt rc_quotes null_glob
115+
116+        typeset -i lastfull=0
117+        typeset -i short=0
118+        typeset -i tilde=0
119+        typeset -i named=0
120+
121+        if zstyle -t ':prompt:shrink_path' fish; then
122+                lastfull=1
123+                short=1
124+                tilde=1
125+        fi
126+        if zstyle -t ':prompt:shrink_path' nameddirs; then
127+                tilde=1
128+                named=1
129+        fi
130+        zstyle -t ':prompt:shrink_path' last && lastfull=1
131+        zstyle -t ':prompt:shrink_path' short && short=1
132+        zstyle -t ':prompt:shrink_path' tilde && tilde=1
133+
134+        while [[ $1 == -* ]]; do
135+                case $1 in
136+                        -f|--fish)
137+                                lastfull=1
138+                                short=1
139+                                tilde=1
140+                        ;;
141+                        -h|--help)
142+                                print 'Usage: shrink_path [-f -l -s -t] [directory]'
143+                                print ' -f, --fish      fish-simulation, like -l -s -t'
144+                                print ' -l, --last      Print the last directory''s full name'
145+                                print ' -s, --short     Truncate directory names to the first character'
146+                                print ' -t, --tilde     Substitute ~ for the home directory'
147+                                print ' -T, --nameddirs Substitute named directories as well'
148+                                print 'The long options can also be set via zstyle, like'
149+                                print '  zstyle :prompt:shrink_path fish yes'
150+                                return 0
151+                        ;;
152+                        -l|--last) lastfull=1 ;;
153+                        -s|--short) short=1 ;;
154+                        -t|--tilde) tilde=1 ;;
155+                        -T|--nameddirs)
156+                                tilde=1
157+                                named=1
158+                        ;;
159+                esac
160+                shift
161+        done
162+
163+        typeset -a tree expn
164+        typeset result part dir=${1-$PWD}
165+        typeset -i i
166+
167+        [[ -d $dir ]] || return 0
168+
169+        if (( named )) {
170+                for part in ${(k)nameddirs}; {
171+                        [[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/${nameddirs[$part]}/\~$part}
172+                }
173+        }
174+        (( tilde )) && dir=${dir/$HOME/\~}
175+        tree=(${(s:/:)dir})
176+        (
177+                unfunction chpwd 2> /dev/null
178+                if [[ $tree[1] == \~* ]] {
179+                        cd ${~tree[1]}