tide/functions/_tide_pwd.fish

58 lines
2.2 KiB
Fish
Raw Normal View History

2020-08-30 01:38:18 +03:00
function _tide_pwd --on-variable PWD --on-signal WINCH
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 -g _tide_git_dir (git rev-parse --show-toplevel 2>/dev/null) # Used for git_prompt item
set -l pwdMaxLength (math $COLUMNS -$tide_pwd_truncate_margin)
2020-08-30 01:38:18 +03:00
# Compute anchors
set -l tidePwdAnchors
if contains 'first' $tide_pwd_anchors
if test -n "$splitPwd[1]"
set -a tidePwdAnchors $splitPwd[1]
else
set -a tidePwdAnchors $splitPwd[2]
end
end
if contains 'last' $tide_pwd_anchors
set -a tidePwdAnchors $splitPwd[-1]
end
if contains 'git' $tide_pwd_anchors
set -a tidePwdAnchors (string split -r -m1 '/' "$_tide_git_dir")[2]
end
set -l colorDirs (set_color $tide_pwd_color_dirs)
set -g _tide_pwd_output $colorDirs$pwd
2020-06-06 20:40:14 +03:00
2020-08-29 22:13:36 +03:00
# Prepend icons
2020-07-12 02:45:23 +03:00
if not test -w $PWD
set _tide_pwd_output $colorDirs{$tide_pwd_unwritable_icon}' ' $_tide_pwd_output
2020-08-29 22:13:36 +03:00
else if test $PWD = $HOME
set _tide_pwd_output $colorDirs{$tide_pwd_home_icon}' ' $_tide_pwd_output
else
set _tide_pwd_output $colorDirs{$tide_pwd_dir_icon}' ' $_tide_pwd_output
2020-06-06 20:40:14 +03:00
end
2020-08-30 01:38:18 +03:00
set -l truncatedList '.' $splitPwd
2020-06-07 19:40:42 +03:00
2020-08-30 01:38:18 +03:00
for dir in $splitPwd
if contains $dir $tidePwdAnchors
2020-06-25 05:47:15 +03:00
set _tide_pwd_output (string replace $dir (set_color -o $tide_pwd_color_anchors)$dir(set_color normal -b $tide_pwd_bg_color) $_tide_pwd_output)
2020-08-30 01:38:18 +03:00
else if test (string length $pwd) -gt $pwdMaxLength
2020-07-11 05:25:02 +03:00
set -l dirTruncated $dir
set -l truncationLength 1
while contains $dirTruncated $truncatedList
set dirTruncated (string sub -l $truncationLength $dir)
set truncationLength (math $truncationLength+1)
2020-06-06 20:40:14 +03:00
end
2020-07-11 05:25:02 +03:00
set -a truncatedList $dirTruncated
2020-08-30 01:38:18 +03:00
set pwd (string replace $dir $dirTruncated $pwd)
2020-07-11 05:25:02 +03:00
set _tide_pwd_output (string replace $dir (set_color $tide_pwd_color_truncated_dirs)$dirTruncated $_tide_pwd_output)
2020-06-06 20:40:14 +03:00
end
end
2020-07-21 00:11:59 +03:00
set _tide_pwd_output (string replace --all '/' $colorDirs'/' $_tide_pwd_output)
2020-06-06 20:40:14 +03:00
end