From 085a78b9b823ff1731070b229a9154bd4ade63b9 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 10 Feb 2018 13:54:05 +0400 Subject: Incorporated @dritter 's changes into next These are the changes made by @dritter on `async_all_the_segments` --- powerlevel9k.zsh-theme | 47 +++++++++-------------------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c32ccd48..26936325 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -718,21 +718,20 @@ prompt_command_execution_time() { # Dir: current working directory set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" -set_default POWERLEVEL9K_DIR_SHOW_WRITABLE false +# Parameters: +# * $1 Alignment: string - left|right +# * $2 Index: integer prompt_dir() { - local tmp="$IFS" - local IFS="" - local current_path=$(pwd | sed -e "s,^$HOME,~,") - local IFS="$tmp" + local current_path="$(print -P "%~")" if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\U2026' case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) - current_path=$(echo "$current_path" | sed $SED_EXTENDED_REGEX_PARAMETER "s/([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})[^/]+([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})\//\1$POWERLEVEL9K_SHORTEN_DELIMITER\2\//g") + current_path=$(pwd | sed -e "s,^$HOME,~," | sed $SED_EXTENDED_REGEX_PARAMETER "s/([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})[^/]+([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})\//\1$POWERLEVEL9K_SHORTEN_DELIMITER\2\//g") ;; truncate_from_right) - current_path=$(truncatePathFromRight "$current_path" ) + current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) ;; truncate_with_package_name) local name repo_path package_path current_dir zero @@ -782,12 +781,8 @@ prompt_dir() { # Instead of printing out the full path, print out the name of the package # from the package.json and append the current subdirectory current_path="`echo $packageName | tr -d '"'`$subdirectory_path" - if [[ "${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then - # add space before the packageName to allow for removing the "first" character, without messing up the package name. - current_path=" ${current_path}" - fi else - current_path=$(truncatePathFromRight "$current_path" ) + current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) fi ;; truncate_with_folder_marker) @@ -816,27 +811,6 @@ prompt_dir() { # the current path. current_path=$current_path${PWD#${last_marked_folder}*} ;; - truncate_to_unique) - # for each parent path component find the shortest unique beginning - # characters sequence. Source: https://stackoverflow.com/a/45336078 - paths=(${(s:/:)PWD}) - cur_path='/' - cur_short_path='/' - for directory in ${paths[@]} - do - cur_dir='' - for (( i=0; i<${#directory}; i++ )); do - cur_dir+="${directory:$i:1}" - matching=("$cur_path"/"$cur_dir"*/) - if [[ ${#matching[@]} -eq 1 ]]; then - break - fi - done - cur_short_path+="$cur_dir/" - cur_path+="$directory/" - done - current_path="${cur_short_path: : -1}" - ;; *) current_path="$(print -P "%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c")" ;; @@ -852,7 +826,7 @@ prompt_dir() { fi if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" ]]; then - current_path=${current_path/#\~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}} + current_path="$( echo "${current_path}" | sed "s/^~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}/")" fi typeset -AH dir_states @@ -860,12 +834,9 @@ prompt_dir() { "DEFAULT" "FOLDER_ICON" "HOME" "HOME_ICON" "HOME_SUBFOLDER" "HOME_SUB_ICON" - "NOT_WRITABLE" "LOCK_ICON" ) local current_state="DEFAULT" - if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then - current_state="NOT_WRITABLE" - elif [[ $(print -P "%~") == '~' ]]; then + if [[ $(print -P "%~") == '~' ]]; then current_state="HOME" elif [[ $(print -P "%~") == '~'* ]]; then current_state="HOME_SUBFOLDER" -- cgit v1.2.3 From 6d25e18779f33cd3339ecd491b3a195a714519ae Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 10 Feb 2018 14:23:27 +0400 Subject: Merged `truncate_to_unique` & state `NOT_WRITABLE` Added `truncate_to_unique` and `NOT_WRITABLE` to @dritter's code. --- powerlevel9k.zsh-theme | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 26936325..87b453a4 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -811,6 +811,27 @@ prompt_dir() { # the current path. current_path=$current_path${PWD#${last_marked_folder}*} ;; + truncate_to_unique) + # for each parent path component find the shortest unique beginning + # characters sequence. Source: https://stackoverflow.com/a/45336078 + paths=(${(s:/:)PWD}) + cur_path='/' + cur_short_path='/' + for directory in ${paths[@]} + do + cur_dir='' + for (( i=0; i<${#directory}; i++ )); do + cur_dir+="${directory:$i:1}" + matching=("$cur_path"/"$cur_dir"*/) + if [[ ${#matching[@]} -eq 1 ]]; then + break + fi + done + cur_short_path+="$cur_dir/" + cur_path+="$directory/" + done + current_path="${cur_short_path: : -1}" + ;; *) current_path="$(print -P "%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c")" ;; @@ -834,9 +855,12 @@ prompt_dir() { "DEFAULT" "FOLDER_ICON" "HOME" "HOME_ICON" "HOME_SUBFOLDER" "HOME_SUB_ICON" + "NOT_WRITABLE" "LOCK_ICON" ) local current_state="DEFAULT" - if [[ $(print -P "%~") == '~' ]]; then + if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then + current_state="NOT_WRITABLE" + elif [[ $(print -P "%~") == '~' ]]; then current_state="HOME" elif [[ $(print -P "%~") == '~'* ]]; then current_state="HOME_SUBFOLDER" -- cgit v1.2.3 From ccba1cc8233143f7e360a3ab3ff61b7175327acf Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 10 Feb 2018 14:46:56 +0400 Subject: PROMPT_DIR color/bold on last dir & color for sep. You can now set the last dir to have a custom color with `POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND` and whether it will be bold with `POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD=true`. You can also set a custom separator color with `POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND`. --- README.md | 4 ++++ powerlevel9k.zsh-theme | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/README.md b/README.md index 74edc446..7f9ee9ca 100644 --- a/README.md +++ b/README.md @@ -421,6 +421,10 @@ You can also configure the `dir` segment to show when you are in a directory wit |----------|---------------|-------------| |`POWERLEVEL9K_DIR_SHOW_WRITABLE`|`false`|If set to `true` and you are in a directory that you do not have write permissions for, this segment will display a lock icon and enter the `NOT_WRITABLE` state (which can be customized per [our usual process](https://github.com/bhilburn/powerlevel9k/wiki/Stylizing-Your-Prompt#segment-color-customization)). Note that this functionality is also available in a separate segment, `dir_writable`.| +If you want to customize the last directory of the path, you can now set `POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND` to a custom color and/or `POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD=true` to display that part in bold. + +You can also color the separator separately by setting the color using `POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND`. + ##### disk_usage The `disk_usage` segment will show the usage level of the partition that your current working directory resides in. It can be configured with the following variables. diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 87b453a4..faaa32ec 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -838,18 +838,12 @@ prompt_dir() { esac fi + local path_opt=$current_path # save state of path for highlighting and bold options + if [[ "${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then current_path="${current_path[2,-1]}" fi - if [[ "${POWERLEVEL9K_DIR_PATH_SEPARATOR}" != "/" ]]; then - current_path="$( echo "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g")" - fi - - if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" ]]; then - current_path="$( echo "${current_path}" | sed "s/^~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}/")" - fi - typeset -AH dir_states dir_states=( "DEFAULT" "FOLDER_ICON" @@ -865,6 +859,34 @@ prompt_dir() { elif [[ $(print -P "%~") == '~'* ]]; then current_state="HOME_SUBFOLDER" fi + + local bd dir_state_foreground dir_state_user_foreground + [[ -n "${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" ]] && bd="%B" || bd="" + + local dir_state_user_foreground="POWERLEVEL9K_DIR_${current_state}_FOREGROUND" + local dir_state_foreground="${(P)dir_state_user_foreground}" + [[ -z "${dir_state_foreground}" ]] && dir_state_foreground="${DEFAULT_COLOR}" + + if [[ -n "${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}" ]]; then + if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + current_path="$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}" + else + current_path="$(dirname $current_path)/$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}" + fi + fi + + if [[ -n "${POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND}" && $path_opt != "/" ]]; then + current_path="$( echo "${current_path}" | sed "s/\//%F{$POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND}\/%F{$dir_state_foreground}/g")" + fi + + if [[ "${POWERLEVEL9K_DIR_PATH_SEPARATOR}" != "/" && $path_opt != "/" ]]; then + current_path="$( echo "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g")" + fi + + if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" && ! ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + current_path="$( echo "${current_path}" | sed "s/~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}/1")" + fi + "$1_prompt_segment" "$0_${current_state}" "$2" "blue" "$DEFAULT_COLOR" "${current_path}" "${dir_states[$current_state]}" } -- cgit v1.2.3 From 8b8dcb20663e56c09ea12d7743a8a139a339cc56 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 10 Feb 2018 15:41:40 +0400 Subject: Last dir bold - missed reset Missed the bold reset code `%b` in the logic, which caused everthing after the last dir to be bold, including the command line entry. --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index faaa32ec..e7d3f1f1 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -869,7 +869,7 @@ prompt_dir() { if [[ -n "${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}" ]]; then if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then - current_path="$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}" + current_path="$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b" else current_path="$(dirname $current_path)/$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}" fi -- cgit v1.2.3 From cd1387f530450889c386ab48b7076987eadbff06 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 11 Feb 2018 07:17:50 +0400 Subject: Fixed reset bold on last dir missing. Missed the `%b` on making the last dir bold which leaked into the command prompt. --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index e7d3f1f1..fe660bc5 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -871,7 +871,7 @@ prompt_dir() { if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then current_path="$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b" else - current_path="$(dirname $current_path)/$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}" + current_path="$(dirname $current_path)/$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}%b" fi fi -- cgit v1.2.3 From 1949fa7b9ee0e9663eb845b964ae77c851d1fd6e Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 11 Feb 2018 07:48:22 +0400 Subject: Added set_default for bold dir segment Added `set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false` --- powerlevel9k.zsh-theme | 1 + 1 file changed, 1 insertion(+) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index fe660bc5..0a2d4cd8 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -718,6 +718,7 @@ prompt_command_execution_time() { # Dir: current working directory set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" +set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # Parameters: # * $1 Alignment: string - left|right # * $2 Index: integer -- cgit v1.2.3 From e644ad640e41f9023096e80a77191bab9e5792d6 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 11 Feb 2018 07:53:09 +0400 Subject: Changed testing for bold last dir --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 0a2d4cd8..3681fbbf 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -862,7 +862,7 @@ prompt_dir() { fi local bd dir_state_foreground dir_state_user_foreground - [[ -n "${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" ]] && bd="%B" || bd="" + [[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" = "true" ]] && bd="%B" || bd="" local dir_state_user_foreground="POWERLEVEL9K_DIR_${current_state}_FOREGROUND" local dir_state_foreground="${(P)dir_state_user_foreground}" -- cgit v1.2.3 From 4d71e217e964e4954e5c572f7ea48b4269338c6c Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 11 Feb 2018 16:31:30 +0400 Subject: Attempt to fix Travis error --- powerlevel9k.zsh-theme | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3681fbbf..40620220 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -864,11 +864,11 @@ prompt_dir() { local bd dir_state_foreground dir_state_user_foreground [[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" = "true" ]] && bd="%B" || bd="" - local dir_state_user_foreground="POWERLEVEL9K_DIR_${current_state}_FOREGROUND" - local dir_state_foreground="${(P)dir_state_user_foreground}" - [[ -z "${dir_state_foreground}" ]] && dir_state_foreground="${DEFAULT_COLOR}" + local dir_state_user_foreground=POWERLEVEL9K_DIR_${current_state}_FOREGROUND + local dir_state_foreground=${(P)dir_state_user_foreground} + [[ -z ${dir_state_foreground} ]] && dir_state_foreground="${DEFAULT_COLOR}" - if [[ -n "${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}" ]]; then + if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then current_path="$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b" else @@ -876,7 +876,7 @@ prompt_dir() { fi fi - if [[ -n "${POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND}" && $path_opt != "/" ]]; then + if [[ -n ${POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND} && $path_opt != "/" ]]; then current_path="$( echo "${current_path}" | sed "s/\//%F{$POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND}\/%F{$dir_state_foreground}/g")" fi -- cgit v1.2.3 From 4ac02b0f1b92dae3710805fcbd60bca4af5315b0 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 11 Feb 2018 16:38:16 +0400 Subject: Renamed $bd to $bld --- powerlevel9k.zsh-theme | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 40620220..c99da804 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -861,8 +861,8 @@ prompt_dir() { current_state="HOME_SUBFOLDER" fi - local bd dir_state_foreground dir_state_user_foreground - [[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" = "true" ]] && bd="%B" || bd="" + local bld dir_state_foreground dir_state_user_foreground + [[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" = "true" ]] && bld="%B" || bld="" local dir_state_user_foreground=POWERLEVEL9K_DIR_${current_state}_FOREGROUND local dir_state_foreground=${(P)dir_state_user_foreground} @@ -870,9 +870,9 @@ prompt_dir() { if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then - current_path="$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b" + current_path="$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b" else - current_path="$(dirname $current_path)/$bd%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}%b" + current_path="$(dirname $current_path)/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}%b" fi fi -- cgit v1.2.3 From 5491ee35c33ac8321fa6e61f1c330a1731e5d689 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 11 Feb 2018 17:49:09 +0400 Subject: Updated logic in prompt_dir Added a case where there is no last dir foreground, but still bold. --- powerlevel9k.zsh-theme | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c99da804..e5429635 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -870,9 +870,15 @@ prompt_dir() { if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then - current_path="$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b" + current_path="$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b%F{$dir_state_foreground}" else - current_path="$(dirname $current_path)/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%F{$dir_state_foreground}%b" + current_path="$(dirname $current_path)/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%b%F{$dir_state_foreground}" + fi + else + if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + current_path="$bld%F{$dir_state_foreground}$current_path%b%F{$dir_state_foreground}" + else + current_path="%F{$dir_state_foreground}$(dirname $current_path)/$bld$(basename $current_path)%b%F{$dir_state_foreground}" fi fi -- cgit v1.2.3 From 536549ea6231dd1fb15077de6e90abd1809eff04 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 14 Feb 2018 11:42:40 +0400 Subject: Parameter substitution instead of dirname/basename Replaced external calls to `dirname` and `basename` with paramater substitution. --- powerlevel9k.zsh-theme | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index e5429635..4f313418 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -868,17 +868,21 @@ prompt_dir() { local dir_state_foreground=${(P)dir_state_user_foreground} [[ -z ${dir_state_foreground} ]] && dir_state_foreground="${DEFAULT_COLOR}" + local dir_name base_name + dir_name=${path_opt%/*} + base_name=${path_opt##*/} + if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then - if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + if [[ $path_opt == "/" || $path_opt == "~" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then current_path="$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$current_path%F{$dir_state_foreground}%b%F{$dir_state_foreground}" else - current_path="$(dirname $current_path)/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}$(basename $current_path)%b%F{$dir_state_foreground}" + current_path="${dir_name}/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b%F{$dir_state_foreground}" fi else - if [[ $path_opt == "/" || $path_opt == "~" || $(dirname $path_opt) == "/" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + if [[ $path_opt == "/" || $path_opt == "~" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then current_path="$bld%F{$dir_state_foreground}$current_path%b%F{$dir_state_foreground}" else - current_path="%F{$dir_state_foreground}$(dirname $current_path)/$bld$(basename $current_path)%b%F{$dir_state_foreground}" + current_path="%F{$dir_state_foreground}${dir_name}/$bld${base_name}%b%F{$dir_state_foreground}" fi fi -- cgit v1.2.3 From 7cca0876e241620199fa1c55c20164af952e310a Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 14 Feb 2018 11:48:14 +0400 Subject: Fixed = instead of == inside [[]] line 865 Missed an = inside the test for highlighting the last dir of the working directory. Travis should work now *fingers crossed* --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 4f313418..4de1fd94 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -862,7 +862,7 @@ prompt_dir() { fi 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="%B" || bld="" local dir_state_user_foreground=POWERLEVEL9K_DIR_${current_state}_FOREGROUND local dir_state_foreground=${(P)dir_state_user_foreground} -- cgit v1.2.3 From 2e417ec510c7f6a26eb332691d9baf7d91e8362d Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 14 Feb 2018 11:57:32 +0400 Subject: Explicitly test if DIR_OMIT_FIRST_CHARACTER = true Changed ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} to "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" Lines 876 & 882 --- powerlevel9k.zsh-theme | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 4de1fd94..b682b0ea 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -873,13 +873,13 @@ prompt_dir() { base_name=${path_opt##*/} if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then - if [[ $path_opt == "/" || $path_opt == "~" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; 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%F{$dir_state_foreground}%b%F{$dir_state_foreground}" else current_path="${dir_name}/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b%F{$dir_state_foreground}" fi else - if [[ $path_opt == "/" || $path_opt == "~" || ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + if [[ $path_opt == "/" || $path_opt == "~" || "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then current_path="$bld%F{$dir_state_foreground}$current_path%b%F{$dir_state_foreground}" else current_path="%F{$dir_state_foreground}${dir_name}/$bld${base_name}%b%F{$dir_state_foreground}" @@ -894,7 +894,7 @@ prompt_dir() { current_path="$( echo "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g")" fi - if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" && ! ${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER} ]]; then + if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" && ! "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then current_path="$( echo "${current_path}" | sed "s/~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}/1")" fi -- cgit v1.2.3 From 9c6330624e1597d337b9aae2dc8e350b5e90a87a Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 14 Feb 2018 12:15:56 +0400 Subject: Cleaned up DIR_PATH_HIGHLIGHT code Removed double call to %F and changed $x variables to ${x} for consistency. --- powerlevel9k.zsh-theme | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index b682b0ea..3454a673 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -874,15 +874,15 @@ 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%F{$dir_state_foreground}%b%F{$dir_state_foreground}" + current_path="${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${current_path}%b%F{$dir_state_foreground}" else - current_path="${dir_name}/$bld%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b%F{$dir_state_foreground}" + current_path="${dir_name}/${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b%F{$dir_state_foreground}" fi else if [[ $path_opt == "/" || $path_opt == "~" || "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then - current_path="$bld%F{$dir_state_foreground}$current_path%b%F{$dir_state_foreground}" + current_path="${bld}${current_path}%b%F{$dir_state_foreground}" else - current_path="%F{$dir_state_foreground}${dir_name}/$bld${base_name}%b%F{$dir_state_foreground}" + current_path="${dir_name}/${bld}${base_name}%b%F{$dir_state_foreground}" fi fi -- cgit v1.2.3 From c71e735c7d0d39374cfe9952598482419eb40f53 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Wed, 14 Feb 2018 12:33:36 +0400 Subject: More code cleaning (extra %F calls) --- powerlevel9k.zsh-theme | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3454a673..9fb280bf 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -874,15 +874,15 @@ 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%F{$dir_state_foreground}" + current_path="${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${current_path}%b" else - current_path="${dir_name}/${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b%F{$dir_state_foreground}" + current_path="${dir_name}/${bld}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${base_name}%b" fi else if [[ $path_opt == "/" || $path_opt == "~" || "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then - current_path="${bld}${current_path}%b%F{$dir_state_foreground}" + current_path="${bld}${current_path}%b" else - current_path="${dir_name}/${bld}${base_name}%b%F{$dir_state_foreground}" + current_path="${dir_name}/${bld}${base_name}%b" fi fi -- cgit v1.2.3 From 64cbdaf4c2a61e30365dc0f12b672d57e879d2aa Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 17:36:36 +0400 Subject: Streamlined dir strategies Removed sed from `truncate_middle`, `truncate_from_right` and partially from `truncate_with_package_name` --- powerlevel9k.zsh-theme | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 9fb280bf..f7d31002 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -724,15 +724,40 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # * $2 Index: integer prompt_dir() { local current_path="$(print -P "%~")" + local paths=(${(s:/:)PWD}) + local cur_path cur_short_path directory dir_length cur_dir + if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then - set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\U2026' + set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\u2026' case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) - current_path=$(pwd | sed -e "s,^$HOME,~," | sed $SED_EXTENDED_REGEX_PARAMETER "s/([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})[^/]+([^/]{$POWERLEVEL9K_SHORTEN_DIR_LENGTH})\//\1$POWERLEVEL9K_SHORTEN_DELIMITER\2\//g") + cur_short_path='/' + local last_pos + local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) + for directory in ${paths[@]} + do + cur_dir=$directory + dir_length=${#cur_dir} + if (( $dir_length > $max_length )); then + last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) + cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER${cur_dir:$last_pos:$dir_length} + fi + cur_short_path+="$cur_dir/" + done + current_path="${cur_short_path: : -1}" ;; truncate_from_right) - current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) + cur_short_path='/' + for directory in ${paths[@]} + do + cur_dir=$directory + if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH )); then + cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER + fi + cur_short_path+="$cur_dir/" + done + current_path="${cur_short_path: : -1}" ;; truncate_with_package_name) local name repo_path package_path current_dir zero @@ -744,11 +769,11 @@ prompt_dir() { # Remove trailing slash from git path, so that we can # remove that git path from the pwd. gitPath=${gitPath%/} - package_path=${$(pwd)%%$gitPath} + package_path=${$current_path%%$gitPath} # Remove trailing slash package_path=${package_path%/} elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then - package_path=${$(pwd)%%/.git*} + package_path=${$current_path%%/.git*} fi # Replace the shortest possible match of the marked folder from @@ -757,11 +782,10 @@ prompt_dir() { # in the path (this is done by the "zero" pattern; see # http://stackoverflow.com/a/40855342/5586433). local zero='%([BSUbfksu]|([FB]|){*})' - current_dir=$(pwd) # Then, find the length of the package_path string, and save the # subdirectory path as a substring of the current directory's path from 0 # to the length of the package path's string - subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") + subdirectory_path=$(truncatePathFromRight "${current_path:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path defined POWERLEVEL9K_DIR_PACKAGE_FILES || POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json) @@ -815,7 +839,6 @@ prompt_dir() { truncate_to_unique) # for each parent path component find the shortest unique beginning # characters sequence. Source: https://stackoverflow.com/a/45336078 - paths=(${(s:/:)PWD}) cur_path='/' cur_short_path='/' for directory in ${paths[@]} -- cgit v1.2.3 From 110f54f7e2d38c0f59ea25fcc947144e317382f1 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 18:12:07 +0400 Subject: Added edge case tests for ~ and / Fixed edge cases --- powerlevel9k.zsh-theme | 77 ++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 34 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f7d31002..2f63b486 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -723,8 +723,9 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # * $1 Alignment: string - left|right # * $2 Index: integer prompt_dir() { - local current_path="$(print -P "%~")" - local paths=(${(s:/:)PWD}) + local current_dir="$(print -P "%~")" + local paths + [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) || paths[0] = "/" local cur_path cur_short_path directory dir_length cur_dir if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then @@ -732,32 +733,40 @@ prompt_dir() { case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) - cur_short_path='/' - local last_pos - local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) - for directory in ${paths[@]} - do - cur_dir=$directory - dir_length=${#cur_dir} - if (( $dir_length > $max_length )); then - last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) - cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER${cur_dir:$last_pos:$dir_length} - fi - cur_short_path+="$cur_dir/" - done - current_path="${cur_short_path: : -1}" + if [[ $current_dir != "/" ]]; then + [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' + local last_pos + local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) + for directory in ${paths[@]} + do + cur_dir=$directory + dir_length=${#cur_dir} + if (( $dir_length > $max_length )); then + last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) + cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER${cur_dir:$last_pos:$dir_length} + fi + cur_short_path+="$cur_dir/" + done + current_path="${cur_short_path: : -1}" + else + current_path="/" + fi ;; truncate_from_right) - cur_short_path='/' - for directory in ${paths[@]} - do - cur_dir=$directory - if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH )); then - cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER - fi - cur_short_path+="$cur_dir/" - done - current_path="${cur_short_path: : -1}" + if [[ $current_dir != "/" ]]; then + [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' + for directory in ${paths[@]} + do + cur_dir=$directory + if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH )); then + cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER + fi + cur_short_path+="$cur_dir/" + done + current_path="${cur_short_path: : -1}" + else + current_path="/" + fi ;; truncate_with_package_name) local name repo_path package_path current_dir zero @@ -769,11 +778,11 @@ prompt_dir() { # Remove trailing slash from git path, so that we can # remove that git path from the pwd. gitPath=${gitPath%/} - package_path=${$current_path%%$gitPath} + package_path=${current_dir%%$gitPath} # Remove trailing slash - package_path=${package_path%/} + package_path=${package_dir%/} elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then - package_path=${$current_path%%/.git*} + package_path=${current_dir%%/.git*} fi # Replace the shortest possible match of the marked folder from @@ -785,7 +794,7 @@ prompt_dir() { # Then, find the length of the package_path string, and save the # subdirectory path as a substring of the current directory's path from 0 # to the length of the package path's string - subdirectory_path=$(truncatePathFromRight "${current_path:${#${(S%%)package_path//$~zero/}}}") + subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path defined POWERLEVEL9K_DIR_PACKAGE_FILES || POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json) @@ -807,7 +816,7 @@ prompt_dir() { # from the package.json and append the current subdirectory current_path="`echo $packageName | tr -d '"'`$subdirectory_path" else - current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) + current_path=$(truncatePathFromRight ${current_path//$HOME/"~"} ) fi ;; truncate_with_folder_marker) @@ -834,7 +843,7 @@ prompt_dir() { # Replace the shortest possible match of the marked folder from # the current path. - current_path=$current_path${PWD#${last_marked_folder}*} + current_path=$current_path${current_dir#${last_marked_folder}*} ;; truncate_to_unique) # for each parent path component find the shortest unique beginning @@ -878,9 +887,9 @@ prompt_dir() { local current_state="DEFAULT" if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then current_state="NOT_WRITABLE" - elif [[ $(print -P "%~") == '~' ]]; then + elif [[ $current_dir == '~' ]]; then current_state="HOME" - elif [[ $(print -P "%~") == '~'* ]]; then + elif [[ $current_dir == '~'* ]]; then current_state="HOME_SUBFOLDER" fi -- cgit v1.2.3 From ad4b284e56244842c69e5e99aac4d5626c10a542 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 18:26:11 +0400 Subject: Fixed accidentally truncating current folder too --- powerlevel9k.zsh-theme | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 2f63b486..4abee3b4 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -725,7 +725,7 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_dir="$(print -P "%~")" local paths - [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) || paths[0] = "/" + [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) local cur_path cur_short_path directory dir_length cur_dir if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then @@ -741,7 +741,7 @@ prompt_dir() { do cur_dir=$directory dir_length=${#cur_dir} - if (( $dir_length > $max_length )); then + if (( $dir_length > $max_length )) && [[ $cur_dir != $paths[${#paths}] ]]; then last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER${cur_dir:$last_pos:$dir_length} fi @@ -758,7 +758,7 @@ prompt_dir() { for directory in ${paths[@]} do cur_dir=$directory - if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH )); then + if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH && [[ $cur_dir != $paths[${#paths}] ]]; then cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From 29620f3d8187a6160950289dda72bb30bf040873 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 18:39:39 +0400 Subject: Accidentally deleted )) which threw an error --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 4abee3b4..0c7292a3 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -758,7 +758,7 @@ prompt_dir() { for directory in ${paths[@]} do cur_dir=$directory - if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH && [[ $cur_dir != $paths[${#paths}] ]]; then + if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) && [[ $cur_dir != $paths[${#paths}] ]]; then cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From b73af072c1b0dd4bce84185c5374054e5dcb70df Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 18:54:43 +0400 Subject: Fixed truncate_from_right to incl length of delim Updated length test to include delimiter length. Added comments --- powerlevel9k.zsh-theme | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 0c7292a3..ecca1f38 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -725,7 +725,7 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_dir="$(print -P "%~")" local paths - [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) + [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) # only split if not root folder local cur_path cur_short_path directory dir_length cur_dir if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then @@ -733,37 +733,37 @@ prompt_dir() { case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) - if [[ $current_dir != "/" ]]; then - [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' + if [[ $current_dir != "/" ]]; then # root is an exception and won't have paths + [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' # if we are in the $HOME folder, we don't need starting / local last_pos - local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) - for directory in ${paths[@]} + local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) # has to be double the length for beginning / end count + for directory in ${paths[@]} # go through all the paths do cur_dir=$directory dir_length=${#cur_dir} - if (( $dir_length > $max_length )) && [[ $cur_dir != $paths[${#paths}] ]]; then + if (( $dir_length > $max_length )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER${cur_dir:$last_pos:$dir_length} fi cur_short_path+="$cur_dir/" done - current_path="${cur_short_path: : -1}" + current_path="${cur_short_path: : -1}" # remove trailing slash else current_path="/" fi ;; truncate_from_right) - if [[ $current_dir != "/" ]]; then - [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' + if [[ $current_dir != "/" ]]; then # root is an exception and won't have paths + [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' # if we are in the $HOME folder, we don't need starting / for directory in ${paths[@]} do cur_dir=$directory - if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) && [[ $cur_dir != $paths[${#paths}] ]]; then + if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#POWERLEVEL9K_SHORTEN_DELIMITER} )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER fi cur_short_path+="$cur_dir/" done - current_path="${cur_short_path: : -1}" + current_path="${cur_short_path: : -1}" # remove trailing slash else current_path="/" fi -- cgit v1.2.3 From 728aa0ec387ab12644f905b5b88cc5638b9da810 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 20:42:35 +0400 Subject: Updated previous test as Travis still fails --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index ecca1f38..c6c17c86 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -758,7 +758,7 @@ prompt_dir() { for directory in ${paths[@]} do cur_dir=$directory - if (( ${#cur_dir} > $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#POWERLEVEL9K_SHORTEN_DELIMITER} )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + if (( ${#cur_dir} > ( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#POWERLEVEL9K_SHORTEN_DELIMITER} ) )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From 9a3dd6bf253a5cd1171d06e6c74a537423e4bebb Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sat, 17 Feb 2018 21:09:27 +0400 Subject: Fix issue with SHORTEN_DELIM If `$POWERLEVEL9K_SHORTEN_DELIMITER` is a unicode escape sequence, its length is 6 instead of 1. Added variable to hold actual character(s) --- powerlevel9k.zsh-theme | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c6c17c86..d5db242f 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -730,6 +730,7 @@ prompt_dir() { if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\u2026' + local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character if required case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) @@ -743,7 +744,7 @@ prompt_dir() { dir_length=${#cur_dir} if (( $dir_length > $max_length )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) - cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER${cur_dir:$last_pos:$dir_length} + cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim${cur_dir:$last_pos:$dir_length} fi cur_short_path+="$cur_dir/" done @@ -758,8 +759,10 @@ prompt_dir() { for directory in ${paths[@]} do cur_dir=$directory - if (( ${#cur_dir} > ( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#POWERLEVEL9K_SHORTEN_DELIMITER} ) )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path - cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$POWERLEVEL9K_SHORTEN_DELIMITER + dir_length=${#cur_dir} + local threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) + if (( $dir_length > $threshhold )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim fi cur_short_path+="$cur_dir/" done -- cgit v1.2.3 From 4f1da4a64dcbd0923d591ffda50ce1f5d2870ce2 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 16:22:59 +0400 Subject: Changed current_dir - Test Travis response --- powerlevel9k.zsh-theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index d5db242f..2e483844 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -723,13 +723,14 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # * $1 Alignment: string - left|right # * $2 Index: integer prompt_dir() { - local current_dir="$(print -P "%~")" + local current_dir="$(echo $PWD)" + current_dir=${current_dir//$HOME/"~"} local paths [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) # only split if not root folder local cur_path cur_short_path directory dir_length cur_dir if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then - set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\u2026' + set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026" local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character if required case "$POWERLEVEL9K_SHORTEN_STRATEGY" in -- cgit v1.2.3 From b1b0ad949a0ee558fa6b6c25882b961c8200a9eb Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 16:29:05 +0400 Subject: Updated truncate_from_right test --- powerlevel9k.zsh-theme | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 2e483844..0f6461c3 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -723,8 +723,7 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # * $1 Alignment: string - left|right # * $2 Index: integer prompt_dir() { - local current_dir="$(echo $PWD)" - current_dir=${current_dir//$HOME/"~"} + local current_dir="$(print -P '%~')" local paths [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) # only split if not root folder local cur_path cur_short_path directory dir_length cur_dir @@ -762,7 +761,7 @@ prompt_dir() { cur_dir=$directory dir_length=${#cur_dir} local threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( $dir_length > $threshhold )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + if (( ( $dir_length + 1) > $threshhold )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From 8180d724efd4e7f97808429d3ea12cec585a7465 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 16:35:07 +0400 Subject: Added test for dir_length == trunc_length --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 0f6461c3..6572b2d0 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -761,7 +761,7 @@ prompt_dir() { cur_dir=$directory dir_length=${#cur_dir} local threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( ( $dir_length + 1) > $threshhold )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + if (( ( $dir_length > $threshhold )) && [[ !c $dir_length == $POWERLEVEL9K_SHORTEN_DIR_LENGTH && $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From 97d4e2877db2bb5e63479e2f987ce475a26d9d0a Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 16:56:32 +0400 Subject: Fixed typo --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 6572b2d0..5a322a57 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -761,7 +761,7 @@ prompt_dir() { cur_dir=$directory dir_length=${#cur_dir} local threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( ( $dir_length > $threshhold )) && [[ !c $dir_length == $POWERLEVEL9K_SHORTEN_DIR_LENGTH && $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + if (( ( $dir_length > $threshhold )) && [[ ! $dir_length == $POWERLEVEL9K_SHORTEN_DIR_LENGTH && $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From 0acdc4e03231293327269b87aabc2d337ed474d0 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 16:58:06 +0400 Subject: Fixed another typo - shouldn't code when sick :p --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 5a322a57..1589cf3d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -761,7 +761,7 @@ prompt_dir() { cur_dir=$directory dir_length=${#cur_dir} local threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( ( $dir_length > $threshhold )) && [[ ! $dir_length == $POWERLEVEL9K_SHORTEN_DIR_LENGTH && $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + if (( $dir_length > $threshhold )) && [[ $dir_length != $POWERLEVEL9K_SHORTEN_DIR_LENGTH && $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim fi cur_short_path+="$cur_dir/" -- cgit v1.2.3 From 38d7e60ae3f9efd5f4a81d52b5ce6046b0416b8b Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 18:36:18 +0400 Subject: Rewrote trunc middle/right to use for loop --- powerlevel9k.zsh-theme | 88 +++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 47 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 1589cf3d..c0820475 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -723,52 +723,44 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # * $1 Alignment: string - left|right # * $2 Index: integer prompt_dir() { - local current_dir="$(print -P '%~')" - local paths - [[ current_dir != "/" ]] && paths=(${(s:/:)current_dir}) # only split if not root folder - local cur_path cur_short_path directory dir_length cur_dir - + local current_path="$(print -P '%~')" + 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 set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026" - local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character if required + local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) - if [[ $current_dir != "/" ]]; then # root is an exception and won't have paths - [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' # if we are in the $HOME folder, we don't need starting / - local last_pos + if (( ${#paths} > 0 )); then # root is an exception and won't have paths local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) # has to be double the length for beginning / end count - for directory in ${paths[@]} # go through all the paths - do - cur_dir=$directory - dir_length=${#cur_dir} - if (( $dir_length > $max_length )) && [[ $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path + local last_pos + for (( i=1; i<${#paths}; i++ )); do + test_dir=$paths[$i] + test_dir_length=${#test_dir} + if (( $test_dir_length > $max_length )); then # only shorten if long enough last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) - cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim${cur_dir:$last_pos:$dir_length} + trunc_path+="${test_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim${test_dir:$last_pos:$test_dir_length}/" + else + trunc_path+="${test_dir}/" fi - cur_short_path+="$cur_dir/" done - current_path="${cur_short_path: : -1}" # remove trailing slash - else - current_path="/" + current_path=$trunc_path${current_path:t} fi ;; truncate_from_right) - if [[ $current_dir != "/" ]]; then # root is an exception and won't have paths - [[ $current_dir == '~'* ]] && cur_short_path='' || cur_short_path='/' # if we are in the $HOME folder, we don't need starting / - for directory in ${paths[@]} - do - cur_dir=$directory - dir_length=${#cur_dir} - local threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( $dir_length > $threshhold )) && [[ $dir_length != $POWERLEVEL9K_SHORTEN_DIR_LENGTH && $cur_dir != $paths[${#paths}] ]]; then # only shorten if long enough and not last path - cur_dir=${cur_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim + if (( ${#paths} > 0 )); then # root is an exception and won't have paths + for (( i=1; i<${#paths}; i++ )); do + test_dir="$paths[$i]" + test_dir_length=${#test_dir} + threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) + if (( $test_dir_length > $threshhold )); then # only shorten if long enough + trunc_path+="${test_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim/" + else + trunc_path+="${test_dir}/" fi - cur_short_path+="$cur_dir/" done - current_path="${cur_short_path: : -1}" # remove trailing slash - else - current_path="/" + current_path=$trunc_path${current_path:t} fi ;; truncate_with_package_name) @@ -851,22 +843,24 @@ prompt_dir() { truncate_to_unique) # for each parent path component find the shortest unique beginning # characters sequence. Source: https://stackoverflow.com/a/45336078 - cur_path='/' - cur_short_path='/' - for directory in ${paths[@]} - do - cur_dir='' - for (( i=0; i<${#directory}; i++ )); do - cur_dir+="${directory:$i:1}" - matching=("$cur_path"/"$cur_dir"*/) - if [[ ${#matching[@]} -eq 1 ]]; then - break - fi + if (( ${#paths} > 0 )); then # root is an exception and won't have paths + local matching + local cur_path='/' + [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' + 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 - cur_short_path+="$cur_dir/" - cur_path+="$directory/" - done - current_path="${cur_short_path: : -1}" + current_path="${trunc_path: : -1}" + fi ;; *) current_path="$(print -P "%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c")" -- cgit v1.2.3 From 9f87bd5654bcf7ed28abdc20109368cf1cc130e4 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 19:05:37 +0400 Subject: Added / for non-home folders --- powerlevel9k.zsh-theme | 2 ++ 1 file changed, 2 insertions(+) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c0820475..53f115c3 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -732,6 +732,7 @@ prompt_dir() { case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) + [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' if (( ${#paths} > 0 )); then # root is an exception and won't have paths local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) # has to be double the length for beginning / end count local last_pos @@ -749,6 +750,7 @@ prompt_dir() { fi ;; truncate_from_right) + [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' if (( ${#paths} > 0 )); then # root is an exception and won't have paths for (( i=1; i<${#paths}; i++ )); do test_dir="$paths[$i]" -- cgit v1.2.3 From 3b182d2eb6f97da5731ba543a2486622d6cfd287 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 22:35:20 +0400 Subject: Updated code for testing --- powerlevel9k.zsh-theme | 60 +++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 53f115c3..70aca115 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -756,7 +756,7 @@ prompt_dir() { test_dir="$paths[$i]" test_dir_length=${#test_dir} threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( $test_dir_length > $threshhold )); then # only shorten if long enough + if (( $test_dir_length > $threshhold && $test_dir_length > 3 )); then # only shorten if long enough trunc_path+="${test_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim/" else trunc_path+="${test_dir}/" @@ -765,6 +765,32 @@ prompt_dir() { current_path=$trunc_path${current_path:t} fi ;; + truncate_with_folder_marker) + local last_marked_folder marked_folder + set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" + + # Search for the folder marker in the parent directories and + # buildup a pattern that is removed from the current path + # later on. + for marked_folder in $(upsearch $POWERLEVEL9K_SHORTEN_FOLDER_MARKER); do + if [[ "$marked_folder" == "/" ]]; then + # If we reached root folder, stop upsearch. + current_path="/" + elif [[ "$marked_folder" == "$HOME" ]]; then + # If we reached home folder, stop upsearch. + current_path="~" + elif [[ "${marked_folder%/*}" == $last_marked_folder ]]; then + current_path="${current_path%/}/${marked_folder##*/}" + else + current_path="${current_path%/}/$POWERLEVEL9K_SHORTEN_DELIMITER/${marked_folder##*/}" + fi + last_marked_folder=$marked_folder + done + + # Replace the shortest possible match of the marked folder from + # the current path. + current_path=$current_path${current_path#${last_marked_folder}*} + ;; truncate_with_package_name) local name repo_path package_path current_dir zero @@ -775,11 +801,11 @@ prompt_dir() { # Remove trailing slash from git path, so that we can # remove that git path from the pwd. gitPath=${gitPath%/} - package_path=${current_dir%%$gitPath} + package_path=${current_path%%$gitPath} # Remove trailing slash package_path=${package_dir%/} elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then - package_path=${current_dir%%/.git*} + package_path=${current_path%%/.git*} fi # Replace the shortest possible match of the marked folder from @@ -791,7 +817,7 @@ prompt_dir() { # Then, find the length of the package_path string, and save the # subdirectory path as a substring of the current directory's path from 0 # to the length of the package path's string - subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") + subdirectory_path=$(truncatePathFromRight "${current_path:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path defined POWERLEVEL9K_DIR_PACKAGE_FILES || POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json) @@ -816,32 +842,6 @@ prompt_dir() { current_path=$(truncatePathFromRight ${current_path//$HOME/"~"} ) fi ;; - truncate_with_folder_marker) - local last_marked_folder marked_folder - set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" - - # Search for the folder marker in the parent directories and - # buildup a pattern that is removed from the current path - # later on. - for marked_folder in $(upsearch $POWERLEVEL9K_SHORTEN_FOLDER_MARKER); do - if [[ "$marked_folder" == "/" ]]; then - # If we reached root folder, stop upsearch. - current_path="/" - elif [[ "$marked_folder" == "$HOME" ]]; then - # If we reached home folder, stop upsearch. - current_path="~" - elif [[ "${marked_folder%/*}" == $last_marked_folder ]]; then - current_path="${current_path%/}/${marked_folder##*/}" - else - current_path="${current_path%/}/$POWERLEVEL9K_SHORTEN_DELIMITER/${marked_folder##*/}" - fi - last_marked_folder=$marked_folder - done - - # Replace the shortest possible match of the marked folder from - # the current path. - current_path=$current_path${current_dir#${last_marked_folder}*} - ;; truncate_to_unique) # for each parent path component find the shortest unique beginning # characters sequence. Source: https://stackoverflow.com/a/45336078 -- cgit v1.2.3 From fb09d753915ff193f4896db0b74a1b67aef2cfa4 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Sun, 18 Feb 2018 22:56:52 +0400 Subject: Updated truncate_to_unique --- powerlevel9k.zsh-theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 70aca115..fcd1ab98 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -725,7 +725,7 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_path="$(print -P '%~')" 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 + (( ${#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 set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026" local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character @@ -861,7 +861,8 @@ prompt_dir() { trunc_path+="$test_dir/" cur_path+="$directory/" done - current_path="${trunc_path: : -1}" + [[ $current_path == "~"* ]] && current_path="~/" || current_path="" + current_path+="${trunc_path: : -1}" fi ;; *) -- cgit v1.2.3 From 58342612eb8ac178e53779224eb6fd00bb7a4649 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 16:05:58 +0400 Subject: Improved truncation code --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index fcd1ab98..36a641d8 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -861,8 +861,8 @@ prompt_dir() { trunc_path+="$test_dir/" cur_path+="$directory/" done - [[ $current_path == "~"* ]] && current_path="~/" || current_path="" - current_path+="${trunc_path: : -1}" + [[ $current_path == "~"* ]] && trunc_path="~/$trunc_path" + current_path="${trunc_path: : -1}" fi ;; *) -- cgit v1.2.3 From 21b7749075a2af48dd52dcd93e3d3d4f8e63d0c4 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 22:00:15 +0400 Subject: Added truncatePath to utilities.zsh Added `function truncatePath()` to utilities.zsh to take care of truncation. This is pure zsh code, without calls to `sed`. Parameters are: * $1 Path: string - the directory path to be truncated * $2 Length: integer - length to truncate to * $3 Delimiter: string - the delimiter to use * $4 From: string - "right" | "middle". If omited, assumes right. Cleaned up code to use the new function instead. --- functions/utilities.zsh | 75 +++++++++++++++++++++++++++++++++++++++ powerlevel9k.zsh-theme | 93 ++++++++++++++++++------------------------------- 2 files changed, 108 insertions(+), 60 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index b4bfb838..9f1ea53e 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -213,6 +213,81 @@ function segmentShouldBeJoined() { fi } +################################################################ +# Given a directory path, truncate it according to the settings. +# Parameters: +# * $1 Path: string - the directory path to be truncated +# * $2 Length: integer - length to truncate to +# * $3 Delimiter: string - the delimiter to use +# * $4 From: string - "right" | "middle". If omited, assumes right. +function truncatePath() { + # if the current path is not 1 character long (e.g. "/" or "~") + if (( ${#1} > 1 )); then + # convert $2 from string to integer + 2=$(( $2 )) + # set $3 to "" if not defined + [[ -z $3 ]] && local 3="" || 3=$(echo -n $3) + # set $4 to "right" if not defined + [[ -z $4 ]] && 4="right" + # create a variable for the truncated path. + local trunc_path + # if the path is in the home folder, don't add a "/" to the start + [[ $1 != "~"* ]] && trunc_path='/' || trunc_path='' + # split the path into an array using "/" as the delimiter and remove "~/" + local paths=(${(s:/:)1}) + # declare locals for the directory being tested and its length + local test_dir test_dir_length + # do the needed truncation + case $4 in + right) + # include the delimiter length in the threshhold + local threshhold=$(( $2 + ${#3} )) + # loop through the paths + for (( i=1; i<${#paths}; i++ )); do + # get the current directory value + test_dir=$paths[$i] + test_dir_length=${#test_dir} + # only truncate if the resulting truncation will be shorter than + # the truncation + delimiter length and at least 3 characters + if (( $test_dir_length > $threshhold )) && (( $test_dir_length > 3 )); then + # use the first $2 characters and the delimiter + trunc_path+="${test_dir:0:$2}$3/" + else + # use the full path + trunc_path+="${test_dir}/" + fi + done + ;; + middle) + # we need double the length for start and end truncation + delimiter length + local threshhold=$(( $2 * 2 + ${#3} )) + # create a variable for the start of the end truncation + local last_pos + # loop through the paths + for (( i=1; i<${#paths}; i++ )); do + # get the current directory value + test_dir=$paths[$i] + test_dir_length=${#test_dir} + # only truncate if the resulting truncation will be shorter than + # the truncation + delimiter length + if (( $test_dir_length > $threshhold + ${#3} )); then + # use the first $2 characters, the delimiter and the last $2 characters + last_pos=$(( $test_dir_length - $2 )) + trunc_path+="${test_dir:0:$2}$3${test_dir:$last_pos:$test_dir_length}/" + else + # use the full path + trunc_path+="${test_dir}/" + fi + done + ;; + esac + # return the truncated path + the current directory + echo $trunc_path${1:t} + else # current path is 1 character long (e.g. "/" or "~") + echo $1 + fi +} + # Given a directory path, truncate it according to the settings for # `truncate_from_right` function truncatePathFromRight() { diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 36a641d8..64682fa3 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -716,12 +716,12 @@ prompt_command_execution_time() { ################################################################ # Dir: current working directory -set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" -set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" -set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false # Parameters: # * $1 Alignment: string - left|right # * $2 Index: integer +set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" +set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" +set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_path="$(print -P '%~')" local paths directory test_dir test_dir_length trunc_path threshhold @@ -732,64 +732,38 @@ prompt_dir() { case "$POWERLEVEL9K_SHORTEN_STRATEGY" in truncate_middle) - [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' - if (( ${#paths} > 0 )); then # root is an exception and won't have paths - local max_length=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH * 2 )) # has to be double the length for beginning / end count - local last_pos - for (( i=1; i<${#paths}; i++ )); do - test_dir=$paths[$i] - test_dir_length=${#test_dir} - if (( $test_dir_length > $max_length )); then # only shorten if long enough - last_pos=$(( $dir_length - $POWERLEVEL9K_SHORTEN_DIR_LENGTH )) - trunc_path+="${test_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim${test_dir:$last_pos:$test_dir_length}/" - else - trunc_path+="${test_dir}/" - fi - done - current_path=$trunc_path${current_path:t} - fi + current_path=$(truncatePath $current_path $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER "middle") ;; truncate_from_right) - [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' + current_path=$(truncatePath "$current_path" $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER) + ;; + truncate_with_folder_marker) if (( ${#paths} > 0 )); then # root is an exception and won't have paths - for (( i=1; i<${#paths}; i++ )); do - test_dir="$paths[$i]" - test_dir_length=${#test_dir} - threshhold=$(( $POWERLEVEL9K_SHORTEN_DIR_LENGTH + ${#delim} )) - if (( $test_dir_length > $threshhold && $test_dir_length > 3 )); then # only shorten if long enough - trunc_path+="${test_dir:0:$POWERLEVEL9K_SHORTEN_DIR_LENGTH}$delim/" + local last_marked_folder marked_folder + set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" + + # Search for the folder marker in the parent directories and + # buildup a pattern that is removed from the current path + # later on. + for marked_folder in $(upsearch $POWERLEVEL9K_SHORTEN_FOLDER_MARKER); do + if [[ "$marked_folder" == "/" ]]; then + # If we reached root folder, stop upsearch. + trunc_path="/" + elif [[ "$marked_folder" == "$HOME" ]]; then + # If we reached home folder, stop upsearch. + trunc_path="~" + elif [[ "${marked_folder%/*}" == $last_marked_folder ]]; then + trunc_path="${trunc_path%/}/${marked_folder##*/}" else - trunc_path+="${test_dir}/" + trunc_path="${trunc_path%/}/$POWERLEVEL9K_SHORTEN_DELIMITER/${marked_folder##*/}" fi + last_marked_folder=$marked_folder done - current_path=$trunc_path${current_path:t} - fi - ;; - truncate_with_folder_marker) - local last_marked_folder marked_folder - set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" - - # Search for the folder marker in the parent directories and - # buildup a pattern that is removed from the current path - # later on. - for marked_folder in $(upsearch $POWERLEVEL9K_SHORTEN_FOLDER_MARKER); do - if [[ "$marked_folder" == "/" ]]; then - # If we reached root folder, stop upsearch. - current_path="/" - elif [[ "$marked_folder" == "$HOME" ]]; then - # If we reached home folder, stop upsearch. - current_path="~" - elif [[ "${marked_folder%/*}" == $last_marked_folder ]]; then - current_path="${current_path%/}/${marked_folder##*/}" - else - current_path="${current_path%/}/$POWERLEVEL9K_SHORTEN_DELIMITER/${marked_folder##*/}" - fi - last_marked_folder=$marked_folder - done - # Replace the shortest possible match of the marked folder from - # the current path. - current_path=$current_path${current_path#${last_marked_folder}*} + # Replace the shortest possible match of the marked folder from + # the current path. + current_path=$trunc_path${current_path#${last_marked_folder}*} + fi ;; truncate_with_package_name) local name repo_path package_path current_dir zero @@ -801,11 +775,11 @@ prompt_dir() { # Remove trailing slash from git path, so that we can # remove that git path from the pwd. gitPath=${gitPath%/} - package_path=${current_path%%$gitPath} + package_path=${$(pwd)%%$gitPath} # Remove trailing slash - package_path=${package_dir%/} + package_path=${package_path%/} elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then - package_path=${current_path%%/.git*} + package_path=${$(pwd)%%/.git*} fi # Replace the shortest possible match of the marked folder from @@ -814,10 +788,11 @@ prompt_dir() { # in the path (this is done by the "zero" pattern; see # http://stackoverflow.com/a/40855342/5586433). local zero='%([BSUbfksu]|([FB]|){*})' + trunc_path=$(pwd) # Then, find the length of the package_path string, and save the # subdirectory path as a substring of the current directory's path from 0 # to the length of the package path's string - subdirectory_path=$(truncatePathFromRight "${current_path:${#${(S%%)package_path//$~zero/}}}") + subdirectory_path=$(truncatePath "${trunc_path:${#${(S%%)package_path//$~zero/}}}" $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER) # Parse the 'name' from the package.json; if there are any problems, just # print the file path defined POWERLEVEL9K_DIR_PACKAGE_FILES || POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json) @@ -838,8 +813,6 @@ prompt_dir() { # Instead of printing out the full path, print out the name of the package # from the package.json and append the current subdirectory current_path="`echo $packageName | tr -d '"'`$subdirectory_path" - else - current_path=$(truncatePathFromRight ${current_path//$HOME/"~"} ) fi ;; truncate_to_unique) -- cgit v1.2.3 From 115b656790bffef629539cc1d90c78502ad8606d Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 22:08:06 +0400 Subject: Forgot to update code when creating new func --- powerlevel9k.zsh-theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 64682fa3..682fbf3d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -724,6 +724,7 @@ set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_path="$(print -P '%~')" + local num_paths=(${(s:/:)current_path}) 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 @@ -738,7 +739,7 @@ prompt_dir() { current_path=$(truncatePath "$current_path" $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER) ;; truncate_with_folder_marker) - if (( ${#paths} > 0 )); then # root is an exception and won't have paths + if (( ${#num_paths} > 0 )); then # root is an exception and won't have paths local last_marked_folder marked_folder set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" @@ -818,7 +819,7 @@ prompt_dir() { truncate_to_unique) # for each parent path component find the shortest unique beginning # characters sequence. Source: https://stackoverflow.com/a/45336078 - if (( ${#paths} > 0 )); then # root is an exception and won't have paths + if (( ${#num_paths} > 0 )); then # root is an exception and won't have paths local matching local cur_path='/' [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' -- cgit v1.2.3 From a699393d58c1e9228dadd95e42ccf54f1b687dd2 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 22:12:16 +0400 Subject: Fixed error in coding Shouldn't code when tired lol. --- powerlevel9k.zsh-theme | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 682fbf3d..5357dab6 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -724,9 +724,8 @@ set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_path="$(print -P '%~')" - local num_paths=(${(s:/:)current_path}) 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 + (( ${#current_path} > 1 )) && num_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 set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026" local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character -- cgit v1.2.3 From 90d0fb1c3209b7f83d49b3a5a7a9168a4778491a Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 22:42:01 +0400 Subject: Updated test for trunc_to_unique Instead of testing for number of folders, now only tests if string is longer than 1 character. --- powerlevel9k.zsh-theme | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 5357dab6..e7cd4acf 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -725,7 +725,7 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_path="$(print -P '%~')" local paths directory test_dir test_dir_length trunc_path threshhold - (( ${#current_path} > 1 )) && num_paths=(${(s:/:)${current_path//"~\/"/}}) || paths=() # only split if not root/home folder + (( ${#current_path} > 1 )) && num_paths=(${(s:/:)${current_path//"~\/"/}}) || num_paths=() # only split if not root/home folder if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026" local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character @@ -818,7 +818,7 @@ prompt_dir() { truncate_to_unique) # for each parent path component find the shortest unique beginning # characters sequence. Source: https://stackoverflow.com/a/45336078 - if (( ${#num_paths} > 0 )); then # root is an exception and won't have paths + if (( ${#current_path} > 1 )); then # root and home are exceptions and won't have paths local matching local cur_path='/' [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' @@ -860,9 +860,9 @@ prompt_dir() { local current_state="DEFAULT" if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then current_state="NOT_WRITABLE" - elif [[ $current_dir == '~' ]]; then + elif [[ $current_path == '~' ]]; then current_state="HOME" - elif [[ $current_dir == '~'* ]]; then + elif [[ $current_path == '~'* ]]; then current_state="HOME_SUBFOLDER" fi -- cgit v1.2.3 From 9c02c6e4d8c7400c4a68a672548e8597a5d6c99f Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 22:46:53 +0400 Subject: All code should now be functional *Fingers crossed* --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index e7cd4acf..dfa6e4e8 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -725,7 +725,7 @@ set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { local current_path="$(print -P '%~')" local paths directory test_dir test_dir_length trunc_path threshhold - (( ${#current_path} > 1 )) && num_paths=(${(s:/:)${current_path//"~\/"/}}) || num_paths=() # only split if not root/home folder + (( ${#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 set_default POWERLEVEL9K_SHORTEN_DELIMITER "\u2026" local delim=$(echo -n $POWERLEVEL9K_SHORTEN_DELIMITER) # convert delimiter from unicode to literal character -- cgit v1.2.3 From 9ef8a2f1f9fac91678edec8fd74bcf73bda30a4a Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 22:51:28 +0400 Subject: Missed one variable name change There's always that one pesky variable name you forget to change... --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index dfa6e4e8..e65a82e0 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -738,7 +738,7 @@ prompt_dir() { current_path=$(truncatePath "$current_path" $POWERLEVEL9K_SHORTEN_DIR_LENGTH $POWERLEVEL9K_SHORTEN_DELIMITER) ;; truncate_with_folder_marker) - if (( ${#num_paths} > 0 )); then # root is an exception and won't have paths + if (( ${#paths} > 0 )); then # root is an exception and won't have paths local last_marked_folder marked_folder set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" -- cgit v1.2.3 From 441fb277d92a6e2310a71caa3920be1276a39afe Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 23:34:25 +0400 Subject: 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 --- powerlevel9k.zsh-theme | 52 ++++++++++++++++++++------- test/segments/dir.spec | 96 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 106 insertions(+), 42 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index e65a82e0..ebf13e66 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -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 diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 7d158389..e313c01c 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -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/po…st/1/12/123/1234/12…45/12…56/12…67/12…78/123456789%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black}/tmp/po…st/1/12/123/1234/12…45/12…56/12…67/12…78/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}tmpxXxpo…stxXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black}tmpxXxpo…stxXx1xXx2 %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}tmpxXxpo…xXx1xXx2%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black}tmpxXxpo…xXx1xXx2 %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 -- cgit v1.2.3 From 1e29203563716297c261a3d03406ac0446f55058 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 00:36:12 +0400 Subject: Removed final SEDs and more comments --- powerlevel9k.zsh-theme | 47 +++++++++++++++++++++++++++++++++-------------- test/powerlevel9k.spec | 14 +++++++------- 2 files changed, 40 insertions(+), 21 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index ebf13e66..2e713bdc 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -865,10 +865,6 @@ prompt_dir() { # 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]}" - fi - typeset -AH dir_states dir_states=( "DEFAULT" "FOLDER_ICON" @@ -887,30 +883,43 @@ prompt_dir() { # 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_on="%B"; bld_off="%b" || bld_on=""; bld_off="" - + # test if user wants the last directory printed in bold + if [[ "${(L)POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD}" == "true" ]]; then + bld_on="%B" + bld_off="%b" + else + bld_on="" + bld_off="" + fi + # determine is the user has set a last directory color local dir_state_user_foreground=POWERLEVEL9K_DIR_${current_state}_FOREGROUND local dir_state_foreground=${(P)dir_state_user_foreground} [[ -z ${dir_state_foreground} ]] && dir_state_foreground="${DEFAULT_COLOR}" local dir_name base_name + # use ZSH substitution to get the dirname and basename instead of calling external functions dir_name=${path_opt%/*} base_name=${path_opt##*/} + # if the user wants the last directory colored... if [[ -n ${POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND} ]]; then - if [[ $path_opt == "/" || $path_opt == "~" || "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then + # it the path is "/" or "~" + if [[ $path_opt == "/" || $path_opt == "~" ]]; then current_path="${bld_on}%F{$POWERLEVEL9K_DIR_PATH_HIGHLIGHT_FOREGROUND}${current_path}${bld_off}" - else + else # has a subfolder + # test if dirname != basename - they are equal if we use truncate_to_last or truncate_absolute 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 + else # no coloring + # it the path is "/" or "~" + if [[ $path_opt == "/" || $path_opt == "~" ]]; then current_path="${bld_on}${current_path}${bld_off}" - else + else # has a subfolder + # test if dirname != basename - they are equal if we use truncate_to_last or truncate_absolute if [[ $dir_name != $base_name ]]; then current_path="${dir_name}/${bld_on}${base_name}${bld_off}" else @@ -919,16 +928,26 @@ prompt_dir() { fi fi + # check if we need to omit the first character and only do it if we are not in "~" or "/" + if [[ "${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" && $path_opt != "/" && $path_opt != "~" ]]; then + current_path="${current_path[2,-1]}" + fi + + # check if the user wants the separator colored. if [[ -n ${POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND} && $path_opt != "/" ]]; then - current_path="$( echo "${current_path}" | sed "s/\//%F{$POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND}\/%F{$dir_state_foreground}/g")" + # because this contains color changing codes, it is easier to set a variable for what should be replaced + local repl="%F{$POWERLEVEL9K_DIR_PATH_SEPARATOR_FOREGROUND}/%F{$dir_state_foreground}" + # escape the / with a \ + current_path=${current_path//\//$repl} fi if [[ "${POWERLEVEL9K_DIR_PATH_SEPARATOR}" != "/" && $path_opt != "/" ]]; then - current_path="$( echo "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g")" + current_path=${current_path//\//$POWERLEVEL9K_DIR_PATH_SEPARATOR} fi if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" && ! "${(L)POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then - current_path="$( echo "${current_path}" | sed "s/~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}/1")" + # use :s to only replace the first occurance + current_path=${current_path:s/~/$POWERLEVEL9K_HOME_FOLDER_ABBREVIATION} fi "$1_prompt_segment" "$0_${current_state}" "$2" "blue" "$DEFAULT_COLOR" "${current_path}" "${dir_states[$current_state]}" diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 5d8c5a31..5f6b6f27 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -20,7 +20,7 @@ function testJoinedSegments() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_joined) cd /tmp - assertEquals "%K{blue} %F{black}/tmp%b %K{blue}%F{black}%F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black}/tmp %K{blue}%F{black}%F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS cd - @@ -30,7 +30,7 @@ function testTransitiveJoinedSegments() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir root_indicator_joined dir_joined) cd /tmp - assertEquals "%K{blue} %F{black}/tmp%b %K{blue}%F{black}%F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black}/tmp %K{blue}%F{black}%F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS cd - @@ -40,7 +40,7 @@ function testJoiningWithConditionalSegment() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir background_jobs dir_joined) cd /tmp - assertEquals "%K{blue} %F{black}/tmp%b %K{blue}%F{black} %F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black}/tmp %K{blue}%F{black} %F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS cd - @@ -51,7 +51,7 @@ function testDynamicColoringOfSegmentsWork() { POWERLEVEL9K_DIR_DEFAULT_BACKGROUND='red' cd /tmp - assertEquals "%K{red} %F{black}/tmp%b %k%F{red}%f " "$(build_left_prompt)" + assertEquals "%K{red} %F{black}/tmp %k%F{red}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_DIR_DEFAULT_BACKGROUND @@ -65,7 +65,7 @@ function testDynamicColoringOfVisualIdentifiersWork() { cd /tmp - assertEquals "%K{blue} %F{green%}icon-here%f %F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{green%}icon-here%f %F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_DIR_DEFAULT_VISUAL_IDENTIFIER_COLOR @@ -86,7 +86,7 @@ function testColoringOfVisualIdentifiersDoesNotOverwriteColoringOfSegment() { cd /tmp - assertEquals "%K{yellow} %F{green%}icon-here%f %F{red}/tmp%b %k%F{yellow}%f " "$(build_left_prompt)" + assertEquals "%K{yellow} %F{green%}icon-here%f %F{red}/tmp %k%F{yellow}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_DIR_DEFAULT_VISUAL_IDENTIFIER_COLOR @@ -106,7 +106,7 @@ function testOverwritingIconsWork() { #cd ~/$testFolder cd /tmp - assertEquals "%K{blue} %F{black%}icon-here%f %F{black}/tmp%b %k%F{blue}%f " "$(build_left_prompt)" + assertEquals "%K{blue} %F{black%}icon-here%f %F{black}/tmp %k%F{blue}%f " "$(build_left_prompt)" unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_DIR_FOLDER_ICON -- cgit v1.2.3 From 5fb3033d58ec4c87162b3e97a0116624bf87a424 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 00:41:36 +0400 Subject: Update to truncate_absolute --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 2e713bdc..10e9852d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -752,7 +752,7 @@ prompt_dir() { 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)} + current_path=$POWERLEVEL9K_SHORTEN_DELIMITER${current_path:(-POWERLEVEL9K_SHORTEN_DIR_LENGTH)} fi ;; truncate_with_folder_marker) -- cgit v1.2.3 From 08432bf70c1874ab4f67538f85e7be50847f1c5d Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 09:28:56 +0400 Subject: Added truncate_to_first_and_last strategy Truncate middle directories from the path. How many directories will be untouched is controlled by POWERLEVEL9K_SHORTER_DIR_LENGTH. --- README.md | 3 ++- powerlevel9k.zsh-theme | 71 ++++++++++++++++++++++++++++++-------------------- test/segments/dir.spec | 18 +++++++++++++ 3 files changed, 63 insertions(+), 29 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/README.md b/README.md index 6642b93f..1dbe756e 100644 --- a/README.md +++ b/README.md @@ -377,8 +377,9 @@ Customizations available are: |Default|Truncate whole directories from left. How many is defined by `POWERLEVEL9K_SHORTEN_DIR_LENGTH`| |`truncate_middle`|Truncates the middle part of a folder. E.g. you are in a folder named `~/MySuperProjects/AwesomeFiles/BoringOffice`, then it will truncated to `~/MyS..cts/Awe..les/BoringOffice`, if `POWERLEVEL9K_SHORTEN_DIR_LENGTH=3` is also set (controls the amount of characters to be left).| |`truncate_from_right`|Just leaves the beginning of a folder name untouched. E.g. your folders will be truncated like so: "/ro../Pr../office". How many characters will be untouched is controlled by `POWERLEVEL9K_SHORTEN_DIR_LENGTH`.| -|`truncate_to_last`|Truncates everything before the last folder in the path.| |`truncate_absolute`|Truncates everything exept the last few characters in the path. E.g. if you are in a folder named "~/Projects/powerlevel9k" and you have set `POWERLEVEL9K_SHORTEN_DIR_LENGTH=3`, you will get "..l9k".| +|`truncate_to_last`|Truncates everything before the last folder in the path.| +|`truncate_to_first_and_last|Truncate middle directories from the path. How many directories will be untouched is controlled by POWERLEVEL9K_SHORTER_DIR_LENGTH. E.g. if you are in a folder named "~/Projects/powerlevel9k" and you have set `POWERLEVEL9K_SHORTEN_DIR_LENGTH=1`, you will get "~/../powerlevel9k".|| |`truncate_to_unique`|Parse all parent path components and truncate them to the shortest unique length. If you copy & paste the result to a shell, after hitting `TAB` it should expand to the original path unambiguously.| |`truncate_with_package_name`|Search for a `package.json` or `composer.json` and prints the `name` field to abbreviate the directory path. The precedence and/or files could be set by `POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json)`. If you have [jq](https://stedolan.github.io/jq/) installed, it will dramatically improve the speed of this strategy.| |`truncate_with_folder_marker`|Search for a file that is specified by `POWERLEVEL9K_SHORTEN_FOLDER_MARKER` and truncate everything before that (if found, otherwise stop on $HOME and ROOT).| diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 10e9852d..17909c61 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -728,7 +728,7 @@ prompt_dir() { local state_path=$current_path # declare all local variables local paths directory test_dir test_dir_length trunc_path threshhold - # if we are not in "~" or "/", split the paths into an array + # if we are not in "~" or "/", split the paths into an array and exclude "~" (( ${#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 @@ -745,16 +745,54 @@ prompt_dir() { # 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_to_last) + # truncate all characters before the current directory + current_path=${current_path##*/} + ;; + truncate_to_first_and_last) + if (( ${#current_path} > 1 )) && (( ${POWERLEVEL9K_SHORTEN_DIR_LENGTH} > 0 )); then + threshhold=$(( ${POWERLEVEL9K_SHORTEN_DIR_LENGTH} * 2)) + # if we are in "~", add it back into the paths array + [[ $current_path == '~'* ]] && paths=("~" "${paths[@]}") + if (( ${#paths} > $threshhold )); then + local num=$(( ${#paths} - ${POWERLEVEL9K_SHORTEN_DIR_LENGTH} )) + # repace the middle elements + for (( i=$POWERLEVEL9K_SHORTEN_DIR_LENGTH; i<$num; i++ )); do + paths[$i+1]=$POWERLEVEL9K_SHORTEN_DELIMITER + done + current_path="${(j:/:)paths}" + fi + fi + ;; + truncate_to_unique) + # 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 cur_path='/' + [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' + 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 + [[ $current_path == "~"* ]] && trunc_path="~/$trunc_path" + current_path="${trunc_path: : -1}" + fi + ;; truncate_with_folder_marker) if (( ${#paths} > 0 )); then # root and home are exceptions and won't have paths, so skip this local last_marked_folder marked_folder @@ -833,29 +871,6 @@ prompt_dir() { current_path="`echo $packageName | tr -d '"'`$subdirectory_path" fi ;; - truncate_to_unique) - # 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 cur_path='/' - [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' - 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 - [[ $current_path == "~"* ]] && trunc_path="~/$trunc_path" - current_path="${trunc_path: : -1}" - fi - ;; *) current_path="$(print -P "%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c")" ;; diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 8c9554d7..3e7f11d4 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -90,6 +90,24 @@ function testTruncateToLastWorks() { unset POWERLEVEL9K_SHORTEN_STRATEGY } +function testTruncateToFirstAndLastWorks() { + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY="truncate_to_first_and_last" + + FOLDER=/tmp/powerlevel9k-test/1/12/123/1234/12345/123456/1234567/12345678/123456789 + mkdir -p $FOLDER + cd $FOLDER + + assertEquals "%K{blue} %F{black}/tmp/12/…/12345678/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_absolute" -- cgit v1.2.3 From 99e62532bb1cab11df5f86d021014930a94bed0d Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 09:53:36 +0400 Subject: Added test for root based folder to first&last --- powerlevel9k.zsh-theme | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 17909c61..051ffb8f 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -766,7 +766,8 @@ prompt_dir() { for (( i=$POWERLEVEL9K_SHORTEN_DIR_LENGTH; i<$num; i++ )); do paths[$i+1]=$POWERLEVEL9K_SHORTEN_DELIMITER done - current_path="${(j:/:)paths}" + [[ $current_path ~= '~'* ]] && current_path="/" || current_path="" + current_path+="${(j:/:)paths}" fi fi ;; -- cgit v1.2.3 From b723800b801f8bfc1aa997eea1f03d4279f96f3e Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 09:56:42 +0400 Subject: Fixed typo - ~= instead of != --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 051ffb8f..d60219eb 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -766,7 +766,7 @@ prompt_dir() { for (( i=$POWERLEVEL9K_SHORTEN_DIR_LENGTH; i<$num; i++ )); do paths[$i+1]=$POWERLEVEL9K_SHORTEN_DELIMITER done - [[ $current_path ~= '~'* ]] && current_path="/" || current_path="" + [[ $current_path != '~'* ]] && current_path="/" || current_path="" current_path+="${(j:/:)paths}" fi fi -- cgit v1.2.3 From ddcdad77a73d971a77283d90fd3ba78e0f61dc66 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 10:08:43 +0400 Subject: Added new variable POWERLEVEL9K_DIR_PATH_ABSOLUTE If user sets POWERLEVEL9K_DIR_PATH_ABSOLUTE to true, uses absolute paths instead of home folder abbreviation, e.g. /Users/chris/... instead of ~/... --- powerlevel9k.zsh-theme | 9 ++++++--- test/segments/dir.spec | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index d60219eb..c404c0c2 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -723,7 +723,10 @@ set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" set_default POWERLEVEL9K_DIR_PATH_HIGHLIGHT_BOLD false prompt_dir() { - local current_path="$(print -P '%~')" + # using $PWD instead of "$(print -P '%~')" to allow use of POWERLEVEL9K_DIR_PATH_ABSOLUTE + local current_path=$PWD # WAS: local current_path="$(print -P '%~')" + # check if the user wants to use absolute paths or "~" paths + [[ ${(L)POWERLEVEL9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path//$HOME/"~"} # 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 @@ -891,9 +894,9 @@ prompt_dir() { local current_state="DEFAULT" if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then current_state="NOT_WRITABLE" - elif [[ $state_path == '~' ]]; then + elif [[ $state_path == $HOME ]]; then # changed '~' to $HOME for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE current_state="HOME" - elif [[ $state_path == '~'* ]]; then + elif [[ $state_path == $HOME* ]]; then # changed '~'* to $HOME* for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE current_state="HOME_SUBFOLDER" fi diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 12242961..efe99672 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -18,6 +18,16 @@ function tearDown() { unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS } +function testAbsolutePathWorks() { + POWERLEVEL9K_DIR_PATH_ABSOLUTE=true + + cd ~ + assertEquals "%K{blue} %F{black}…/12345678/123456789 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + unset POWERLEVEL9K_DIR_PATH_ABSOLUTE +} + function testTruncateFoldersWorks() { POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 POWERLEVEL9K_SHORTEN_STRATEGY='truncate_folders' -- cgit v1.2.3 From 5cec4d652a7428bfc8900f84be0bb6885e2e12ef Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 10:21:02 +0400 Subject: Added POWERLEVEL9K_DIR_PATH_ABSOLUTE to README.md --- README.md | 1 + powerlevel9k.zsh-theme | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/README.md b/README.md index 1dbe756e..26298b18 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,7 @@ Customizations available are: | Variable | Default Value | Description | |----------|---------------|-------------| +|`POWERLEVEL9K_DIR_PATH_ABSOLUTE`|None|If set to `true`, will use absolute paths instead of home folder abbreviation `~`| |`POWERLEVEL9K_SHORTEN_DIR_LENGTH`|`2`|If your shorten strategy, below, is entire directories, this field determines how many directories to leave at the end. If your shorten strategy is by character count, this field determines how many characters to allow per directory string.| |`POWERLEVEL9K_SHORTEN_STRATEGY`|None|How the directory strings should be truncated. See the table below for more informations.| |`POWERLEVEL9K_SHORTEN_DELIMITER`|`..`|Delimiter to use in truncated strings. This can be any string you choose, including an empty string if you wish to have no delimiter.| diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c404c0c2..baf46336 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -894,9 +894,9 @@ prompt_dir() { local current_state="DEFAULT" if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then current_state="NOT_WRITABLE" - elif [[ $state_path == $HOME ]]; then # changed '~' to $HOME for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE + elif [[ "$state_path" == "$HOME" ]]; then # changed '~' to $HOME for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE current_state="HOME" - elif [[ $state_path == $HOME* ]]; then # changed '~'* to $HOME* for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE + elif [[ "$state_path" == "$HOME"* ]]; then # changed '~'* to $HOME* for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE current_state="HOME_SUBFOLDER" fi -- cgit v1.2.3 From 9b7f8babe7f7eb4cd4330c1ba4534150baa90b82 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 23 Feb 2018 10:25:56 +0400 Subject: Updated testing for folder icons --- powerlevel9k.zsh-theme | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'powerlevel9k.zsh-theme') diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index baf46336..8dd2d957 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -727,8 +727,6 @@ prompt_dir() { local current_path=$PWD # WAS: local current_path="$(print -P '%~')" # check if the user wants to use absolute paths or "~" paths [[ ${(L)POWERLEVEL9K_DIR_PATH_ABSOLUTE} != "true" ]] && current_path=${current_path//$HOME/"~"} - # 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 # if we are not in "~" or "/", split the paths into an array and exclude "~" @@ -891,12 +889,13 @@ prompt_dir() { "HOME_SUBFOLDER" "HOME_SUB_ICON" "NOT_WRITABLE" "LOCK_ICON" ) + local state_path="$(print -P '%~')" local current_state="DEFAULT" if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then current_state="NOT_WRITABLE" - elif [[ "$state_path" == "$HOME" ]]; then # changed '~' to $HOME for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE + elif [[ $state_path == '~' ]]; then current_state="HOME" - elif [[ "$state_path" == "$HOME"* ]]; then # changed '~'* to $HOME* for compatibility with POWERLEVEL9K_DIR_PATH_ABSOLUTE + elif [[ $state_path == '~'* ]]; then current_state="HOME_SUBFOLDER" fi -- cgit v1.2.3