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 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'functions') 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() { -- cgit v1.2.3 From 4996e955e02108eadcb57a5cc1e181dd2c008734 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 22:18:59 +0400 Subject: Updated path splitting line Works in the console, but Travis doesn't like it. --- functions/utilities.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 9f1ea53e..a539fc6e 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -234,7 +234,7 @@ function truncatePath() { # 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}) + local paths=(${(s:/:)${1}}) # declare locals for the directory being tested and its length local test_dir test_dir_length # do the needed truncation -- cgit v1.2.3 From 04726d21ef4acb46cd995eb21ad631632a09f6ff Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 23:52:07 +0400 Subject: Changed path splitting to test Travis --- functions/utilities.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index a539fc6e..c59652f5 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -232,9 +232,9 @@ function truncatePath() { # 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}}) + [[ $1 == "~"* ]] && trunc_path='' || trunc_path='/' + # split the path into an array using "/" as the delimiter + local paths=(${(s:/:)1}) # declare locals for the directory being tested and its length local test_dir test_dir_length # do the needed truncation -- cgit v1.2.3 From f10a7daab02af4b23e161627bba3850823f3b678 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Mon, 19 Feb 2018 23:59:41 +0400 Subject: Another change to test Travis --- functions/utilities.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index c59652f5..5babee2f 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -231,10 +231,10 @@ function truncatePath() { [[ -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='/' + # if the path is in the home folder, add "~/" to the start otherwise "/" + [[ $1 == "~"* ]] && trunc_path='~/' || trunc_path='/' # split the path into an array using "/" as the delimiter - local paths=(${(s:/:)1}) + local paths=(${(s:/:)${1//"~\/"/}}) # declare locals for the directory being tested and its length local test_dir test_dir_length # do the needed truncation -- cgit v1.2.3 From d31ac26caa359c867f2f82d6fbfee2bb72cf22cc Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Tue, 20 Feb 2018 01:14:06 +0400 Subject: Another Travis test --- functions/utilities.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 5babee2f..fb1cc276 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -234,7 +234,8 @@ function truncatePath() { # if the path is in the home folder, add "~/" to the start otherwise "/" [[ $1 == "~"* ]] && trunc_path='~/' || trunc_path='/' # split the path into an array using "/" as the delimiter - local paths=(${(s:/:)${1//"~\/"/}}) + local paths=$1 + paths=(${(s:/:)${paths//"~\/"/}}) # declare locals for the directory being tested and its length local test_dir test_dir_length # do the needed truncation -- cgit v1.2.3 From 2214124327849f51b3e78d2eacb0cd77e117fed7 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 22:17:26 +0400 Subject: Updated truncatePath() Added variable `delim_len` with test when delim="" --- functions/utilities.zsh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index fb1cc276..7dee844c 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -234,15 +234,16 @@ function truncatePath() { # if the path is in the home folder, add "~/" to the start otherwise "/" [[ $1 == "~"* ]] && trunc_path='~/' || trunc_path='/' # split the path into an array using "/" as the delimiter - local paths=$1 - paths=(${(s:/:)${paths//"~\/"/}}) + local paths=(${(s:/:)${1//"~\/"/}}) # declare locals for the directory being tested and its length - local test_dir test_dir_length + local test_dir test_dir_length delim_len # do the needed truncation case $4 in right) + # check the length of the delimiter + [[ -z $3 ]] && delim_len=${#3} || delim_len=0 # include the delimiter length in the threshhold - local threshhold=$(( $2 + ${#3} )) + local threshhold=$(( $2 + $delim_len )) # loop through the paths for (( i=1; i<${#paths}; i++ )); do # get the current directory value @@ -261,7 +262,7 @@ function truncatePath() { ;; middle) # we need double the length for start and end truncation + delimiter length - local threshhold=$(( $2 * 2 + ${#3} )) + local threshhold=$(( $2 * 2 )) # create a variable for the start of the end truncation local last_pos # loop through the paths @@ -271,7 +272,7 @@ function truncatePath() { 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 + if (( $test_dir_length > $threshhold )); 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}/" -- cgit v1.2.3 From ec0f7bdacbd2be5214588ca7c0b63f8b2697e2fd Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 22:22:04 +0400 Subject: Update for Travis While zsh accepts the following code: ``` local paths=(${(s:/:)${1//"~\/"/}}) ``` Travis fails unless it is ``` local paths=$1 paths=(${(s:/:)${paths//"~\/"/}}) ``` --- functions/utilities.zsh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 7dee844c..1b2035ac 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -234,7 +234,8 @@ function truncatePath() { # if the path is in the home folder, add "~/" to the start otherwise "/" [[ $1 == "~"* ]] && trunc_path='~/' || trunc_path='/' # split the path into an array using "/" as the delimiter - local paths=(${(s:/:)${1//"~\/"/}}) + local paths=$1 + paths=(${(s:/:)${paths//"~\/"/}}) # declare locals for the directory being tested and its length local test_dir test_dir_length delim_len # do the needed truncation -- cgit v1.2.3 From 2bd3e0f67ee8c4f78cbcf20b6c5c391650ebf5c8 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 22 Feb 2018 22:33:43 +0400 Subject: Fixed silly coding error Did `local 3=...` instead of `3=...` --- functions/utilities.zsh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'functions') diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 1b2035ac..b4173b99 100644 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -226,7 +226,7 @@ function truncatePath() { # convert $2 from string to integer 2=$(( $2 )) # set $3 to "" if not defined - [[ -z $3 ]] && local 3="" || 3=$(echo -n $3) + [[ -z $3 ]] && 3="" || 3=$(echo -n $3) # set $4 to "right" if not defined [[ -z $4 ]] && 4="right" # create a variable for the truncated path. @@ -237,14 +237,12 @@ function truncatePath() { local paths=$1 paths=(${(s:/:)${paths//"~\/"/}}) # declare locals for the directory being tested and its length - local test_dir test_dir_length delim_len + local test_dir test_dir_length # do the needed truncation case $4 in right) - # check the length of the delimiter - [[ -z $3 ]] && delim_len=${#3} || delim_len=0 # include the delimiter length in the threshhold - local threshhold=$(( $2 + $delim_len )) + local threshhold=$(( $2 + ${#3} )) # loop through the paths for (( i=1; i<${#paths}; i++ )); do # get the current directory value -- cgit v1.2.3