From bcb0fed873221588bfd5ba101588d7f646c54ca0 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 21 Jan 2017 14:30:19 +0100 Subject: [PATCH 01/22] Avoid error if CLOBBER is not set If the tempfile already exists and CLOBBER is not set, a file exists error occurrs. This commit avoids this problem. --- powerlevel9k.zsh-theme | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..0df17de0 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -469,6 +469,11 @@ prompt_battery() { fi } +# Public IP segment +# Parameters: +# * $1 Alignment: string - left|right +# * $2 Index: integer +# * $3 Joined: bool - If the segment should be joined prompt_public_ip() { # set default values for segment set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300" @@ -531,12 +536,17 @@ prompt_public_ip() { fi # write IP to tmp file or clear tmp file if an IP was not retrieved - [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE > $POWERLEVEL9K_PUBLIC_IP_FILE + # Redirection with `>!`. From the manpage: Same as >, except that the file + # is truncated to zero length if it exists, even if CLOBBER is unset. + # If the file already exists, and a simple `>` redirection and CLOBBER + # unset, ZSH will produce an error. + [[ -n "${fresh_ip}" ]] && echo $fresh_ip >! $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE >! $POWERLEVEL9K_PUBLIC_IP_FILE fi # read public IP saved to tmp file local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) + # Draw the prompt segment if [[ -n $public_ip ]]; then $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON' fi From a576a157599ef3b41b939c4e8dc5df8656c30023 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 25 Jan 2017 23:47:20 +0100 Subject: [PATCH 02/22] Fix error with truncating package name if repo is symlinked --- powerlevel9k.zsh-theme | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..38c2ceb4 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -587,7 +587,14 @@ prompt_dir() { # Get the path of the Git repo, which should have the package.json file if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == "true" ]]; then - package_path=$(git rev-parse --show-toplevel) + # Get path from the root of the git repository to the current dir + local gitPath=$(git rev-parse --show-prefix) + # Remove trailing slash from git path, so that we can + # remove that git path from the pwd. + gitPath=${gitPath%/} + package_path=${$(pwd)%%$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*} fi From 6c352c8d5c90c189166beb07e3f9f7c753f6b79d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 28 Jan 2017 01:29:38 +0100 Subject: [PATCH 03/22] Simplify `public_ip` segment --- powerlevel9k.zsh-theme | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..f1e4314b 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -475,6 +475,7 @@ prompt_public_ip() { set_default POWERLEVEL9K_PUBLIC_IP_NONE "" set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip" set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" + defined POWERLEVEL9K_PUBLIC_IP_METHODS || POWERLEVEL9K_PUBLIC_IP_METHODS=(wget curl dig) # Do we need a fresh IP? local refresh_ip=false @@ -491,51 +492,33 @@ prompt_public_ip() { fi # grab a fresh IP if needed + local fresh_ip if [[ $refresh_ip =~ true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then - # if method specified, don't use fallback methods - if [[ -n $POWERLEVEL9K_PUBLIC_IP_METHOD ]] && [[ $POWERLEVEL9K_PUBLIC_IP_METHOD =~ 'wget|curl|dig' ]]; then - local method=$POWERLEVEL9K_PUBLIC_IP_METHOD - fi - if [[ -n $method ]]; then + for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do case $method in 'dig') - if type -p dig >/dev/null; then - fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" - [[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip - fi + fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" + [[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip ;; 'curl') - if [[ -z "$fresh_ip" ]] && type -p curl >/dev/null; then - fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi + fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" ;; 'wget') - if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then - fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi + fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" ;; esac - else - if type -p dig >/dev/null; then - fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" - [[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip + # If we found a fresh IP, break loop. + if [[ -n "${fresh_ip}" ]]; then + break; fi - - if [[ -z "$fresh_ip" ]] && type -p curl >/dev/null; then - fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi - - if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then - fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi - fi + done # write IP to tmp file or clear tmp file if an IP was not retrieved [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE > $POWERLEVEL9K_PUBLIC_IP_FILE fi # read public IP saved to tmp file - local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) + local public_ip="$(cat $POWERLEVEL9K_PUBLIC_IP_FILE)" if [[ -n $public_ip ]]; then $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON' From 06cb9bb43fd6a5010fafa116fabe8290f87a90f0 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 28 Jan 2017 13:21:40 +0100 Subject: [PATCH 04/22] Change order of public ip methods as it was before --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f1e4314b..61d9f99c 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -475,7 +475,7 @@ prompt_public_ip() { set_default POWERLEVEL9K_PUBLIC_IP_NONE "" set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip" set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" - defined POWERLEVEL9K_PUBLIC_IP_METHODS || POWERLEVEL9K_PUBLIC_IP_METHODS=(wget curl dig) + defined POWERLEVEL9K_PUBLIC_IP_METHODS || POWERLEVEL9K_PUBLIC_IP_METHODS=(dig curl wget) # Do we need a fresh IP? local refresh_ip=false From bfbfd5c10cc45792f260e76fb919deb26f5d5f2a Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 28 Jan 2017 13:29:08 +0100 Subject: [PATCH 05/22] Fix README entry for renamed variable POWERLEVEL9K_PUBLIC_IP_METHODS --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7a80a09..518cda0c 100644 --- a/README.md +++ b/README.md @@ -348,7 +348,7 @@ segment will not be displayed. |`POWERLEVEL9K_PUBLIC_IP_FILE`|'/tmp/p8k_public_ip'|This is the file your public IP is cached in.| |`POWERLEVEL9K_PUBLIC_IP_HOST`|'http://ident.me'|This is the default host to get your public IP.| |`POWERLEVEL9K_PUBLIC_IP_TIMEOUT`|300|The amount of time in seconds between refreshing your cached IP.| -|`POWERLEVEL9K_PUBLIC_IP_METHOD`|None|You can set this to any of 'dig', 'curl', or 'wget' to only use that method to refresh your IP.| +|`POWERLEVEL9K_PUBLIC_IP_METHODS`|(dig curl wget)| These methods in that order are used to refresh your IP.| |`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained| ##### rbenv From 52eb3136264e7ea9894a7dc150d320ad47590c3d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 29 Jan 2017 14:34:32 +0100 Subject: [PATCH 06/22] Fix theme for use with plain "prompt" command ZSH has the ability to preview and set themes directly from the `prompt` builtin. To do so, the theme must be in FPATH and named "prompt_theme_setup". --- prompt_powerlevel9k_setup | 1 + 1 file changed, 1 insertion(+) create mode 120000 prompt_powerlevel9k_setup diff --git a/prompt_powerlevel9k_setup b/prompt_powerlevel9k_setup new file mode 120000 index 00000000..311575f4 --- /dev/null +++ b/prompt_powerlevel9k_setup @@ -0,0 +1 @@ +powerlevel9k.zsh-theme \ No newline at end of file From b379f03f148d99a0da8c8eb200f17b3007a87e47 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 30 Jan 2017 21:10:09 +0100 Subject: [PATCH 07/22] Add ability to omit the first character in dir path --- README.md | 7 +++++-- powerlevel9k.zsh-theme | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f7a80a09..04632323 100644 --- a/README.md +++ b/README.md @@ -304,9 +304,12 @@ the path shown would be `my-cool-project`. If you navigate to `$HOME/projects/m If you want to customize the directory separator, you could set: ```zsh -# You'll need patched awesome-terminal fonts for that example -POWERLEVEL9K_DIR_PATH_SEPARATOR="%f "$'\uE0B1'" %F" +# Double quotes are important here! +POWERLEVEL9K_DIR_PATH_SEPARATOR="%F{red} $(print_icon 'LEFT_SUBSEGMENT_SEPARATOR') %F{black}" ``` +To omit the first character (usually a slash that gets replaced if you set `POWERLEVEL9K_DIR_PATH_SEPARATOR`), +you could set `POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true`. + ##### disk_usage diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..3010790e 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -616,6 +616,10 @@ prompt_dir() { esac fi + if [[ "${POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER}" == "true" ]]; then + current_path="${current_path[2,-1]}" + fi + if [[ "${POWERLEVEL9K_DIR_PATH_SEPARATOR}" != "/" ]]; then current_path=$(print -P "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g") fi From ebf808c533ed86ede4b5afcf9206c05b832278db Mon Sep 17 00:00:00 2001 From: Benoit Averty Date: Wed, 10 Aug 2016 00:07:04 +0200 Subject: [PATCH 08/22] Add dir_truncate_root option --- functions/utilities.zsh | 15 +++++++++++++++ powerlevel9k.zsh-theme | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index f27c7f99..8263ce10 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -208,3 +208,18 @@ function truncatePathFromRight() { echo $1 | sed $SED_EXTENDED_REGEX_PARAMETER \ "s@(([^/]{$((POWERLEVEL9K_SHORTEN_DIR_LENGTH))})([^/]{$delim_len}))[^/]+/@\2$POWERLEVEL9K_SHORTEN_DELIMITER/@g" } + +# Search recursively in parent folders for given file. +function upsearch () { + if test -e "$1"; then + echo "$PWD" + else + if [[ "$PWD" == "/" || "$PWD" == "$HOME" ]]; then + echo "$PWD"; + else + pushd .. > /dev/null + upsearch "$1" + popd > /dev/null + fi + fi +} diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..c679ba56 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -571,8 +571,7 @@ prompt_custom() { set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" prompt_dir() { local current_path='%~' - if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" ]]; then - + if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_root_marker" ]]; then set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\U2026' case "$POWERLEVEL9K_SHORTEN_STRATEGY" in @@ -610,6 +609,20 @@ prompt_dir() { current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) fi ;; + truncate_with_root_marker) + local dir_truncate_root + + dir_truncate_root=$(upsearch $POWERLEVEL9K_ROOT_MARKER_FILE) + zero='%([BSUbfksu]|([FB]|){*})' + + if [[ "$dir_truncate_root" == "/" || "$dir_truncate_root" == "$HOME" ]]; then + current_path='%~' + elif [[ "$dir_truncate_root" == "$HOME"* ]]; then + current_path="~/$POWERLEVEL9K_SHORTEN_DELIMITER/${dir_truncate_root##*/}${PWD:${#${(S%%)dir_truncate_root//$~zero/}}}" + else + current_path="/$POWERLEVEL9K_SHORTEN_DELIMITER/${dir_truncate_root##*/}${PWD:${#${(S%%)dir_truncate_root//$~zero/}}}" + fi + ;; *) current_path="%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c" ;; From 5cc2d51b5829d0ae2eefaa115892da71f6a14d79 Mon Sep 17 00:00:00 2001 From: Benoit Averty Date: Wed, 10 Aug 2016 13:00:52 +0200 Subject: [PATCH 09/22] Fix bug when truncate root was set to direct subfolder of home or root --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c679ba56..afc4a6bc 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -615,7 +615,7 @@ prompt_dir() { dir_truncate_root=$(upsearch $POWERLEVEL9K_ROOT_MARKER_FILE) zero='%([BSUbfksu]|([FB]|){*})' - if [[ "$dir_truncate_root" == "/" || "$dir_truncate_root" == "$HOME" ]]; then + if [[ "$dir_truncate_root" == "/" || "$dir_truncate_root" == "$HOME" || "${dir_truncate_root%/*}" == "$HOME" || ${dir_truncate_root%/*} == "/" ]]; then current_path='%~' elif [[ "$dir_truncate_root" == "$HOME"* ]]; then current_path="~/$POWERLEVEL9K_SHORTEN_DELIMITER/${dir_truncate_root##*/}${PWD:${#${(S%%)dir_truncate_root//$~zero/}}}" From 9f4772f9930c7d56ab04df900ac94456e5a08a22 Mon Sep 17 00:00:00 2001 From: Benoit Averty Date: Wed, 10 Aug 2016 21:31:58 +0200 Subject: [PATCH 10/22] More possibilities with truncate folder marker --- functions/utilities.zsh | 17 +++++++++-------- powerlevel9k.zsh-theme | 30 ++++++++++++++++++------------ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 8263ce10..22b53c6a 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -211,15 +211,16 @@ function truncatePathFromRight() { # Search recursively in parent folders for given file. function upsearch () { - if test -e "$1"; then + if [[ "$PWD" == "$HOME" || "$PWD" == "/" ]]; then + echo "$PWD" + elif test -e "$1"; then + pushd .. > /dev/null + upsearch "$1" + popd > /dev/null echo "$PWD" else - if [[ "$PWD" == "/" || "$PWD" == "$HOME" ]]; then - echo "$PWD"; - else - pushd .. > /dev/null - upsearch "$1" - popd > /dev/null - fi + pushd .. > /dev/null + upsearch "$1" + popd > /dev/null fi } diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index afc4a6bc..420f7d0b 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -571,7 +571,7 @@ prompt_custom() { set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" prompt_dir() { local current_path='%~' - if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_root_marker" ]]; then + 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 @@ -609,19 +609,25 @@ prompt_dir() { current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) fi ;; - truncate_with_root_marker) - local dir_truncate_root + truncate_with_folder_marker) + local last_marked_folder marked_folder zero + set_default POWERLEVEL9K_SHORTEN_FOLDER_MARKER ".shorten_folder_marker" + + for marked_folder in $(upsearch $POWERLEVEL9K_SHORTEN_FOLDER_MARKER); do + if [[ "$marked_folder" == "/" ]]; then + current_path="/" + elif [[ "$marked_folder" == "$HOME" ]]; then + 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 - dir_truncate_root=$(upsearch $POWERLEVEL9K_ROOT_MARKER_FILE) zero='%([BSUbfksu]|([FB]|){*})' - - if [[ "$dir_truncate_root" == "/" || "$dir_truncate_root" == "$HOME" || "${dir_truncate_root%/*}" == "$HOME" || ${dir_truncate_root%/*} == "/" ]]; then - current_path='%~' - elif [[ "$dir_truncate_root" == "$HOME"* ]]; then - current_path="~/$POWERLEVEL9K_SHORTEN_DELIMITER/${dir_truncate_root##*/}${PWD:${#${(S%%)dir_truncate_root//$~zero/}}}" - else - current_path="/$POWERLEVEL9K_SHORTEN_DELIMITER/${dir_truncate_root##*/}${PWD:${#${(S%%)dir_truncate_root//$~zero/}}}" - fi + current_path=$current_path${PWD:${#${(S%%)last_marked_folder//$~zero/}}} ;; *) current_path="%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c" From 6927b0ca94157e2fb9dcd3b12766b4e913d0e44e Mon Sep 17 00:00:00 2001 From: Benoit Averty Date: Tue, 9 Aug 2016 11:22:02 +0200 Subject: [PATCH 11/22] Add directory permission prompt segment --- functions/icons.zsh | 3 +++ powerlevel9k.zsh-theme | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/functions/icons.zsh b/functions/icons.zsh index df92d22a..44d99095 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -78,6 +78,7 @@ case $POWERLEVEL9K_MODE in PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' PUBLIC_IP_ICON '' + LOCK_ICON $'\UE138' #  ) ;; 'awesome-fontconfig') @@ -138,6 +139,7 @@ case $POWERLEVEL9K_MODE in PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' PUBLIC_IP_ICON '' + LOCK_ICON $'\UE138' #  ) ;; *) @@ -198,6 +200,7 @@ case $POWERLEVEL9K_MODE in PYTHON_ICON '' SWIFT_ICON 'Swift' PUBLIC_IP_ICON '' + LOCK_ICON $'\UE0A2' ) ;; esac diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..620a5f7d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -1114,6 +1114,13 @@ prompt_swift_version() { "$1_prompt_segment" "$0" "$2" "magenta" "white" "${swift_version}" 'SWIFT_ICON' } +# dir_permision: Display information about the user's permission to write in the current directory +prompt_dir_permission() { + if [[ ! -w "$PWD" ]]; then + "$1_prompt_segment" "$0_FORBIDDEN" "$2" "red" "226" "" 'LOCK_ICON' + fi +} + ################################################################ # Prompt processing and drawing ################################################################ @@ -1240,4 +1247,3 @@ prompt_powerlevel9k_setup() { } prompt_powerlevel9k_setup "$@" - From 052eba9c74b2153dbd70eb4ba829f258a596baf2 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 31 Jan 2017 23:57:59 +0100 Subject: [PATCH 12/22] Rename `dir_permissions` segment to `dir_writable` Additionally: - Add changelog entry - Add documentation --- CHANGELOG.md | 4 ++++ README.md | 1 + powerlevel9k.zsh-theme | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index defc1dd0..5db1f6e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ Added an option to configure the path separator. If you want something else than an ordinary slash, you could set `POWERLEVEL9K_DIR_PATH_SEPARATOR` to whatever you want. +### New segment 'dir_writable' added + +This segment displays a lock icon if your user has no write permissions in the current folder. + ### New segment 'disk_usage' added This segment will show the usage level of your current partition. diff --git a/README.md b/README.md index f7a80a09..8f606f6b 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ The segments that are currently available are: * [`battery`](#battery) - Current battery status. * [`context`](#context) - Your username and host. * [`dir`](#dir) - Your current working directory. +* `dir_writable` - Displays a lock icon, if you do not have write permissions on the current folder. * [`disk_usage`](#disk_usage) - Disk usage of your current partition. * `history` - The command number for the current line. * [`ip`](#ip) - Shows the current IP address. diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 620a5f7d..b89f390c 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -1114,8 +1114,8 @@ prompt_swift_version() { "$1_prompt_segment" "$0" "$2" "magenta" "white" "${swift_version}" 'SWIFT_ICON' } -# dir_permision: Display information about the user's permission to write in the current directory -prompt_dir_permission() { +# dir_writable: Display information about the user's permission to write in the current directory +prompt_dir_writable() { if [[ ! -w "$PWD" ]]; then "$1_prompt_segment" "$0_FORBIDDEN" "$2" "red" "226" "" 'LOCK_ICON' fi From eb865664e4225b94fc01ce1c05f27d1db18e172d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Thu, 2 Feb 2017 00:31:04 +0100 Subject: [PATCH 13/22] Add comments to the dir segment --- powerlevel9k.zsh-theme | 2 ++ 1 file changed, 2 insertions(+) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 420f7d0b..723954be 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -615,8 +615,10 @@ prompt_dir() { 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##*/}" From ed9d18f1de5d5e0142f40bea452ddc3749967baa Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Thu, 2 Feb 2017 00:31:36 +0100 Subject: [PATCH 14/22] Add documentation for dir truncation strategies --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f7a80a09..99f1d6e2 100644 --- a/README.md +++ b/README.md @@ -278,9 +278,17 @@ Customizations available are: | Variable | Default Value | Description | |----------|---------------|-------------| |`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. By default, it will truncate whole directories. Other options are `truncate_middle`, which leaves the start and end of the directory strings, and `truncate_from_right`, which cuts starting from the end of the string. You can also use `truncate_with_package_name` to use the `package.json` `name` field to abbreviate the directory path.| +|`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.| +| Strategy Name | Description | +|---------------|-------------| +|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_with_package_name`|Use the `package.json` `name` field to abbreviate the directory path.| +|`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).| + For example, if you wanted the truncation behavior of the `fish` shell, which truncates `/usr/share/plasma` to `/u/s/plasma`, you would use the following: ```zsh From 268c3247c95eb11c92054465404e37d8877c0f5d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 3 Feb 2017 20:21:53 +0100 Subject: [PATCH 15/22] Do path expansion at first --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3010790e..3e529b3b 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -570,7 +570,7 @@ prompt_custom() { # Dir: current working directory set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" prompt_dir() { - local current_path='%~' + local current_path="$(print -P "%~")" if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" ]]; then set_default POWERLEVEL9K_SHORTEN_DELIMITER $'\U2026' @@ -621,7 +621,7 @@ prompt_dir() { fi if [[ "${POWERLEVEL9K_DIR_PATH_SEPARATOR}" != "/" ]]; then - current_path=$(print -P "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g") + current_path="$( echo "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g")" fi typeset -AH dir_states From a8af89eeb7db3980f9bef14a96828fc8c8bdb854 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 4 Feb 2017 08:51:52 +0100 Subject: [PATCH 16/22] Fix default dir truncation strategy --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 3e529b3b..37bdbbb8 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -611,7 +611,7 @@ prompt_dir() { fi ;; *) - current_path="%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c" + current_path="$(print -P "%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c")" ;; esac fi From 3d451499aaa62f1d01e1d5fb3dd0e274ebcf5ad2 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 4 Feb 2017 08:53:35 +0100 Subject: [PATCH 17/22] Fix tests --- test/powerlevel9k.spec | 74 +++++++++++++++++++++++++++--------------- test/segments/dir.spec | 27 +++++++-------- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 1aab2c5f..5f6b6f27 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -10,86 +10,108 @@ function setUp() { # Load Powerlevel9k source powerlevel9k.zsh-theme source functions/* + + # Unset mode, so that user settings + # do not interfere with tests + unset POWERLEVEL9K_MODE } function testJoinedSegments() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_joined) + cd /tmp - assertEquals "%K{blue} %F{black}%~ %K{blue}%F{black}%F{black}%~ %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 - } function testTransitiveJoinedSegments() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir root_indicator_joined dir_joined) + cd /tmp - assertEquals "%K{blue} %F{black}%~ %K{blue}%F{black}%F{black}%~ %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 - } function testJoiningWithConditionalSegment() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir background_jobs dir_joined) + cd /tmp - assertEquals "%K{blue} %F{black}%~ %K{blue}%F{black} %F{black}%~ %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 - } function testDynamicColoringOfSegmentsWork() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) - POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND='red' + POWERLEVEL9K_DIR_DEFAULT_BACKGROUND='red' + cd /tmp - assertEquals "%K{red} %F{black}%~ %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_HOME_SUBFOLDER_BACKGROUND + unset POWERLEVEL9K_DIR_DEFAULT_BACKGROUND + cd - } function testDynamicColoringOfVisualIdentifiersWork() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) - POWERLEVEL9K_MODE='awesome-patched' - POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR='green' + POWERLEVEL9K_DIR_DEFAULT_VISUAL_IDENTIFIER_COLOR='green' + POWERLEVEL9K_FOLDER_ICON="icon-here" - # Re-Source the icons, as the POWERLEVEL9K_MODE is directly - # evaluated there. - source functions/icons.zsh + cd /tmp - assertEquals "%K{blue} %F{green%}%f %F{black}%~ %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_MODE - unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR + unset POWERLEVEL9K_DIR_DEFAULT_VISUAL_IDENTIFIER_COLOR + unset POWERLEVEL9K_FOLDER_ICON + cd - } function testColoringOfVisualIdentifiersDoesNotOverwriteColoringOfSegment() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) - POWERLEVEL9K_MODE='awesome-patched' - POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR='green' - POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND='red' - POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND='yellow' + POWERLEVEL9K_DIR_DEFAULT_VISUAL_IDENTIFIER_COLOR='green' + POWERLEVEL9K_DIR_DEFAULT_FOREGROUND='red' + POWERLEVEL9K_DIR_DEFAULT_BACKGROUND='yellow' + POWERLEVEL9K_FOLDER_ICON="icon-here" # Re-Source the icons, as the POWERLEVEL9K_MODE is directly # evaluated there. source functions/icons.zsh - assertEquals "%K{yellow} %F{green%}%f %F{red}%~ %k%F{yellow}%f " "$(build_left_prompt)" + cd /tmp + + 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_MODE - unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_VISUAL_IDENTIFIER_COLOR - unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND - unset POWERLEVEL9K_DIR_HOME_SUBFOLDER_BACKGROUND + unset POWERLEVEL9K_DIR_DEFAULT_VISUAL_IDENTIFIER_COLOR + unset POWERLEVEL9K_DIR_DEFAULT_FOREGROUND + unset POWERLEVEL9K_DIR_DEFAULT_BACKGROUND + unset POWERLEVEL9K_FOLDER_ICON + cd - } function testOverwritingIconsWork() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) - POWERLEVEL9K_HOME_SUB_ICON='icon-here' + POWERLEVEL9K_FOLDER_ICON='icon-here' + #local testFolder=$(mktemp -d -p p9k) + # Move testFolder under home folder + #mv testFolder ~ + # Go into testFolder + #cd ~/$testFolder - assertEquals "%K{blue} %F{black%}icon-here%f %F{black}%~ %k%F{blue}%f " "$(build_left_prompt)" + cd /tmp + 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_HOME_SUB_ICON + unset POWERLEVEL9K_DIR_FOLDER_ICON + cd - + # rm -fr ~/$testFolder } source shunit2/source/2.1/src/shunit2 diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 840a298b..a5378ab6 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -9,10 +9,16 @@ function setUp() { export TERM="xterm-256color" # Load Powerlevel9k source powerlevel9k.zsh-theme + + # Every test should at least use the dir segment + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) +} + +function tearDown() { + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS } function testTruncateFoldersWorks() { - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 POWERLEVEL9K_SHORTEN_STRATEGY='truncate_folders' @@ -20,19 +26,17 @@ function testTruncateFoldersWorks() { mkdir -p $FOLDER cd $FOLDER - assertEquals "%K{blue} %F{black}%3(c:…/:)%2c %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 unset FOLDER - unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_SHORTEN_DIR_LENGTH unset POWERLEVEL9K_SHORTEN_STRATEGY } function testTruncateMiddleWorks() { - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 POWERLEVEL9K_SHORTEN_STRATEGY='truncate_middle' @@ -47,12 +51,10 @@ function testTruncateMiddleWorks() { unset FOLDER unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS - unset POWERLEVEL9K_SHORTEN_DIR_LENGTH unset POWERLEVEL9K_SHORTEN_STRATEGY } function testTruncationFromRightWorks() { - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 POWERLEVEL9K_SHORTEN_STRATEGY='truncate_from_right' @@ -66,52 +68,45 @@ function testTruncationFromRightWorks() { rm -fr /tmp/powerlevel9k-test unset FOLDER - unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_SHORTEN_DIR_LENGTH unset POWERLEVEL9K_SHORTEN_STRATEGY } function testHomeFolderDetectionWorks() { - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_HOME_ICON='home-icon' cd ~ - assertEquals "%K{blue} %F{black%}home-icon%f %F{black}%~ %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_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_HOME_ICON } function testHomeSubfolderDetectionWorks() { - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_HOME_SUB_ICON='sub-icon' FOLDER=~/powerlevel9k-test mkdir $FOLDER cd $FOLDER - assertEquals "%K{blue} %F{black%}sub-icon%f %F{black}%~ %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 unset FOLDER - unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_HOME_SUB_ICON } function testOtherFolderDetectionWorks() { - POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_FOLDER_ICON='folder-icon' FOLDER=/tmp/powerlevel9k-test mkdir $FOLDER cd $FOLDER - assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}%~ %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 unset FOLDER - unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS unset POWERLEVEL9K_FOLDER_ICON } From b6e55c189e6a7958164304cf5ffd7766ffe8f3bf Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 4 Feb 2017 09:13:02 +0100 Subject: [PATCH 18/22] Add tests for omitting first character in `dir` segment --- test/segments/dir.spec | 103 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/test/segments/dir.spec b/test/segments/dir.spec index a5378ab6..747ab385 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -110,4 +110,107 @@ function testOtherFolderDetectionWorks() { unset POWERLEVEL9K_FOLDER_ICON } +function testChangingDirPathSeparator() { + POWERLEVEL9K_DIR_PATH_SEPARATOR='xXx' + local FOLDER="/tmp/powerlevel9k-test/1/2" + mkdir -p $FOLDER + cd $FOLDER + + assertEquals "%K{blue} %F{black}xXxtmpxXxpowerlevel9k-testxXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + unset FOLDER + rm -fr /tmp/powerlevel9k-test + unset POWERLEVEL9K_DIR_PATH_SEPARATOR +} + +function testOmittingFirstCharacterWorks() { + POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true + POWERLEVEL9K_FOLDER_ICON='folder-icon' + cd /tmp + + assertEquals "%K{blue} %F{black%}folder-icon%f %F{black}tmp %k%F{blue}%f " "$(build_left_prompt)" + + cd - + unset POWERLEVEL9K_FOLDER_ICON + unset POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER +} + +function testOmittingFirstCharacterWorksWithChangingPathSeparator() { + POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true + POWERLEVEL9K_DIR_PATH_SEPARATOR='xXx' + POWERLEVEL9K_FOLDER_ICON='folder-icon' + 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 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + unset POWERLEVEL9K_FOLDER_ICON + unset POWERLEVEL9K_DIR_PATH_SEPARATOR + unset POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER +} + +# This test makes it obvious that combining a truncation strategy +# that cuts off folders from the left and omitting the the first +# character does not make much sense. The truncation strategy +# comes first, prints an ellipsis and that gets then cut off by +# POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER.. +# But it does more sense in combination with other truncation +# strategies. +function testOmittingFirstCharacterWorksWithChangingPathSeparatorAndDefaultTruncation() { + POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true + POWERLEVEL9K_DIR_PATH_SEPARATOR='xXx' + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY='truncate_folders' + mkdir -p /tmp/powerlevel9k-test/1/2 + cd /tmp/powerlevel9k-test/1/2 + + assertEquals "%K{blue} %F{black}xXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + unset POWERLEVEL9K_DIR_PATH_SEPARATOR + unset POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_SHORTEN_STRATEGY +} + +function testOmittingFirstCharacterWorksWithChangingPathSeparatorAndMiddleTruncation() { + POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true + POWERLEVEL9K_DIR_PATH_SEPARATOR='xXx' + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY='truncate_middle' + mkdir -p /tmp/powerlevel9k-test/1/2 + cd /tmp/powerlevel9k-test/1/2 + + assertEquals "%K{blue} %F{black}tmpxXxpo…stxXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + unset POWERLEVEL9K_DIR_PATH_SEPARATOR + unset POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_SHORTEN_STRATEGY +} + +function testOmittingFirstCharacterWorksWithChangingPathSeparatorAndRightTruncation() { + POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true + POWERLEVEL9K_DIR_PATH_SEPARATOR='xXx' + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY='truncate_from_right' + mkdir -p /tmp/powerlevel9k-test/1/2 + cd /tmp/powerlevel9k-test/1/2 + + assertEquals "%K{blue} %F{black}tmpxXxpo…xXx1xXx2 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr /tmp/powerlevel9k-test + unset POWERLEVEL9K_DIR_PATH_SEPARATOR + unset POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_SHORTEN_STRATEGY +} + source shunit2/source/2.1/src/shunit2 From 8e99944797b171b84bd347cc8b9c065d7298d25c Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 5 Feb 2017 14:34:56 +0100 Subject: [PATCH 19/22] Add test for truncate_with_folder_marker truncation strategy --- test/segments/dir.spec | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 840a298b..0f253ac3 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -71,6 +71,52 @@ function testTruncationFromRightWorks() { unset POWERLEVEL9K_SHORTEN_STRATEGY } +function testTruncateWithFolderMarkerWorks() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY="truncate_with_folder_marker" + + local BASEFOLDER=/tmp/powerlevel9k-test + local FOLDER=$BASEFOLDER/1/12/123/1234/12345/123456/1234567 + mkdir -p $FOLDER + # Setup folder marker + touch $BASEFOLDER/1/12/.shorten_folder_marker + cd $FOLDER + assertEquals "%K{blue} %F{black}/…/12/123/1234/12345/123456/1234567 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr $BASEFOLDER + unset BASEFOLDER + unset FOLDER + unset POWERLEVEL9K_SHORTEN_STRATEGY + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS +} + +function testTruncateWithFolderMarkerWithChangedFolderMarker() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 + POWERLEVEL9K_SHORTEN_STRATEGY="truncate_with_folder_marker" + POWERLEVEL9K_SHORTEN_FOLDER_MARKER='.xxx' + + local BASEFOLDER=/tmp/powerlevel9k-test + local FOLDER=$BASEFOLDER/1/12/123/1234/12345/123456/1234567 + mkdir -p $FOLDER + # Setup folder marker + touch $BASEFOLDER/1/12/.xxx + cd $FOLDER + assertEquals "%K{blue} %F{black}/…/12/123/1234/12345/123456/1234567 %k%F{blue}%f " "$(build_left_prompt)" + + cd - + rm -fr $BASEFOLDER + unset BASEFOLDER + unset FOLDER + unset POWERLEVEL9K_SHORTEN_FOLDER_MARKER + unset POWERLEVEL9K_SHORTEN_STRATEGY + unset POWERLEVEL9K_SHORTEN_DIR_LENGTH + unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS +} + function testHomeFolderDetectionWorks() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) POWERLEVEL9K_HOME_ICON='home-icon' From a4cb875615bedc305168c694793616de8d72c1b3 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 12 Feb 2017 23:27:16 +0100 Subject: [PATCH 20/22] Add documentation about mysterious 'BSUbfksu' pattern --- powerlevel9k.zsh-theme | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index d51ec8f2..e6befc6d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -591,7 +591,12 @@ prompt_dir() { package_path=${$(pwd)%%/.git*} fi - zero='%([BSUbfksu]|([FB]|){*})' + # Replace the shortest possible match of the marked folder from + # the current path. Remove the amount of characters up to the + # folder marker from the left. Count only the visible characters + # 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 @@ -613,6 +618,9 @@ prompt_dir() { local last_marked_folder marked_folder zero 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. @@ -628,7 +636,12 @@ prompt_dir() { last_marked_folder=$marked_folder done - zero='%([BSUbfksu]|([FB]|){*})' + # Replace the shortest possible match of the marked folder from + # the current path. Remove the amount of characters up to the + # folder marker from the left. Count only the visible characters + # in the path (this is done by the "zero" pattern; see + # http://stackoverflow.com/a/40855342/5586433). + local zero='%([BSUbfksu]|([FB]|){*})' current_path=$current_path${PWD:${#${(S%%)last_marked_folder//$~zero/}}} ;; *) From f6c65e3cf7b87a07b7dffb0c3d77a6932241c242 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 13 Feb 2017 00:18:34 +0100 Subject: [PATCH 21/22] Get rid of unreadable BSU-pattern Now the current folder in `truncate_with_folder_marker` strategy is truncated by cutting off the found folder from the beginning of $PWD. --- powerlevel9k.zsh-theme | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index e6befc6d..55e33680 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -615,7 +615,7 @@ prompt_dir() { fi ;; truncate_with_folder_marker) - local last_marked_folder marked_folder zero + 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 @@ -637,12 +637,8 @@ prompt_dir() { done # Replace the shortest possible match of the marked folder from - # the current path. Remove the amount of characters up to the - # folder marker from the left. Count only the visible characters - # in the path (this is done by the "zero" pattern; see - # http://stackoverflow.com/a/40855342/5586433). - local zero='%([BSUbfksu]|([FB]|){*})' - current_path=$current_path${PWD:${#${(S%%)last_marked_folder//$~zero/}}} + # the current path. + current_path=$current_path${PWD#${last_marked_folder}*} ;; *) current_path="%$((POWERLEVEL9K_SHORTEN_DIR_LENGTH+1))(c:$POWERLEVEL9K_SHORTEN_DELIMITER/:)%${POWERLEVEL9K_SHORTEN_DIR_LENGTH}c" From 6e1259ee99a51a8cb52f975ebb45ede2792a4730 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Mon, 13 Feb 2017 00:20:01 +0100 Subject: [PATCH 22/22] Remove unnecessary variable from tests --- test/segments/dir.spec | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/segments/dir.spec b/test/segments/dir.spec index 0f253ac3..69359e25 100755 --- a/test/segments/dir.spec +++ b/test/segments/dir.spec @@ -73,7 +73,6 @@ function testTruncationFromRightWorks() { function testTruncateWithFolderMarkerWorks() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) - POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 POWERLEVEL9K_SHORTEN_STRATEGY="truncate_with_folder_marker" local BASEFOLDER=/tmp/powerlevel9k-test @@ -89,13 +88,11 @@ function testTruncateWithFolderMarkerWorks() { unset BASEFOLDER unset FOLDER unset POWERLEVEL9K_SHORTEN_STRATEGY - unset POWERLEVEL9K_SHORTEN_DIR_LENGTH unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS } function testTruncateWithFolderMarkerWithChangedFolderMarker() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) - POWERLEVEL9K_SHORTEN_DIR_LENGTH=2 POWERLEVEL9K_SHORTEN_STRATEGY="truncate_with_folder_marker" POWERLEVEL9K_SHORTEN_FOLDER_MARKER='.xxx' @@ -113,7 +110,6 @@ function testTruncateWithFolderMarkerWithChangedFolderMarker() { unset FOLDER unset POWERLEVEL9K_SHORTEN_FOLDER_MARKER unset POWERLEVEL9K_SHORTEN_STRATEGY - unset POWERLEVEL9K_SHORTEN_DIR_LENGTH unset POWERLEVEL9K_LEFT_PROMPT_ELEMENTS }