summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md10
-rw-r--r--functions/utilities.zsh16
-rwxr-xr-xpowerlevel9k.zsh-theme25
-rwxr-xr-xtest/segments/dir.spec46
4 files changed, 94 insertions, 3 deletions
diff --git a/README.md b/README.md
index df653c45..82fe40ef 100644
--- a/README.md
+++ b/README.md
@@ -279,9 +279,17 @@ 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. You can also use `truncate_with_package_name` to use the `package.json` `name` field to abbreviate the directory path.|
+|`POWERLEVEL9K_SHORTEN_STRATEGY`|None|How the directory strings should be truncated. See the table below for more informations.|
|`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.|
+| Strategy Name | Description |
+|---------------|-------------|
+|Default|Truncate whole directories from left. How many is defined by `POWERLEVEL9K_SHORTEN_DIR_LENGTH`|
+|`truncate_middle`|Truncates the middle part of a folder. E.g. you are in a folder named "~/MySuperProjects/AwesomeFiles/BoringOffice", then it will truncated to "~/MyS..cts/Awe..les/BoringOffice", if `POWERLEVEL9K_SHORTEN_DIR_LENGTH=3` is also set (controls the amount of characters to be left).|
+|`truncate_from_right`|Just leaves the beginning of a folder name untouched. E.g. your folders will be truncated like so: "/ro../Pr../office". How many characters will be untouched is controlled by `POWERLEVEL9K_SHORTEN_DIR_LENGTH`.|
+|`truncate_with_package_name`|Use the `package.json` `name` field to abbreviate the directory path.|
+|`truncate_with_folder_marker`|Search for a file that is specified by `POWERLEVEL9K_SHORTEN_FOLDER_MARKER` and truncate everything before that (if found, otherwise stop on $HOME and ROOT).|
+
For example, if you wanted the truncation behavior of the `fish` shell, which
truncates `/usr/share/plasma` to `/u/s/plasma`, you would use the following:
```zsh
diff --git a/functions/utilities.zsh b/functions/utilities.zsh
index f27c7f99..22b53c6a 100644
--- a/functions/utilities.zsh
+++ b/functions/utilities.zsh
@@ -208,3 +208,19 @@ function truncatePathFromRight() {
echo $1 | sed $SED_EXTENDED_REGEX_PARAMETER \
"s@(([^/]{$((POWERLEVEL9K_SHORTEN_DIR_LENGTH))})([^/]{$delim_len}))[^/]+/@\2$POWERLEVEL9K_SHORTEN_DELIMITER/@g"
}
+
+# Search recursively in parent folders for given file.
+function upsearch () {
+ if [[ "$PWD" == "$HOME" || "$PWD" == "/" ]]; then
+ echo "$PWD"
+ elif test -e "$1"; then
+ pushd .. > /dev/null
+ upsearch "$1"
+ popd > /dev/null
+ echo "$PWD"
+ else
+ pushd .. > /dev/null
+ upsearch "$1"
+ popd > /dev/null
+ fi
+}
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme
index 7499197b..d51ec8f2 100755
--- a/powerlevel9k.zsh-theme
+++ b/powerlevel9k.zsh-theme
@@ -564,8 +564,7 @@ prompt_custom() {
set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/"
prompt_dir() {
local current_path='%~'
- if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" ]]; then
-
+ if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then
set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\U2026'
case "$POWERLEVEL9K_SHORTEN_STRATEGY" in
@@ -610,6 +609,28 @@ prompt_dir() {
current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" )
fi
;;
+ truncate_with_folder_marker)
+ local last_marked_folder marked_folder zero
+ set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker"
+
+ for marked_folder in $(upsearch $POWERLEVEL9K_SHORTEN_FOLDER_MARKER); do
+ if [[ "$marked_folder" == "/" ]]; then
+ # If we reached root folder, stop upsearch.
+ current_path="/"
+ elif [[ "$marked_folder" == "$HOME" ]]; then
+ # If we reached home folder, stop upsearch.
+ current_path="~"
+ elif [[ "${marked_folder%/*}" == $last_marked_folder ]]; then
+ current_path="${current_path%/}/${marked_folder##*/}"
+ else
+ current_path="${current_path%/}/$POWERLEVEL9K_SHORTEN_DELIMITER/${marked_folder##*/}"
+ fi
+ last_marked_folder=$marked_folder
+ done
+
+ zero='%([BSUbfksu]|([FB]|){*})'
+ current_path=$current_path${PWD:${#${(S%%)last_marked_folder//$~zero/}}}
+ ;;
*)
current_path="%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c"
;;
diff --git a/test/segments/dir.spec b/test/segments/dir.spec
index 840a298b..0f253ac3 100755
--- a/test/segments/dir.spec
+++ b/test/segments/dir.spec
@@ -71,6 +71,52 @@ function testTruncationFromRightWorks() {
unset POWERLEVEL9K_SHORTEN_STRATEGY
}
+function testTruncateWithFolderMarkerWorks() {
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
+ POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
+ POWERLEVEL9K_SHORTEN_STRATEGY="truncate_with_folder_marker"
+
+ local BASEFOLDER=/tmp/powerlevel9k-test
+ local FOLDER=$BASEFOLDER/1/12/123/1234/12345/123456/1234567
+ mkdir -p $FOLDER
+ # Setup folder marker
+ touch $BASEFOLDER/1/12/.shorten_folder_marker
+ cd $FOLDER
+ assertEquals "%K{blue} %F{black}/…/12/123/1234/12345/123456/1234567 %k%F{blue}%f " "$(build_left_prompt)"
+
+ cd -
+ rm -fr $BASEFOLDER
+ unset BASEFOLDER
+ unset FOLDER
+ unset POWERLEVEL9K_SHORTEN_STRATEGY
+ unset POWERLEVEL9K_SHORTEN_DIR_LENGTH
+ unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+}
+
+function testTruncateWithFolderMarkerWithChangedFolderMarker() {
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
+ POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
+ POWERLEVEL9K_SHORTEN_STRATEGY="truncate_with_folder_marker"
+ POWERLEVEL9K_SHORTEN_FOLDER_MARKER='.xxx'
+
+ local BASEFOLDER=/tmp/powerlevel9k-test
+ local FOLDER=$BASEFOLDER/1/12/123/1234/12345/123456/1234567
+ mkdir -p $FOLDER
+ # Setup folder marker
+ touch $BASEFOLDER/1/12/.xxx
+ cd $FOLDER
+ assertEquals "%K{blue} %F{black}/…/12/123/1234/12345/123456/1234567 %k%F{blue}%f " "$(build_left_prompt)"
+
+ cd -
+ rm -fr $BASEFOLDER
+ unset BASEFOLDER
+ unset FOLDER
+ unset POWERLEVEL9K_SHORTEN_FOLDER_MARKER
+ unset POWERLEVEL9K_SHORTEN_STRATEGY
+ unset POWERLEVEL9K_SHORTEN_DIR_LENGTH
+ unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+}
+
function testHomeFolderDetectionWorks() {
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
POWERLEVEL9K_HOME_ICON='home-icon'