New pwd logic with markers

This commit is contained in:
Ilan Cosman 2021-01-10 08:56:46 -08:00
parent 4129099cd2
commit e555aad9e0
5 changed files with 31 additions and 33 deletions

View File

@ -1,56 +1,50 @@
function _tide_item_pwd
set -l preMaxDirsSplitPwd (string replace $HOME '~' $PWD | string split '/')
set -l splitPwd $preMaxDirsSplitPwd[(math --scale=0 -$tide_pwd_max_dirs)..-1]
set -l pwd (string join '/' $splitPwd)
set -l splitPwd $preMaxDirsSplitPwd[(math -$tide_pwd_max_dirs)..-1]
set -l splitPwdForLength $splitPwd
set -l splitPwdForOutput $splitPwd
set -l gitDir (git rev-parse --show-toplevel 2>/dev/null)
# Compute anchorDirs
contains 'first' $tide_pwd_anchors && if test -n "$splitPwd[1]"
set -a anchorDirs $splitPwd[1]
set -l anchorDirs
contains first $tide_pwd_anchors && if test -n "$splitPwd[1]"
set -a anchorDirs 1
else
set -a anchorDirs $splitPwd[2]
set -a anchorDirs 2
end
contains 'last' $tide_pwd_anchors && set -a anchorDirs $splitPwd[-1]
contains 'git' $tide_pwd_anchors && set -a anchorDirs (string split -r -m1 '/' "$gitDir")[2]
contains last $tide_pwd_anchors && set -a anchorDirs (count $splitPwd)
set -l colorDirs (set_color $tide_pwd_color_dirs || echo)
set -l colorAnchors (set_color -o $tide_pwd_color_anchors || echo)
set -l keepBackgroundColor (set_color normal -b $tide_pwd_bg_color || echo)
set -l colorTruncatedDirs (set_color $tide_pwd_color_truncated_dirs || echo)
set -g _tide_pwd_output $colorDirs$pwd
# Prepend icons
if not test -w $PWD
set -p _tide_pwd_output $colorDirs$tide_pwd_unwritable_icon
printf '%s' $colorDirs $tide_pwd_unwritable_icon' '
else if test $PWD = $HOME
set -p _tide_pwd_output $colorDirs$tide_pwd_home_icon
printf '%s' $colorDirs $tide_pwd_home_icon' '
else
set -p _tide_pwd_output $colorDirs$tide_pwd_dir_icon
printf '%s' $colorDirs $tide_pwd_dir_icon' '
end
set -l truncatedList $splitPwd '.'
# Empty string will cause an issue for the while loop
test -z "$splitPwd[1]" && set -e truncatedList[1]
set -l pwdMaxLength (math $COLUMNS -$tide_pwd_truncate_margin)
for dir in $splitPwd
if contains $dir $anchorDirs
set _tide_pwd_output (string replace $dir $colorAnchors$dir$keepBackgroundColor $_tide_pwd_output)
else if test (string length $pwd) -gt $pwdMaxLength
set -l dirTruncated $dir
set -l truncationLength 1
test (string length $dirTruncated) -gt $truncationLength && while contains $dirTruncated $truncatedList
set dirTruncated (string sub -l $truncationLength $dir)
set truncationLength (math $truncationLength+1)
end
set -a truncatedList $dirTruncated
for i in (seq (count $splitPwd))
set -l parentDir (string join '/' $splitPwd[1..(math $i-1)])
set pwd (string replace $dir $dirTruncated $pwd)
set _tide_pwd_output (string replace $dir $colorTruncatedDirs$dirTruncated $_tide_pwd_output)
# This line returns true if any markers exist in parentDir, or if anchorDirs contains i
if eval test -z thisPartIsFalse "-o -e $parentDir"/$splitPwd[$i]/$tide_pwd_markers || contains $i $anchorDirs
set splitPwdForOutput[$i] $colorAnchors$splitPwd[$i]
else if test (string join '/' $splitPwdForLength | string length) -gt $pwdMaxLength
set -l truncationLength 1
while set -l truncated (string sub --length $truncationLength $splitPwd[$i])
test (string length $truncated) -lt (string length $splitPwdForLength[$i]) || break
eval set -l truncatedPath $parentDir/$truncated
test (count $truncatedPath*/) -gt 1 || break
set truncationLength (math $truncationLength + 1)
end
set splitPwdForLength[$i] $truncated
set splitPwdForOutput[$i] $colorTruncatedDirs$truncated
end
end
string replace --all '/' $colorDirs'/' $_tide_pwd_output
string join $keepBackgroundColor$colorDirs'/' $splitPwdForOutput
end

View File

@ -61,6 +61,7 @@ tide_pwd_color_dirs $_tide_color_dark_blue
tide_pwd_color_truncated_dirs 8787AF
tide_pwd_dir_icon
tide_pwd_home_icon
tide_pwd_markers .bzr .citc .git .hg .node-version .python-version .ruby-version .shorten_folder_marker .svn .terraform Cargo.toml composer.json CVS go.mod package.json
tide_pwd_max_dirs 0
tide_pwd_truncate_margin 10
tide_pwd_unwritable_icon ''

View File

@ -61,6 +61,7 @@ tide_pwd_color_dirs $_tide_color_dark_blue
tide_pwd_color_truncated_dirs 8787AF
tide_pwd_dir_icon
tide_pwd_home_icon
tide_pwd_markers .bzr .citc .git .hg .node-version .python-version .ruby-version .shorten_folder_marker .svn .terraform Cargo.toml composer.json CVS go.mod package.json
tide_pwd_max_dirs 0
tide_pwd_truncate_margin 10
tide_pwd_unwritable_icon ''

View File

@ -61,6 +61,7 @@ tide_pwd_color_dirs blue
tide_pwd_color_truncated_dirs 8787AF
tide_pwd_dir_icon
tide_pwd_home_icon
tide_pwd_markers
tide_pwd_max_dirs 0
tide_pwd_truncate_margin 10
tide_pwd_unwritable_icon

View File

@ -61,6 +61,7 @@ tide_pwd_color_dirs E4E4E4
tide_pwd_color_truncated_dirs BCBCBC
tide_pwd_dir_icon
tide_pwd_home_icon
tide_pwd_markers .bzr .citc .git .hg .node-version .python-version .ruby-version .shorten_folder_marker .svn .terraform Cargo.toml composer.json CVS go.mod package.json
tide_pwd_max_dirs 0
tide_pwd_truncate_margin 10
tide_pwd_unwritable_icon ''