tide/functions/_tide_item_pwd.fish

53 lines
2.2 KiB
Fish
Raw Normal View History

2020-05-20 20:05:33 +03:00
function _tide_item_pwd
2020-11-24 01:56:52 +03:00
set -l preMaxDirsSplitPwd (string replace $HOME '~' $PWD | string split '/')
2021-01-10 19:56:46 +03:00
set -l splitPwd $preMaxDirsSplitPwd[(math -$tide_pwd_max_dirs)..-1]
set -l splitPwdForLength $splitPwd
set -l splitPwdForOutput $splitPwd
2020-11-24 01:56:52 +03:00
2021-01-10 19:56:46 +03:00
set -l anchorDirs
contains first $tide_pwd_anchors && if test -n "$splitPwd[1]"
set -a anchorDirs 1
2020-12-26 03:32:03 +03:00
else
2021-01-10 19:56:46 +03:00
set -a anchorDirs 2
2020-11-24 01:56:52 +03:00
end
2021-01-10 19:56:46 +03:00
contains last $tide_pwd_anchors && set -a anchorDirs (count $splitPwd)
2020-11-24 01:56:52 +03:00
2020-12-26 19:42:21 +03:00
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)
2020-11-24 01:56:52 +03:00
if not test -w $PWD
2021-01-10 19:56:46 +03:00
printf '%s' $colorDirs $tide_pwd_unwritable_icon' '
2020-11-24 01:56:52 +03:00
else if test $PWD = $HOME
2021-01-10 19:56:46 +03:00
printf '%s' $colorDirs $tide_pwd_home_icon' '
2020-11-24 01:56:52 +03:00
else
2021-01-10 19:56:46 +03:00
printf '%s' $colorDirs $tide_pwd_dir_icon' '
2020-11-24 01:56:52 +03:00
end
set -l pwdMaxLength (math $COLUMNS -$tide_pwd_truncate_margin)
2021-01-10 19:56:46 +03:00
for i in (seq (count $splitPwd))
set -l parentDir (string join '/' $splitPwd[1..(math $i-1)])
2021-01-12 03:23:35 +03:00
# This line returns true if any markers exist in splitPwd[$i], or if anchorDirs contains i
2021-01-10 19:56:46 +03:00
if eval test -z thisPartIsFalse "-o -e $parentDir"/$splitPwd[$i]/$tide_pwd_markers || contains $i $anchorDirs
2021-01-12 03:23:35 +03:00
set splitPwdForOutput[$i] $colorAnchors$splitPwd[$i]$keepBackgroundColor$colorDirs
2021-01-10 19:56:46 +03:00
else if test (string join '/' $splitPwdForLength | string length) -gt $pwdMaxLength
2020-11-24 01:56:52 +03:00
set -l truncationLength 1
2021-01-10 19:56:46 +03:00
while set -l truncated (string sub --length $truncationLength $splitPwd[$i])
test (string length $truncated) -lt (string length $splitPwdForLength[$i]) || break
2021-01-12 03:23:35 +03:00
2021-01-10 19:56:46 +03:00
eval set -l truncatedPath $parentDir/$truncated
test (count $truncatedPath*/) -gt 1 || break
2021-01-12 03:23:35 +03:00
2021-01-10 19:56:46 +03:00
set truncationLength (math $truncationLength + 1)
2020-11-24 01:56:52 +03:00
end
2021-01-12 03:23:35 +03:00
2021-01-10 19:56:46 +03:00
set splitPwdForLength[$i] $truncated
2021-01-12 03:23:35 +03:00
set splitPwdForOutput[$i] $colorTruncatedDirs$truncated$keepBackgroundColor$colorDirs
2020-11-24 01:56:52 +03:00
end
end
2021-01-12 03:23:35 +03:00
string join '/' $splitPwdForOutput
2020-04-03 01:39:10 +03:00
end