1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-16 09:50:06 +00:00

Added truncate_to_first_and_last strategy

Truncate middle directories from the path. How many directories will be
untouched is controlled by POWERLEVEL9K_SHORTER_DIR_LENGTH.
This commit is contained in:
Christo Kotze 2018-02-23 09:28:56 +04:00
parent 01f7261df4
commit 08432bf70c
3 changed files with 63 additions and 29 deletions

View file

@ -377,8 +377,9 @@ Customizations available are:
|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_to_last`|Truncates everything before the last folder in the path.|
|`truncate_absolute`|Truncates everything exept the last few characters in the path. E.g. if you are in a folder named "~/Projects/powerlevel9k" and you have set `POWERLEVEL9K_SHORTEN_DIR_LENGTH=3`, you will get "..l9k".|
|`truncate_to_last`|Truncates everything before the last folder in the path.|
|`truncate_to_first_and_last|Truncate middle directories from the path. How many directories will be untouched is controlled by POWERLEVEL9K_SHORTER_DIR_LENGTH. E.g. if you are in a folder named "~/Projects/powerlevel9k" and you have set `POWERLEVEL9K_SHORTEN_DIR_LENGTH=1`, you will get "~/../powerlevel9k".||
|`truncate_to_unique`|Parse all parent path components and truncate them to the shortest unique length. If you copy & paste the result to a shell, after hitting `TAB` it should expand to the original path unambiguously.|
|`truncate_with_package_name`|Search for a `package.json` or `composer.json` and prints the `name` field to abbreviate the directory path. The precedence and/or files could be set by `POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json)`. If you have [jq](https://stedolan.github.io/jq/) installed, it will dramatically improve the speed of this strategy.|
|`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).|

View file

@ -728,7 +728,7 @@ prompt_dir() {
local state_path=$current_path
# declare all local variables
local paths directory test_dir test_dir_length trunc_path threshhold
# if we are not in "~" or "/", split the paths into an array
# if we are not in "~" or "/", split the paths into an array and exclude "~"
(( ${#current_path} > 1 )) && paths=(${(s:/:)${current_path//"~\/"/}}) || paths=()
# only run the code if SHORTEN_DIR_LENGTH is set, or we are using the two strategies that don't rely on it.
if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_to_last" ]]; then
@ -745,16 +745,54 @@ prompt_dir() {
# truncate characters from the right of the path
current_path=$(truncatePath "$current_path" $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER)
;;
truncate_to_last)
# truncate all characters before the current directory
current_path=${current_path##*/}
;;
truncate_absolute)
# truncate all characters except the last POWERLEVEL9K_SHORTEN_DIR_LENGTH characters
if [ ${#current_path} -gt $(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#POWERLEVEL9K_SHORTEN_DELIMITER} )) ]; then
current_path=$POWERLEVEL9K_SHORTEN_DELIMITER${current_path:(-POWERLEVEL9K_SHORTEN_DIR_LENGTH)}
fi
;;
truncate_to_last)
# truncate all characters before the current directory
current_path=${current_path##*/}
;;
truncate_to_first_and_last)
if (( ${#current_path} > 1 )) && (( ${POWERLEVEL9K_SHORTEN_DIR_LENGTH} > 0 )); then
threshhold=$(( ${POWERLEVEL9K_SHORTEN_DIR_LENGTH} * 2))
# if we are in "~", add it back into the paths array
[[ $current_path == '~'* ]] && paths=("~" "${paths[@]}")
if (( ${#paths} > $threshhold )); then
local num=$(( ${#paths} - ${POWERLEVEL9K_SHORTEN_DIR_LENGTH} ))
# repace the middle elements
for (( i=$POWERLEVEL9K_SHORTEN_DIR_LENGTH; i<$num; i++ )); do
paths[$i+1]=$POWERLEVEL9K_SHORTEN_DELIMITER
done
current_path="${(j:/:)paths}"
fi
fi
;;
truncate_to_unique)
# for each parent path component find the shortest unique beginning
# characters sequence. Source: https://stackoverflow.com/a/45336078
if (( ${#current_path} > 1 )); then # root and home are exceptions and won't have paths
local matching
local cur_path='/'
[[ $current_path != "~"* ]] && trunc_path='/' || trunc_path=''
for directory in ${paths[@]}; do
test_dir=''
for (( i=0; i<${#directory}; i++ )); do
test_dir+="${directory:$i:1}"
matching=("$cur_path"/"$test_dir"*/)
if [[ ${#matching[@]} -eq 1 ]]; then
break
fi
done
trunc_path+="$test_dir/"
cur_path+="$directory/"
done
[[ $current_path == "~"* ]] && trunc_path="~/$trunc_path"
current_path="${trunc_path: : -1}"
fi
;;
truncate_with_folder_marker)
if (( ${#paths} > 0 )); then # root and home are exceptions and won't have paths, so skip this
local last_marked_folder marked_folder
@ -833,29 +871,6 @@ prompt_dir() {
current_path="`echo $packageName | tr -d '"'`$subdirectory_path"
fi
;;
truncate_to_unique)
# for each parent path component find the shortest unique beginning
# characters sequence. Source: https://stackoverflow.com/a/45336078
if (( ${#current_path} > 1 )); then # root and home are exceptions and won't have paths
local matching
local cur_path='/'
[[ $current_path != "~"* ]] && trunc_path='/' || trunc_path=''
for directory in ${paths[@]}; do
test_dir=''
for (( i=0; i<${#directory}; i++ )); do
test_dir+="${directory:$i:1}"
matching=("$cur_path"/"$test_dir"*/)
if [[ ${#matching[@]} -eq 1 ]]; then
break
fi
done
trunc_path+="$test_dir/"
cur_path+="$directory/"
done
[[ $current_path == "~"* ]] && trunc_path="~/$trunc_path"
current_path="${trunc_path: : -1}"
fi
;;
*)
current_path="$(print -P "%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c")"
;;

View file

@ -90,6 +90,24 @@ function testTruncateToLastWorks() {
unset POWERLEVEL9K_SHORTEN_STRATEGY
}
function testTruncateToFirstAndLastWorks() {
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_STRATEGY="truncate_to_first_and_last"
FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}/tmp/12//12345678/123456789 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
unset FOLDER
unset POWERLEVEL9K_SHORTEN_DIR_LENGTH
unset POWERLEVEL9K_SHORTEN_STRATEGY
}
function testTruncateAbsoluteWorks() {
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_STRATEGY="truncate_absolute"