diff --git a/README.md b/README.md index cc68ecbe..286f7ffe 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ The segments that are currently available are: **System Status Segments:** * [`background_jobs`](#background_jobs) - Indicator for background jobs. * [`battery`](#battery) - Current battery status. -* [`context`](#context) - Your username and host. +* [`context`](#context) - Your username and host, conditionalized based on $USER and SSH status. * [`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. @@ -102,6 +102,7 @@ The segments that are currently available are: * `swap` - Prints the current swap size. * [`time`](#time) - System time. * [`vi_mode`](#vi_mode)- Your prompt's Vi editing mode (NORMAL|INSERT). +* `ssh` - Indicates whether or not you are in an SSH session. **Development Environment Segments:** * [`vcs`](#vcs) - Information about this `git` or `hg` repository (if you are in one). @@ -243,24 +244,26 @@ main theme distribution so that everyone can use it! ##### context -The `context` segment (user@host string) is conditional. This lets you enable -it, but only display it if you are not your normal user or on a remote host -(basically, only print it when it's likely you need it). +The `context` segment (user@host string) is conditional. By default, it will +only print if you are not your 'normal' user (including if you are root), or if +you are SSH'd to a remote host. To use this feature, make sure the `context` segment is enabled in your prompt elements (it is by default), and define a `DEFAULT_USER` in your `~/.zshrc`: You can set the `POWERLEVEL9K_CONTEXT_HOST_DEPTH` variable to change how the hostname is displayed. See (ZSH Manual)[http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Login-information] -for details. Default is set to %m which will show the hostname up to the first ‘.’ +for details. The default is set to %m which will show the hostname up to the first ‘.’ You can set it to %{N}m where N is an integer to show that many segments of system hostname. Setting N to a negative integer will show that many segments from the end of the hostname. | Variable | Default Value | Description | |----------|---------------|-------------| -|`DEFAULT_USER`|None|Username to consider a "default context" (you can also use `$USER`)| -|`POWERLEVEL9K_CONTEXT_HOST_DEPTH`|%m|Customizable host depth on prompt| +|`DEFAULT_USER`|None|Username to consider a "default context".| +|`POWERLEVEL9K_CONTEXT_HOST_DEPTH`|%m|Customizable host depth on prompt.| +|`POWERLEVEL9K_ALWAYS_SHOW_CONTEXT`|false|Always show this segment, including $USER and hostname.| +|`POWERLEVEL9K_ALWAYS_SHOW_USER`|false|Always show the username, but conditionalize the hostname.| ##### dir @@ -285,9 +288,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 @@ -311,9 +322,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/functions/icons.zsh b/functions/icons.zsh index d8232f07..f0aec351 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -80,6 +80,7 @@ case $POWERLEVEL9K_MODE in PUBLIC_IP_ICON '' LOCK_ICON $'\UE138' #  EXECUTION_TIME_ICON $'\UE89C' #  + SSH_ICON '(ssh)' ) ;; 'awesome-fontconfig') @@ -142,6 +143,7 @@ case $POWERLEVEL9K_MODE in PUBLIC_IP_ICON '' LOCK_ICON $'\UE138' #  EXECUTION_TIME_ICON $'\uF253' + SSH_ICON '(ssh)' ) ;; *) @@ -204,6 +206,7 @@ case $POWERLEVEL9K_MODE in PUBLIC_IP_ICON '' LOCK_ICON $'\UE0A2' EXECUTION_TIME_ICON 'Dur' + SSH_ICON '(ssh)' ) ;; esac diff --git a/functions/utilities.zsh b/functions/utilities.zsh index f27c7f99..22b53c6a 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -208,3 +208,19 @@ 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 [[ "$PWD" == "$HOME" || "$PWD" == "/" ]]; then + echo "$PWD" + elif test -e "$1"; then + pushd .. > /dev/null + upsearch "$1" + popd > /dev/null + echo "$PWD" + else + pushd .. > /dev/null + upsearch "$1" + popd > /dev/null + fi +} diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 77c9592f..39454b59 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -537,16 +537,34 @@ prompt_public_ip() { # Context: user@hostname (who am I and where am I) # Note that if $DEFAULT_USER is not set, this prompt segment will always print +set_default POWERLEVEL9K_ALWAYS_SHOW_CONTEXT false +set_default POWERLEVEL9K_ALWAYS_SHOW_USER false +set_default POWERLEVEL9K_CONTEXT_HOST_DEPTH "%m" prompt_context() { - set_default POWERLEVEL9K_CONTEXT_HOST_DEPTH "%m" - if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then - if [[ $(print -P "%#") == '#' ]]; then - # Shell runs as root - "$1_prompt_segment" "$0_ROOT" "$2" "$DEFAULT_COLOR" "yellow" "$USER@$POWERLEVEL9K_CONTEXT_HOST_DEPTH" - else - "$1_prompt_segment" "$0_DEFAULT" "$2" "$DEFAULT_COLOR" "011" "$USER@$POWERLEVEL9K_CONTEXT_HOST_DEPTH" - fi + local current_state="DEFAULT" + typeset -AH context_states + context_states=( + "ROOT" "yellow" + "DEFAULT" "011" + ) + + local content="" + + if [[ "$POWERLEVEL9K_ALWAYS_SHOW_CONTEXT" == true ]] || [[ "$USER" != "$DEFAULT_USER" ]] || [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then + + if [[ $(print -P "%#") == '#' ]]; then + current_state="ROOT" + fi + + content="$USER@${POWERLEVEL9K_CONTEXT_HOST_DEPTH}" + + elif [[ "$POWERLEVEL9K_ALWAYS_SHOW_USER" == true ]]; then + content="$USER" + else + return fi + + "$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${context_states[$current_state]}" "${content}" } # The 'custom` prompt provides a way for users to invoke commands and display @@ -596,9 +614,8 @@ prompt_command_execution_time() { # Dir: current working directory set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" prompt_dir() { - local current_path='%~' - if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" ]]; then - + 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 @@ -625,7 +642,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 @@ -643,14 +665,44 @@ prompt_dir() { current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$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${PWD#${last_marked_folder}*} + ;; *) - 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 + 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") + current_path="$( echo "${current_path}" | sed "s/\//${POWERLEVEL9K_DIR_PATH_SEPARATOR}/g")" fi typeset -AH dir_states @@ -920,6 +972,12 @@ prompt_rvm() { fi } +prompt_ssh() { + if [[ -n "$SSH_CLIENT" ]] || [[ -n "$SSH_TTY" ]]; then + "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "" 'SSH_ICON' + fi +} + # Status: return code if verbose, otherwise just an icon if an error occurred set_default POWERLEVEL9K_STATUS_VERBOSE true set_default POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE false 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..ce6869b7 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,53 +68,191 @@ 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() { +function testTruncateWithFolderMarkerWorks() { POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + 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_LEFT_PROMPT_ELEMENTS +} + +function testTruncateWithFolderMarkerWithChangedFolderMarker() { + POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) + 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_LEFT_PROMPT_ELEMENTS +} + +function testHomeFolderDetectionWorks() { 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 } +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