Merge pull request #398 from dritter/benoits_dir_segment

Add "truncate_with_folder_marker" shorten strategy
pull/22/head
Ben Hilburn 8 years ago committed by GitHub
commit 221030013e

@ -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

@ -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
}

@ -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
@ -592,7 +591,12 @@ prompt_dir() {
package_path=${$(pwd)%%/.git*}
fi
zero='%([BSUbfksu]|([FB]|){*})'
# Replace the shortest possible match of the marked folder from
# the current path. Remove the amount of characters up to the
# folder marker from the left. Count only the visible characters
# in the path (this is done by the "zero" pattern; see
# http://stackoverflow.com/a/40855342/5586433).
local zero='%([BSUbfksu]|([FB]|){*})'
current_dir=$(pwd)
# 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
@ -610,6 +614,32 @@ prompt_dir() {
current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" )
fi
;;
truncate_with_folder_marker)
local last_marked_folder marked_folder
set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker"
# Search for the folder marker in the parent directories and
# buildup a pattern that is removed from the current path
# later on.
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
# Replace the shortest possible match of the marked folder from
# the current path.
current_path=$current_path${PWD#${last_marked_folder}*}
;;
*)
current_path="%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c"
;;

@ -71,6 +71,48 @@ function testTruncationFromRightWorks() {
unset POWERLEVEL9K_SHORTEN_STRATEGY
}
function testTruncateWithFolderMarkerWorks() {
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
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_LEFT_PROMPT_ELEMENTS
}
function testTruncateWithFolderMarkerWithChangedFolderMarker() {
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
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_LEFT_PROMPT_ELEMENTS
}
function testHomeFolderDetectionWorks() {
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
POWERLEVEL9K_HOME_ICON='home-icon'

Loading…
Cancel
Save