Skip to content

Commit

Permalink
Use visible string length (fixes #134)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlanCosman committed Sep 22, 2021
1 parent 3787c72 commit b9fda6b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
7 changes: 3 additions & 4 deletions functions/_tide_prompt.fish
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ 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
printf '%s' $right_prompt[1] \n $left_prompt[2]' '
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
Expand Down
23 changes: 10 additions & 13 deletions functions/_tide_pwd.fish
Original file line number Diff line number Diff line change
@@ -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' '
Expand All @@ -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
Expand All @@ -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

0 comments on commit b9fda6b

Please sign in to comment.