1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-25 13:30:07 +00:00

Comments, trunc_to_last, trunc_absolute

Added comments throughout the code
Added `truncate_to_last` - displays only the current folder
Added `truncate_absolute` - displays only the last
POWERLEVEL9K_SHORTER_DIR_LENGTH characters - see #736
This commit is contained in:
Christo Kotze 2018-02-22 23:34:25 +04:00
parent 9ef8a2f1f9
commit 441fb277d9
2 changed files with 106 additions and 42 deletions

View file

@ -724,21 +724,39 @@ set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~"
set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false
prompt_dir() {
local current_path="$(print -P '%~')"
# save this path so that we can use the pure path for STATE icons and colors later.
local state_path=$current_path
# declare all local variables
local paths directory test_dir test_dir_length trunc_path threshhold
(( ${#current_path} > 1 )) && paths=(${(s:/:)${current_path//"~\/"/}}) || paths=() # only split if not root/home folder
if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then
# if we are not in "~" or "/", split the paths into an array
(( ${#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
set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026"
local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character
# convert delimiter from unicode to literal character, so that we can get the correct length later
local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER)
case "$POWERLEVEL9K_SHORTEN_STRATEGY" in
truncate_middle)
# truncate characters from the middle of the path
current_path=$(truncatePath $current_path $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER "middle")
;;
truncate_from_right)
# 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_with_folder_marker)
if (( ${#paths} > 0 )); then # root is an exception and won't have paths
if (( ${#paths} > 0 )); then # root and home are exceptions and won't have paths, so skip this
local last_marked_folder marked_folder
set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker"
@ -844,7 +862,8 @@ prompt_dir() {
esac
fi
local path_opt=$current_path # save state of path for highlighting and bold options
# save state of path for highlighting and bold options
local path_opt=$current_path
if [[ "${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then
current_path="${current_path[2,-1]}"
@ -860,14 +879,15 @@ prompt_dir() {
local current_state="DEFAULT"
if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then
current_state="NOT_WRITABLE"
elif [[ $current_path == '~' ]]; then
elif [[ $state_path == '~' ]]; then
current_state="HOME"
elif [[ $current_path == '~'* ]]; then
elif [[ $state_path == '~'* ]]; then
current_state="HOME_SUBFOLDER"
fi
# declare variables used for bold and state colors
local bld dir_state_foreground dir_state_user_foreground
[[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" == "true" ]] && bld="%B" || bld=""
[[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" == "true" ]] && bld_on="%B"; bld_off="%b" || bld_on=""; bld_off=""
local dir_state_user_foreground=POWERLEVEL9K_DIR_${current_state}_FOREGROUND
local dir_state_foreground=${(P)dir_state_user_foreground}
@ -879,15 +899,23 @@ prompt_dir() {
if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then
if [[ $path_opt == "/" || $path_opt == "~" || "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then
current_path="${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${current_path}%b"
current_path="${bld_on}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${current_path}${bld_off}"
else
current_path="${dir_name}/${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b"
if [[ $dir_name != $base_name ]]; then
current_path="${dir_name}/${bld_on}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}${bld_off}"
else
current_path="${bld_on}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}${bld_off}"
fi
fi
else
if [[ $path_opt == "/" || $path_opt == "~" || "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then
current_path="${bld}${current_path}%b"
current_path="${bld_on}${current_path}${bld_off}"
else
current_path="${dir_name}/${bld}${base_name}%b"
if [[ $dir_name != $base_name ]]; then
current_path="${dir_name}/${bld_on}${base_name}${bld_off}"
else
current_path="${bld_on}${base_name}${bld_off}"
fi
fi
fi

View file

@ -26,7 +26,7 @@ function testTruncateFoldersWorks() {
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}/12345678/123456789%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/12345678/123456789 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -44,7 +44,7 @@ function testTruncateMiddleWorks() {
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}/tmp/post/1/12/123/1234/1245/1256/1267/1278/123456789%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/tmp/post/1/12/123/1234/1245/1256/1267/1278/123456789 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -62,7 +62,43 @@ function testTruncationFromRightWorks() {
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}/tmp/po/1/12/123/12/12/12/12/12/123456789%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/tmp/po/1/12/123/12/12/12/12/12/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 testTruncateToLastWorks() {
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_STRATEGY="truncate_to_last"
FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}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_to_last"
FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}89 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -81,7 +117,7 @@ function testTruncationFromRightWithEmptyDelimiter() {
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}/tmp/po/1/12/123/12/12/12/12/12/123456789%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/tmp/po/1/12/123/12/12/12/12/12/123456789 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -102,7 +138,7 @@ function testTruncateWithFolderMarkerWorks() {
# Setup folder marker
touch $BASEFOLDER/1/12/.shorten_folder_marker
cd $FOLDER
assertEquals "%K{blue} %F{black}//12/123/1234/12345/123456/1234567%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}//12/123/1234/12345/123456/1234567 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr $BASEFOLDER
@ -123,7 +159,7 @@ function testTruncateWithFolderMarkerWithChangedFolderMarker() {
# Setup folder marker
touch $BASEFOLDER/1/12/.xxx
cd $FOLDER
assertEquals "%K{blue} %F{black}//12/123/1234/12345/123456/1234567%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}//12/123/1234/12345/123456/1234567 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr $BASEFOLDER
@ -156,7 +192,7 @@ function testTruncateWithPackageNameWorks() {
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_STRATEGY='truncate_with_package_name'
assertEquals "%K{blue} %F{black}My_Package/1/12/123/12/12/12/12/12/123456789%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}My_Package/1/12/123/12/12/12/12/12/123456789 %k%F{blue}%f " "$(build_left_prompt)"
# Go back
cd $p9kFolder
@ -195,7 +231,7 @@ function testTruncateWithPackageNameIfRepoIsSymlinkedInsideDeepFolder() {
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_STRATEGY='truncate_with_package_name'
assertEquals "%K{blue} %F{black}My_Package/as/qwerqwer%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}My_Package/as/qwerqwer %k%F{blue}%f " "$(build_left_prompt)"
# Go back
cd $p9kFolder
@ -230,7 +266,7 @@ function testTruncateWithPackageNameIfRepoIsSymlinkedInsideGitDir() {
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_STRATEGY='truncate_with_package_name'
assertEquals "%K{blue} %F{black}My_Package/.g/re/heads%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}My_Package/.g/re/heads %k%F{blue}%f " "$(build_left_prompt)"
# Go back
cd $p9kFolder
@ -244,7 +280,7 @@ function testHomeFolderDetectionWorks() {
POWERLEVEL9K_HOME_ICON='home-icon'
cd ~
assertEquals "%K{blue} %F{black%}home-icon%f %F{black}~%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black%}home-icon%f %F{black}~ %k%F{blue}%f " "$(build_left_prompt)"
cd -
unset POWERLEVEL9K_HOME_ICON
@ -256,7 +292,7 @@ function testHomeSubfolderDetectionWorks() {
FOLDER=~/powerlevel9k-test
mkdir $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black%}sub-icon%f %F{black}~/powerlevel9k-test%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black%}sub-icon%f %F{black}~/powerlevel9k-test %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr $FOLDER
@ -270,7 +306,7 @@ function testOtherFolderDetectionWorks() {
FOLDER=/tmp/powerlevel9k-test
mkdir $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}/tmp/powerlevel9k-test%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}/tmp/powerlevel9k-test %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr $FOLDER
@ -284,7 +320,7 @@ function testChangingDirPathSeparator() {
mkdir -p $FOLDER
cd $FOLDER
assertEquals "%K{blue} %F{black}xXxtmpxXxpowerlevel9k-testxXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}xXxtmpxXxpowerlevel9k-testxXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)"
cd -
unset FOLDER
@ -299,20 +335,20 @@ function testHomeFolderAbbreviation() {
cd ~/
# default
POWERLEVEL9K_HOME_FOLDER_ABBREVIATION='~'
assertEquals "%K{blue} %F{black}~%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}~ %k%F{blue}%f " "$(build_left_prompt)"
# substituted
POWERLEVEL9K_HOME_FOLDER_ABBREVIATION='qQq'
assertEquals "%K{blue} %F{black}qQq%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}qQq %k%F{blue}%f " "$(build_left_prompt)"
cd /tmp
# default
POWERLEVEL9K_HOME_FOLDER_ABBREVIATION='~'
assertEquals "%K{blue} %F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)"
# substituted
POWERLEVEL9K_HOME_FOLDER_ABBREVIATION='qQq'
assertEquals "%K{blue} %F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)"
cd "$dir"
}
@ -322,7 +358,7 @@ function testOmittingFirstCharacterWorks() {
POWERLEVEL9K_FOLDER_ICON='folder-icon'
cd /tmp
assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}tmp%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}tmp %k%F{blue}%f " "$(build_left_prompt)"
cd -
unset POWERLEVEL9K_FOLDER_ICON
@ -336,7 +372,7 @@ function testOmittingFirstCharacterWorksWithChangingPathSeparator() {
mkdir -p /tmp/powerlevel9k-test/1/2
cd /tmp/powerlevel9k-test/1/2
assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}tmpxXxpowerlevel9k-testxXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}tmpxXxpowerlevel9k-testxXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -360,7 +396,7 @@ function testOmittingFirstCharacterWorksWithChangingPathSeparatorAndDefaultTrunc
mkdir -p /tmp/powerlevel9k-test/1/2
cd /tmp/powerlevel9k-test/1/2
assertEquals "%K{blue} %F{black}xXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}xXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -378,7 +414,7 @@ function testOmittingFirstCharacterWorksWithChangingPathSeparatorAndMiddleTrunca
mkdir -p /tmp/powerlevel9k-test/1/2
cd /tmp/powerlevel9k-test/1/2
assertEquals "%K{blue} %F{black}tmpxXxpostxXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}tmpxXxpostxXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -396,7 +432,7 @@ function testOmittingFirstCharacterWorksWithChangingPathSeparatorAndRightTruncat
mkdir -p /tmp/powerlevel9k-test/1/2
cd /tmp/powerlevel9k-test/1/2
assertEquals "%K{blue} %F{black}tmpxXxpoxXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}tmpxXxpoxXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -417,7 +453,7 @@ function testTruncateToUniqueWorks() {
mkdir -p /tmp/powerlevel9k-test/bob/docs
cd /tmp/powerlevel9k-test/alice/devl
assertEquals "%K{blue} %F{black}txXxpxXxalxXxde%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}txXxpxXxalxXxde %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -485,7 +521,7 @@ function testHighlightHomeWorks() {
POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND='red'
cd ~
assertEquals "%K{blue} %F{black}%F{red}~%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}%F{red}~ %k%F{blue}%f " "$(build_left_prompt)"
cd -
unset POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND
@ -496,7 +532,7 @@ function testHighlightHomeSubdirWorks() {
mkdir -p ~/powerlevel9k-test
cd ~/powerlevel9k-test
assertEquals "%K{blue} %F{black}~/%F{red}powerlevel9k-test%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}~/%F{red}powerlevel9k-test %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr ~/powerlevel9k-test
@ -507,7 +543,7 @@ function testHighlightRootWorks() {
POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND='red'
cd /
assertEquals "%K{blue} %F{black}%F{red}/%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}%F{red}/ %k%F{blue}%f " "$(build_left_prompt)"
cd -
unset POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND
@ -517,7 +553,7 @@ function testHighlightRootSubdirWorks() {
POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND='red'
cd /tmp
assertEquals "%K{blue} %F{black}/%F{red}tmp%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/%F{red}tmp %k%F{blue}%f " "$(build_left_prompt)"
cd -
unset POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND
@ -528,7 +564,7 @@ function testHighlightRootSubSubdirWorks() {
mkdir /tmp/powerlevel9k-test
cd /tmp/powerlevel9k-test
assertEquals "%K{blue} %F{black}/tmp/%F{red}powerlevel9k-test%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}/tmp/%F{red}powerlevel9k-test %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test
@ -540,7 +576,7 @@ function testDirSeparatorColorHomeSubdirWorks() {
mkdir -p ~/powerlevel9k-test
cd ~/powerlevel9k-test
assertEquals "%K{blue} %F{black}~%F{red}/%F{black}powerlevel9k-test%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}~%F{red}/%F{black}powerlevel9k-test %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr ~/powerlevel9k-test
@ -552,7 +588,7 @@ function testDirSeparatorColorRootSubSubdirWorks() {
mkdir -p /tmp/powerlevel9k-test
cd /tmp/powerlevel9k-test
assertEquals "%K{blue} %F{black}%F{red}/%F{black}tmp%F{red}/%F{black}powerlevel9k-test%b %k%F{blue}%f " "$(build_left_prompt)"
assertEquals "%K{blue} %F{black}%F{red}/%F{black}tmp%F{red}/%F{black}powerlevel9k-test %k%F{blue}%f " "$(build_left_prompt)"
cd -
rm -fr /tmp/powerlevel9k-test