Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1063 from dritter/fix_dir_absolute
Browse files Browse the repository at this point in the history
Make all truncation stratiegies respect P9K_DIR_PATH_ABSOLUTE
  • Loading branch information
dritter authored Nov 15, 2018
2 parents 98b1c06 + 21b3fb0 commit da65a22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 8 additions & 4 deletions segments/dir.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -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 "~"
Expand Down Expand Up @@ -112,8 +112,9 @@ 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..
# 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}*}"
Expand Down Expand Up @@ -163,6 +164,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

Expand Down
17 changes: 16 additions & 1 deletion test/segments/dir.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
}

Expand Down

0 comments on commit da65a22

Please sign in to comment.