feat(shrink-path): add ability to toggle off path shrinking (#9794)

This commit is contained in:
Marc Cornellà 2021-05-21 21:57:34 +02:00 committed by GitHub
parent d646884add
commit 1bda62dffa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 26 deletions

View file

@ -1,43 +1,50 @@
# A plugin to shrink directory paths for brevity and pretty-printing # shrink-path
A plugin to shrink directory paths for brevity and pretty-printing.
To use it, add `shrink-path` to the plugins array in your zshrc file:
```zsh
plugins=(... shrink-path)
```
## Examples ## Examples
For this directory tree: For this directory tree:
``` ```
/home/ /home/
me/ me/
f o o/ # The prefix f is ambiguous between "f o o" and "f i g". f o o/ # The prefix f is ambiguous between "f o o" and "f i g".
bar/ bar/
quux/ quux/
biz/ # The prefix b is ambiguous between bar and biz. biz/ # The prefix b is ambiguous between bar and biz.
f i g/ f i g/
baz/ baz/
``` ```
here are the results of calling `shrink_path <option> /home/me/foo/bar/quux`: here are the results of calling `shrink_path <option> /home/me/foo/bar/quux`:
``` ```
Option Result Option Result
<none> /h/m/f o/ba/q <none> /h/m/f o/ba/q
-l|--last /h/m/f o/ba/q -l|--last /h/m/f o/ba/q
-s|--short /h/m/f/b/q -s|--short /h/m/f/b/q
-t|--tilde ~/f o/ba/q -t|--tilde ~/f o/ba/q
-f|--fish ~/f/b/quux -f|--fish ~/f/b/quux
-g|--glob /h*/m*/f o*/ba*/q* -g|--glob /h*/m*/f o*/ba*/q*
-3 /hom/me/f o/bar/quu -3 /hom/me/f o/bar/quu
-e '$' -3 /hom$/me/f o$/bar/quu$ -e '$' -3 /hom$/me/f o$/bar/quu$
-q /h/m/f\ o/ba/q -q /h/m/f\ o/ba/q
-g -q /h*/m*/f\ o*/ba*/q* -g -q /h*/m*/f\ o*/ba*/q*
-x /home/me/foo/bar/quux
``` ```
## Usage ## Usage
For a fish-style working directory in your command prompt, add the following to For a fish-style working directory in your command prompt, add the following to
your theme or zshrc: your theme or zshrc:
``` ```zsh
setopt prompt_subst setopt prompt_subst
PS1='%n@%m $(shrink_path -f)>' PS1='%n@%m $(shrink_path -f)>'
``` ```
The following options are available: The following options are available:
@ -54,17 +61,45 @@ The following options are available:
ellipsis character(s) (defaulting to 1). ellipsis character(s) (defaulting to 1).
-e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated. -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.
-q, --quote Quote special characters in the shrunk path -q, --quote Quote special characters in the shrunk path
-x, --expand Print the full path. This takes precedence over the other options
``` ```
The long options can also be set via zstyle, like The long options can also be set via zstyle, like
``` ```zsh
zstyle :prompt:shrink_path fish yes zstyle :prompt:shrink_path fish yes
``` ```
Note: Directory names containing two or more consecutive spaces are not yet Note: Directory names containing two or more consecutive spaces are not yet
supported. supported.
## Trick: toggle shrinking with a keyboard shortcut
You can use the `expand` option to disable the path shrinking. You can combine that
with a key binding widget to toggle path shrinking on and off.
```zsh
# Toggle off path shrinking
zstyle ':prompt:shrink_path' expand true
# Toggle on path shrinking
zstyle -d ':prompt:shrink_path' expand
```
Combined with a widget:
```zsh
# Widget definition
shrink-path-toggle() {
zstyle -t ':prompt:shrink_path' expand \
&& zstyle -d ':prompt:shrink_path' expand \
|| zstyle ':prompt:shrink_path' expand true
zle reset-prompt
}
zle -N shrink-path-toggle
# Key binding to ALT+SHIFT+S
bindkey "^[S" shrink-path-toggle
```
## License ## License
Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx> Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>

View file

@ -45,6 +45,7 @@ shrink_path () {
typeset -i length=1 typeset -i length=1
typeset ellipsis="" typeset ellipsis=""
typeset -i quote=0 typeset -i quote=0
typeset -i expand=0
if zstyle -t ':prompt:shrink_path' fish; then if zstyle -t ':prompt:shrink_path' fish; then
lastfull=1 lastfull=1
@ -60,6 +61,7 @@ shrink_path () {
zstyle -t ':prompt:shrink_path' tilde && tilde=1 zstyle -t ':prompt:shrink_path' tilde && tilde=1
zstyle -t ':prompt:shrink_path' glob && ellipsis='*' zstyle -t ':prompt:shrink_path' glob && ellipsis='*'
zstyle -t ':prompt:shrink_path' quote && quote=1 zstyle -t ':prompt:shrink_path' quote && quote=1
zstyle -t ':prompt:shrink_path' expand && expand=1
while [[ $1 == -* ]]; do while [[ $1 == -* ]]; do
case $1 in case $1 in
@ -85,6 +87,8 @@ shrink_path () {
print ' ellipsis character(s) (defaulting to 1).' print ' ellipsis character(s) (defaulting to 1).'
print ' -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.' print ' -e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.'
print ' -q, --quote Quote special characters in the shrunk path' print ' -q, --quote Quote special characters in the shrunk path'
print ' -x, --expand Print the full path. This takes precedence over the other options'
print ''
print 'The long options can also be set via zstyle, like' print 'The long options can also be set via zstyle, like'
print ' zstyle :prompt:shrink_path fish yes' print ' zstyle :prompt:shrink_path fish yes'
return 0 return 0
@ -109,6 +113,9 @@ shrink_path () {
-q|--quote) -q|--quote)
quote=1 quote=1
;; ;;
-x|--expand)
expand=1
;;
esac esac
shift shift
done done
@ -120,6 +127,11 @@ shrink_path () {
[[ -d $dir ]] || return 0 [[ -d $dir ]] || return 0
if (( expand )) {
echo "$dir"
return 0
}
if (( named )) { if (( named )) {
for part in ${(k)nameddirs}; { for part in ${(k)nameddirs}; {
[[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part} [[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part}