2022-01-22 13:15:21 +08:00
|
|
|
function prompt_pwd --description 'short CWD for the prompt'
|
2021-04-12 03:31:45 +08:00
|
|
|
set -l options h/help d/dir-length= D/full-length-dirs=
|
2021-04-12 03:44:44 +08:00
|
|
|
argparse -n prompt_pwd $options -- $argv
|
2017-07-14 04:57:17 +08:00
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help prompt_pwd
|
|
|
|
return 0
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2015-12-17 22:17:28 +08:00
|
|
|
|
2021-04-12 03:44:44 +08:00
|
|
|
set -q argv[1]
|
|
|
|
or set argv $PWD
|
|
|
|
|
2021-04-12 03:31:45 +08:00
|
|
|
set -ql _flag_d
|
|
|
|
and set -l fish_prompt_pwd_dir_length $_flag_d
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
set -q fish_prompt_pwd_dir_length
|
|
|
|
or set -l fish_prompt_pwd_dir_length 1
|
2015-10-10 06:37:43 +08:00
|
|
|
|
2021-04-12 03:31:45 +08:00
|
|
|
set -l fulldirs 0
|
|
|
|
set -ql _flag_D
|
2022-08-08 02:23:45 +08:00
|
|
|
and set -l fish_prompt_pwd_full_dirs $_flag_D
|
2021-04-12 03:31:45 +08:00
|
|
|
|
|
|
|
set -q fish_prompt_pwd_full_dirs
|
2021-04-12 03:44:44 +08:00
|
|
|
or set -l fish_prompt_pwd_full_dirs 1
|
|
|
|
|
|
|
|
for path in $argv
|
|
|
|
# Replace $HOME with "~"
|
2022-09-04 15:18:57 +08:00
|
|
|
set -l realhome (string escape --style=regex -- ~)
|
2021-04-12 03:44:44 +08:00
|
|
|
set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $path)
|
|
|
|
|
|
|
|
if test "$fish_prompt_pwd_dir_length" -eq 0
|
|
|
|
echo $tmp
|
|
|
|
else
|
|
|
|
# Shorten to at most $fish_prompt_pwd_dir_length characters per directory
|
|
|
|
# with full-length-dirs components left at full length.
|
|
|
|
set -l full
|
|
|
|
if test $fish_prompt_pwd_full_dirs -gt 0
|
|
|
|
set -l all (string split -m (math $fish_prompt_pwd_full_dirs - 1) -r / $tmp)
|
|
|
|
set tmp $all[1]
|
|
|
|
set full $all[2..]
|
2021-04-12 03:46:56 +08:00
|
|
|
else if test $fish_prompt_pwd_full_dirs -eq 0
|
|
|
|
# 0 means not even the last component is kept
|
|
|
|
string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*' '$1' $tmp
|
|
|
|
continue
|
2021-04-12 03:44:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
string join / (string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full
|
2021-04-12 03:31:45 +08:00
|
|
|
end
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2006-02-12 21:14:21 +08:00
|
|
|
end
|