From 0fd8055dd0c7f755d39743f5588d9ac4f6461019 Mon Sep 17 00:00:00 2001 From: Ilan Cosman Date: Thu, 26 Aug 2021 13:47:43 -0700 Subject: [PATCH] Use visible string length (fixes #134) --- functions/_tide_prompt.fish | 7 +++---- functions/_tide_pwd.fish | 23 ++++++++++------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/functions/_tide_prompt.fish b/functions/_tide_prompt.fish index 35b6a345..c3a02ef0 100644 --- a/functions/_tide_prompt.fish +++ b/functions/_tide_prompt.fish @@ -11,8 +11,8 @@ function _tide_prompt echo $right_prompt[2] - set -lx dist_btwn_sides (math $COLUMNS + 5 - ( # Regex removes color. 5 = @PWD@ length which will be replaced - string replace -ar '\e(\[[\d;]*|\(B\e\[)m(\co)?' '' "$left_prompt[1]""$right_prompt[1]" | string length)) + # 5 = @PWD@ length which will be replaced + set -lx dist_btwn_sides (math $COLUMNS + 5 - (string length --visible "$left_prompt[1]""$right_prompt[1]")) printf '%s' (string replace @PWD@ (_tide_pwd) "$left_prompt[1]") $_tide_prompt_and_frame_color string repeat --no-newline --max (math max 0, $dist_btwn_sides - $pwd_length) $tide_prompt_icon_connection @@ -20,8 +20,7 @@ function _tide_prompt else echo $right_prompt[1] - set -lx dist_btwn_sides (math $COLUMNS + 5 -$tide_prompt_min_cols - ( - string replace -ar '\e(\[[\d;]*|\(B\e\[)m(\co)?' '' "$left_prompt[1]""$right_prompt[1]" | string length)) + set -lx dist_btwn_sides (math $COLUMNS + 5 -$tide_prompt_min_cols - (string length --visible "$left_prompt[1]""$right_prompt[1]")) string replace @PWD@ (_tide_pwd) "$left_prompt[1] " end end diff --git a/functions/_tide_pwd.fish b/functions/_tide_pwd.fish index b1ad9263..79d0e90e 100644 --- a/functions/_tide_pwd.fish +++ b/functions/_tide_pwd.fish @@ -1,14 +1,5 @@ function _tide_pwd set -l split_pwd (string replace -- $HOME '~' $PWD | string split /) - set -l split_pwd_for_length $split_pwd - - # Anchor first and last directories (which may be the same) - if test -n "$split_pwd[1]" # ~/foo/bar, hightlight ~ - set split_pwd_for_output $_tide_color_anchors$split_pwd[1]$_tide_reset_to_color_dirs $split_pwd[2..] - else # /foo/bar, hightlight foo not empty string - set split_pwd_for_output '' $_tide_color_anchors$split_pwd[2]$_tide_reset_to_color_dirs $split_pwd[3..] - end - set split_pwd_for_output[-1] $_tide_color_anchors$split_pwd[-1]$_tide_reset_to_color_dirs if not test -w $PWD set -g tide_pwd_icon $tide_pwd_icon_unwritable' ' @@ -18,7 +9,15 @@ function _tide_pwd set -g tide_pwd_icon $tide_pwd_icon' ' end - set -g pwd_length (string length "$tide_pwd_icon"(string join / $split_pwd_for_length)) + # Anchor first and last directories (which may be the same) + if test -n "$split_pwd[1]" # ~/foo/bar, hightlight ~ + set split_pwd_for_output "$_tide_reset_to_color_dirs$tide_pwd_icon"$_tide_color_anchors$split_pwd[1]$_tide_reset_to_color_dirs $split_pwd[2..] + else # /foo/bar, hightlight foo not empty string + set split_pwd_for_output "$_tide_reset_to_color_dirs$tide_pwd_icon"'' $_tide_color_anchors$split_pwd[2]$_tide_reset_to_color_dirs $split_pwd[3..] + end + set split_pwd_for_output[-1] $_tide_color_anchors$split_pwd[-1]$_tide_reset_to_color_dirs + + string join / $split_pwd_for_output | string length --visible | read -g pwd_length i=1 for dir_section in $split_pwd[2..-2] set -l parent_dir (string join -- / $split_pwd[..$i] | string replace '~' $HOME) # Uses i before increment @@ -33,12 +32,10 @@ function _tide_pwd set -l truncated (string sub --length $truncation_length -- $dir_section) && test $truncated != $dir_section -a (count $parent_dir/$truncated*/) -gt 1 end - set split_pwd_for_length[$i] $truncated set split_pwd_for_output[$i] $_tide_color_truncated_dirs$truncated$_tide_reset_to_color_dirs - set pwd_length (string length "$tide_pwd_icon"(string join / $split_pwd_for_length)) # Update length + string join / $split_pwd_for_output | string length --visible | read -g pwd_length end end - printf '%s' $_tide_reset_to_color_dirs $tide_pwd_icon string join -- / $split_pwd_for_output end