From 29c85be8d9a07cc46bbf764a2a9b1955f786b0d5 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Tue, 17 Jul 2018 12:17:21 +0400 Subject: [PATCH 1/3] Fix for "truncate_to_unique" strategy --- powerlevel9k.zsh-theme | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f3126b81..74c57d57 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -744,6 +744,38 @@ prompt_command_execution_time() { fi } +################################################################ +# Determine the home folder unique path - this is needed for +# truncate_to_unique to work when P9K_DIR_PATH_ABSOLUTE is not +# set the true. +# +local home_path="" +getUniqueHomeFolder() { + if [[ "$home_path" == "" ]]; then + local trunc_path directory test_dir test_dir_length + local -a matching + local -a paths + local cur_path='/' + # the first time we run the script, the working directory *should* be the home folder + [[ -z $HOME ]] && paths=${PWD} || paths=$HOME + paths=(${(s:/:)paths}) + 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 + home_path="${trunc_path: : -1}" + fi +} +getUniqueHomeFolder + ################################################################ # Dir: current working directory # Parameters: @@ -811,12 +843,13 @@ prompt_dir() { # 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 -a matching local cur_path='/' - [[ $current_path != "~"* ]] && trunc_path='/' || trunc_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 + for (( i=0; i < ${#directory}; i++ )); do test_dir+="${directory:$i:1}" matching=("$cur_path"/"$test_dir"*/) if [[ ${#matching[@]} -eq 1 ]]; then @@ -826,7 +859,8 @@ prompt_dir() { trunc_path+="$test_dir/" cur_path+="$directory/" done - [[ $current_path == "~"* ]] && trunc_path="~/$trunc_path" + # cheating here to retain ~ as home folder + [[ $current_path == "~"* ]] && trunc_path="~${trunc_path//${home_path}/}" || trunc_path="/${trunc_path}" current_path="${trunc_path: : -1}" fi ;; From 38c8519f1070e1fbce56b92417c9e096262887a7 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 18 Jul 2018 19:26:15 +0400 Subject: [PATCH 2/3] Changed getUniqueHomeFolder to use $HOME It is a Posix specification that $HOME be set, so we can use that. See http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html --- powerlevel9k.zsh-theme | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 74c57d57..8074bd29 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -756,9 +756,8 @@ getUniqueHomeFolder() { local -a matching local -a paths local cur_path='/' - # the first time we run the script, the working directory *should* be the home folder - [[ -z $HOME ]] && paths=${PWD} || paths=$HOME - paths=(${(s:/:)paths}) + # all users have the $HOME variable set automatically... see http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html + paths=(${(s:/:)HOME}) for directory in ${paths[@]}; do test_dir='' for (( i=0; i < ${#directory}; i++ )); do From dd25b5db1234ea2959b809b83990a7df493816d1 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 18 Jul 2018 19:46:31 +0400 Subject: [PATCH 3/3] truncate_to_unique - simplified the code --- powerlevel9k.zsh-theme | 67 +++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 8074bd29..f171df10 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -745,35 +745,29 @@ prompt_command_execution_time() { } ################################################################ -# Determine the home folder unique path - this is needed for -# truncate_to_unique to work when P9K_DIR_PATH_ABSOLUTE is not -# set the true. +# Determine the unique path - this is needed for the +# truncate_to_unique strategy. # -local home_path="" -getUniqueHomeFolder() { - if [[ "$home_path" == "" ]]; then - local trunc_path directory test_dir test_dir_length - local -a matching - local -a paths - local cur_path='/' - # all users have the $HOME variable set automatically... see http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html - paths=(${(s:/:)HOME}) - 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/" +function getUniqueFolder() { + local trunc_path directory test_dir test_dir_length + local -a matching + local -a paths + local cur_path='/' + paths=(${(s:/:)1}) + 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 - home_path="${trunc_path: : -1}" - fi + trunc_path+="$test_dir/" + cur_path+="$directory/" + done + echo "${trunc_path: : -1}" } -getUniqueHomeFolder ################################################################ # Dir: current working directory @@ -842,25 +836,10 @@ prompt_dir() { # 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 -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 - [[ $current_path == "~"* ]] && trunc_path="~${trunc_path//${home_path}/}" || trunc_path="/${trunc_path}" - current_path="${trunc_path: : -1}" + local home_path="$(getUniqueFolder $HOME)" + trunc_path="$(getUniqueFolder $PWD)" + [[ $current_path == "~"* ]] && current_path="~${trunc_path//${home_path}/}" || current_path="/${trunc_path}" fi ;; truncate_with_folder_marker)