1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-12-18 10:22:00 +00:00

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
For this directory tree:
```
/home/
me/
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/
/home/
me/
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 <option> /home/me/foo/bar/quux`:
```
Option Result
<none> /h/m/f o/ba/q
-l|--last /h/m/f o/ba/q
-s|--short /h/m/f/b/q
-t|--tilde ~/f o/ba/q
-f|--fish ~/f/b/quux
-g|--glob /h*/m*/f o*/ba*/q*
-3 /hom/me/f o/bar/quu
-e '$' -3 /hom$/me/f o$/bar/quu$
-q /h/m/f\ o/ba/q
-g -q /h*/m*/f\ o*/ba*/q*
Option Result
<none> /h/m/f o/ba/q
-l|--last /h/m/f o/ba/q
-s|--short /h/m/f/b/q
-t|--tilde ~/f o/ba/q
-f|--fish ~/f/b/quux
-g|--glob /h*/m*/f o*/ba*/q*
-3 /hom/me/f o/bar/quu
-e '$' -3 /hom$/me/f o$/bar/quu$
-q /h/m/f\ o/ba/q
-g -q /h*/m*/f\ o*/ba*/q*
-x /home/me/foo/bar/quux
```
## Usage
For a fish-style working directory in your command prompt, add the following to
your theme or zshrc:
```
setopt prompt_subst
PS1='%n@%m $(shrink_path -f)>'
```zsh
setopt prompt_subst
PS1='%n@%m $(shrink_path -f)>'
```
The following options are available:
@ -54,17 +61,45 @@ The following options are available:
ellipsis character(s) (defaulting to 1).
-e SYMBOL Postfix symbol(s) to indicate that a directory name had been truncated.
-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
```
zstyle :prompt:shrink_path fish yes
```zsh
zstyle :prompt:shrink_path fish yes
```
Note: Directory names containing two or more consecutive spaces are not yet
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
Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>

View file

@ -45,6 +45,7 @@ shrink_path () {
typeset -i length=1
typeset ellipsis=""
typeset -i quote=0
typeset -i expand=0
if zstyle -t ':prompt:shrink_path' fish; then
lastfull=1
@ -60,6 +61,7 @@ shrink_path () {
zstyle -t ':prompt:shrink_path' tilde && tilde=1
zstyle -t ':prompt:shrink_path' glob && ellipsis='*'
zstyle -t ':prompt:shrink_path' quote && quote=1
zstyle -t ':prompt:shrink_path' expand && expand=1
while [[ $1 == -* ]]; do
case $1 in
@ -85,6 +87,8 @@ shrink_path () {
print ' ellipsis character(s) (defaulting to 1).'
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 ' -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 ' zstyle :prompt:shrink_path fish yes'
return 0
@ -109,6 +113,9 @@ shrink_path () {
-q|--quote)
quote=1
;;
-x|--expand)
expand=1
;;
esac
shift
done
@ -120,6 +127,11 @@ shrink_path () {
[[ -d $dir ]] || return 0
if (( expand )) {
echo "$dir"
return 0
}
if (( named )) {
for part in ${(k)nameddirs}; {
[[ $dir == ${nameddirs[$part]}(/*|) ]] && dir=${dir/#${nameddirs[$part]}/\~$part}