2022-01-22 05:15:21 +00:00
|
|
|
function prompt_pwd --description 'short CWD for the prompt'
|
2021-04-11 19:31:45 +00:00
|
|
|
set -l options h/help d/dir-length= D/full-length-dirs=
|
2021-04-11 19:44:44 +00:00
|
|
|
argparse -n prompt_pwd $options -- $argv
|
2017-07-13 20:57:17 +00:00
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help prompt_pwd
|
|
|
|
return 0
|
2016-11-28 05:27:22 +00:00
|
|
|
end
|
2015-12-17 14:17:28 +00:00
|
|
|
|
2021-04-11 19:44:44 +00:00
|
|
|
set -q argv[1]
|
|
|
|
or set argv $PWD
|
|
|
|
|
2021-04-11 19:31:45 +00:00
|
|
|
set -ql _flag_d
|
|
|
|
and set -l fish_prompt_pwd_dir_length $_flag_d
|
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
set -q fish_prompt_pwd_dir_length
|
|
|
|
or set -l fish_prompt_pwd_dir_length 1
|
2015-10-09 22:37:43 +00:00
|
|
|
|
2021-04-11 19:31:45 +00:00
|
|
|
set -l fulldirs 0
|
|
|
|
set -ql _flag_D
|
2022-08-07 18:23:45 +00:00
|
|
|
and set -l fish_prompt_pwd_full_dirs $_flag_D
|
2021-04-11 19:31:45 +00:00
|
|
|
|
|
|
|
set -q fish_prompt_pwd_full_dirs
|
2021-04-11 19:44:44 +00:00
|
|
|
or set -l fish_prompt_pwd_full_dirs 1
|
|
|
|
|
|
|
|
for path in $argv
|
|
|
|
# Replace $HOME with "~"
|
2022-09-04 07:18:57 +00:00
|
|
|
set -l realhome (string escape --style=regex -- ~)
|
2021-04-11 19:44:44 +00: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-11 19:46:56 +00: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-11 19:44:44 +00:00
|
|
|
end
|
|
|
|
|
2023-12-25 17:46:23 +00:00
|
|
|
string join / -- (string replace -ar -- '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full
|
2021-04-11 19:31:45 +00:00
|
|
|
end
|
2016-11-28 05:27:22 +00:00
|
|
|
end
|
2006-02-12 13:14:21 +00:00
|
|
|
end
|