1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-12-23 15:41:57 +00:00

truncate_to_unique - simplified the code

This commit is contained in:
Christo Kotze 2018-07-18 19:46:31 +04:00
parent 38c8519f10
commit dd25b5db12

View file

@ -745,19 +745,15 @@ prompt_command_execution_time() {
} }
################################################################ ################################################################
# Determine the home folder unique path - this is needed for # Determine the unique path - this is needed for the
# truncate_to_unique to work when P9K_DIR_PATH_ABSOLUTE is not # truncate_to_unique strategy.
# set the true.
# #
local home_path="" function getUniqueFolder() {
getUniqueHomeFolder() {
if [[ "$home_path" == "" ]]; then
local trunc_path directory test_dir test_dir_length local trunc_path directory test_dir test_dir_length
local -a matching local -a matching
local -a paths local -a paths
local cur_path='/' local cur_path='/'
# all users have the $HOME variable set automatically... see http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html paths=(${(s:/:)1})
paths=(${(s:/:)HOME})
for directory in ${paths[@]}; do for directory in ${paths[@]}; do
test_dir='' test_dir=''
for (( i=0; i < ${#directory}; i++ )); do for (( i=0; i < ${#directory}; i++ )); do
@ -770,10 +766,8 @@ getUniqueHomeFolder() {
trunc_path+="$test_dir/" trunc_path+="$test_dir/"
cur_path+="$directory/" cur_path+="$directory/"
done done
home_path="${trunc_path: : -1}" echo "${trunc_path: : -1}"
fi
} }
getUniqueHomeFolder
################################################################ ################################################################
# Dir: current working directory # Dir: current working directory
@ -842,25 +836,10 @@ prompt_dir() {
# for each parent path component find the shortest unique beginning # for each parent path component find the shortest unique beginning
# characters sequence. Source: https://stackoverflow.com/a/45336078 # characters sequence. Source: https://stackoverflow.com/a/45336078
if (( ${#current_path} > 1 )); then # root and home are exceptions and won't have paths if (( ${#current_path} > 1 )); then # root and home are exceptions and won't have paths
local -a matching
local cur_path='/'
# we need to use absolute paths for this strategy to work correctly
paths=(${(s:/:)PWD})
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
# cheating here to retain ~ as home folder # cheating here to retain ~ as home folder
[[ $current_path == "~"* ]] && trunc_path="~${trunc_path//${home_path}/}" || trunc_path="/${trunc_path}" local home_path="$(getUniqueFolder $HOME)"
current_path="${trunc_path: : -1}" trunc_path="$(getUniqueFolder $PWD)"
[[ $current_path == "~"* ]] && current_path="~${trunc_path//${home_path}/}" || current_path="/${trunc_path}"
fi fi
;; ;;
truncate_with_folder_marker) truncate_with_folder_marker)