From 1d2a6a3187e591350c5e213b93c06072b4e191ac Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 13 Nov 2018 02:17:03 +0100 Subject: [PATCH 1/2] Make all truncation stratiegies respect P9K_DIR_PATH_ABSOLUTE --- segments/dir.p9k | 10 +++++----- test/segments/dir.spec | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/segments/dir.p9k b/segments/dir.p9k index 026ce2a14..e671d8117 100644 --- a/segments/dir.p9k +++ b/segments/dir.p9k @@ -44,8 +44,8 @@ prompt_dir() { local current_path=${PWD} # WAS: local current_path="$(print -P '%~')" local pathPrefix [[ ${current_path} == "${HOME}"* ]] && pathPrefix="~/" || pathPrefix="/" - # check if the user wants to use absolute paths or "~" paths - [[ ${(L)P9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path//$HOME/"~"} + # Always rewrite current path to be prefixed with ~ if in home folder + current_path=${current_path//$HOME/"~"} # declare all local variables local directory test_dir test_dir_length trunc_path threshold # if we are not in "~" or "/", split the paths into an array and exclude "~" @@ -112,9 +112,6 @@ prompt_dir() { local markedFolder="${markedFolders[1]}" # We want to remove up to the matched parent folder markedFolder="${markedFolder:h}" - # TODO: Do not duplicate code from above, but this is necessary to truncate the path right. - # Also, this has to take P9K_DIR_PATH_ABSOLUTE into account.. - markedFolder=${markedFolder//$HOME/"~"} local shortenedFolder="${current_path#${markedFolder}*}" current_path="${pathPrefix}${P9K_DIR_SHORTEN_DELIMITER}${current_path#${markedFolder}*}" fi @@ -173,6 +170,9 @@ prompt_dir() { esac fi + # check if the user wants to use absolute paths or "~" paths + [[ ${(L)P9K_DIR_PATH_ABSOLUTE} == "true" ]] && current_path=${current_path//"~"/$HOME} + # save state of path for highlighting and bold options local path_opt=$current_path diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 07d68659e..37dba96af 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -27,8 +27,23 @@ function testDirPathAbsoluteWorks() { local P9K_DIR_PATH_ABSOLUTE=true cd ~ - assertEquals "%K{004} %F{000}${PWD} %k%F{004}%f " "$(__p9k_build_left_prompt)" + local absoluteDir="${PWD}" + assertEquals "%K{004} %F{000}${absoluteDir} %k%F{004}%f " "$(__p9k_build_left_prompt)" + local P9K_DIR_PATH_ABSOLUTE=false + assertEquals "%K{004} %F{000}~ %k%F{004}%f " "$(__p9k_build_left_prompt)" + + typeset -a _strategies + _strategies=( truncate_from_left truncate_from_right truncate_middle truncate_to_last truncate_to_first_and_last truncate_absolute truncate_to_unique truncate_with_folder_marker truncate_with_package_name ) + + for strategy in ${_strategies}; do + local P9K_DIR_PATH_ABSOLUTE=true + P9K_DIR_SHORTEN_STRATEGY=${strategy} + assertEquals "${strategy} failed rendering absolute dir" "%K{004} %F{000}${absoluteDir} %k%F{004}%f " "$(__p9k_build_left_prompt)" + + local P9K_DIR_PATH_ABSOLUTE=false + assertEquals "${strategy} failed rendering relative dir" "%K{004} %F{000}~ %k%F{004}%f " "$(__p9k_build_left_prompt)" + done cd - } From 21b3fb038f97da8ce2b775ccf297021eb58fac5d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 13 Nov 2018 03:47:55 +0100 Subject: [PATCH 2/2] Fix truncating with folder marker --- segments/dir.p9k | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/segments/dir.p9k b/segments/dir.p9k index e671d8117..6f92b791f 100644 --- a/segments/dir.p9k +++ b/segments/dir.p9k @@ -112,6 +112,10 @@ prompt_dir() { local markedFolder="${markedFolders[1]}" # We want to remove up to the matched parent folder markedFolder="${markedFolder:h}" + # Remove $HOME from it. This has to be done, as the truncated + # path may be prefixed by a tilde, and we cannot resolve the + # current path softly without symlinks. + markedFolder=${markedFolder//$HOME/"~"} local shortenedFolder="${current_path#${markedFolder}*}" current_path="${pathPrefix}${P9K_DIR_SHORTEN_DELIMITER}${current_path#${markedFolder}*}" fi