From e65bce849580fbb96c436b1a2a3739cb8c27c299 Mon Sep 17 00:00:00 2001 From: "Pavel N. Krivitsky" Date: Mon, 2 Mar 2020 23:30:52 +1100 Subject: 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. --- plugins/shrink-path/shrink-path.plugin.zsh | 37 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'plugins/shrink-path/shrink-path.plugin.zsh') diff --git a/plugins/shrink-path/shrink-path.plugin.zsh b/plugins/shrink-path/shrink-path.plugin.zsh index 86102e651..19dc19c2a 100644 --- a/plugins/shrink-path/shrink-path.plugin.zsh +++ b/plugins/shrink-path/shrink-path.plugin.zsh @@ -10,10 +10,13 @@ # # -f, --fish fish simulation, equivalent to -l -s -t. # -l, --last Print the last directory's full name. -# -s, --short Truncate directory names to the first character. Without +# -s, --short Truncate directory names to the number of characters given by -#. Without # -s, names are truncated without making them ambiguous. # -t, --tilde Substitute ~ for the home directory. # -T, --nameddirs Substitute named directories as well. +# -# Truncate each directly to at least this many characters inclusive of the +# ellipsis character(s) (defaulting to 1). +# -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated. # # The long options can also be set via zstyle, like # zstyle :prompt:shrink_path fish yes @@ -37,6 +40,8 @@ shrink_path () { typeset -i short=0 typeset -i tilde=0 typeset -i named=0 + typeset -i length=1 + typeset ellipsis="" if zstyle -t ':prompt:shrink_path' fish; then lastfull=1 @@ -53,6 +58,10 @@ shrink_path () { while [[ $1 == -* ]]; do case $1 in + --) + shift + break + ;; -f|--fish) lastfull=1 short=1 @@ -62,9 +71,13 @@ shrink_path () { print 'Usage: shrink_path [-f -l -s -t] [directory]' print ' -f, --fish fish-simulation, like -l -s -t' print ' -l, --last Print the last directory''s full name' - print ' -s, --short Truncate directory names to the first character' + print ' -s, --short Truncate directory names to the number of characters given by -#. Without' + print ' -s, names are truncated without making them ambiguous.' print ' -t, --tilde Substitute ~ for the home directory' print ' -T, --nameddirs Substitute named directories as well' + print ' -# Truncate each directly to at least this many characters inclusive of the' + print ' ellipsis character(s) (defaulting to 1).' + print ' -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.' print 'The long options can also be set via zstyle, like' print ' zstyle :prompt:shrink_path fish yes' return 0 @@ -76,10 +89,18 @@ shrink_path () { tilde=1 named=1 ;; + -[0-9]|-[0-9][0-9]) + length=${1/-/} + ;; + -e) + shift + ellipsis="$1" + ;; esac shift done + typeset -i elllen=${#ellipsis} typeset -a tree expn typeset result part dir=${1-$PWD} typeset -i i @@ -109,12 +130,20 @@ shrink_path () { expn=(a b) part='' i=0 - until [[ (( ${#expn} == 1 )) || $dir = $expn || $i -gt 99 ]] do + until [[ $i -gt 99 || ( $i -ge $((length - ellen)) || $dir == $part ) && ( (( ${#expn} == 1 )) || $dir = $expn ) ]]; do (( i++ )) part+=$dir[$i] expn=($(echo ${part}*(-/))) - (( short )) && break + (( short )) && [[ $i -ge $((length - ellen)) ]] && break done + + typeset -i dif=$(( ${#dir} - ${#part} - ellen )) + if [[ $dif -gt 0 ]] + then + part+="$ellipsis" + else + part="$dir" + fi result+="/$part" cd -q $dir shift tree -- cgit v1.2.3-70-g09d2 From 2f345a3d0c9a94891c909a9ae6f6e2e2dc6df5b2 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Thu, 5 Mar 2020 13:08:05 +0100 Subject: 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 * Fix typo "Asterix" and replace with "asterisk" * Add optional quoting support to shrink_path Co-authored-by: Jacob Tomaw --- plugins/shrink-path/README.md | 19 +++++++++++++------ plugins/shrink-path/shrink-path.plugin.zsh | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'plugins/shrink-path/shrink-path.plugin.zsh') diff --git a/plugins/shrink-path/README.md b/plugins/shrink-path/README.md index 54bc351d3..33dc3f8ae 100644 --- a/plugins/shrink-path/README.md +++ b/plugins/shrink-path/README.md @@ -7,21 +7,26 @@ For this directory tree: ``` /home/ me/ - foo/ + f o o/ # The prefix f is ambiguous between "f o o" and "f i g". bar/ quux/ biz/ # The prefix b is ambiguous between bar and biz. + f i g/ + baz/ ``` here are the results of calling `shrink_path