diff --git a/README.md b/README.md index d46a8dea..43170580 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,7 @@ Customizations available are: | Variable | Default Value | Description | |----------|---------------|-------------| |`POWERLEVEL9K_SHORTEN_DIR_LENGTH`|`2`|If your shorten strategy, below, is entire directories, this field determines how many directories to leave at the end. If your shorten strategy is by character count, this field determines how many characters to allow per directory string.| -|`POWERLEVEL9K_SHORTEN_STRATEGY`|None|How the directory strings should be truncated. By default, it will truncate whole directories. Other options are `truncate_middle`, which leaves the start and end of the directory strings, and `truncate_from_right`, which cuts starting from the end of the string.| +|`POWERLEVEL9K_SHORTEN_STRATEGY`|None|How the directory strings should be truncated. By default, it will truncate whole directories. Other options are `truncate_middle`, which leaves the start and end of the directory strings, and `truncate_from_right`, which cuts starting from the end of the string. You can also use `truncate_with_package_name` to use the `package.json` `name` field to abbreviate the directory path.| |`POWERLEVEL9K_SHORTEN_DELIMITER`|`..`|Delimiter to use in truncated strings. This can be any string you choose, including an empty string if you wish to have no delimiter.| For example, if you wanted the truncation behavior of the `fish` shell, which @@ -256,6 +256,15 @@ In each case you have to specify the length you want to shorten the directory to. So in some cases `POWERLEVEL9K_SHORTEN_DIR_LENGTH` means characters, in others whole directories. +The `truncate_with_package_name` strategy gives your directory path relative to the root of your project. For example, if you have a project inside `$HOME/projects/my-project` with a `package.json` that looks like: + +```json +{ + "name": "my-cool-project" +} +``` + +the path shown would be `my-cool-project`. If you navigate to `$HOME/projects/my-project/src`, then the path shown would be `my-cool-project/src`. Please note that this currently looks for `.git` directory to determine the root of the project. ##### ip diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 5ca5b431..eed6ccc5 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -186,3 +186,9 @@ function segmentShouldBeJoined() { return 1 fi } + +# Given a directory path, truncate it according to the settings for +# `truncate_from_right` +function truncatePathFromRight() { + echo $1 | sed $SED_EXTENDED_REGEX_PARAMETER "s/([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})[^/]+\//\1$POWERLEVEL9K_SHORTEN_DELIMITER\//g" +} diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 89b38e48..67406aca 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -444,7 +444,7 @@ prompt_dir() { current_path=$(pwd | sed -e "s,^$HOME,~," | sed $SED_EXTENDED_REGEX_PARAMETER "s/([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})[^/]+([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})\//\1$POWERLEVEL9K_SHORTEN_DELIMITER\2\//g") ;; truncate_from_right) - current_path=$(pwd | sed -e "s,^$HOME,~," | sed $SED_EXTENDED_REGEX_PARAMETER "s/([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})[^/]+\//\1$POWERLEVEL9K_SHORTEN_DELIMITER\//g") + current_path=$(truncatePathFromRight $(pwd | sed -e "s,^$HOME,~,") ) ;; truncate_with_package_name) local name repo_path package_path current_dir zero @@ -466,7 +466,7 @@ prompt_dir() { # Then, find the length of the package_path string, and save the # subdirectory path as a substring of the current directory's path from 0 # to the length of the package path's string - subdirectory_path="/${current_dir:${#${(S%%)package_path//$~zero/}}}" + subdirectory_path=$(truncatePathFromRight "/${current_dir:${#${(S%%)package_path//$~zero/}}}") fi fi @@ -479,7 +479,7 @@ prompt_dir() { # from the package.json and append the current subdirectory current_path="`echo $name | tr -d '"'`$subdirectory_path" else - current_path='%~' + current_path=$(truncatePathFromRight $(pwd | sed -e "s,^$HOME,~,") ) fi ;; *)