Allow passing directories to prompt_pwd

This allows us to test it without cd-ing about the place.
This commit is contained in:
Fabian Homborg 2021-04-11 21:44:44 +02:00
parent 7a8feb4656
commit af2952dd2f
2 changed files with 35 additions and 17 deletions

View file

@ -1,6 +1,6 @@
function prompt_pwd --description 'Print the current working directory, shortened to fit the prompt'
set -l options h/help d/dir-length= D/full-length-dirs=
argparse -n prompt_pwd --max-args=0 $options -- $argv
argparse -n prompt_pwd $options -- $argv
or return
if set -q _flag_help
@ -8,6 +8,9 @@ function prompt_pwd --description 'Print the current working directory, shortene
return 0
end
set -q argv[1]
or set argv $PWD
set -ql _flag_d
and set -l fish_prompt_pwd_dir_length $_flag_d
@ -19,24 +22,26 @@ function prompt_pwd --description 'Print the current working directory, shortene
and set fish_prompt_pwd_full_dirs $_flag_D
set -q fish_prompt_pwd_full_dirs
or set -l fish_prompt_pwd_full_dirs 0
or set -l fish_prompt_pwd_full_dirs 1
# Replace $HOME with "~"
set -l realhome ~
set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD)
for path in $argv
# Replace $HOME with "~"
set -l realhome ~
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..]
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..]
end
string join / (string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full
end
string join / (string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full
end
end

13
tests/checks/prompt.fish Normal file
View file

@ -0,0 +1,13 @@
#RUN: %fish %s
prompt_pwd -d 1 /foo/bar/baz
# CHECK: /f/b/baz
prompt_pwd /usr/share/fish/tools/web_config/sample_prompts
# CHECK: /u/s/f/t/w/sample_prompts
prompt_pwd -D 2 /usr/share/fish/tools/web_config/sample_prompts
# CHECK: /u/s/f/t/web_config/sample_prompts
prompt_pwd -D 0 /usr/share/fish/tools/web_config/sample_prompts
# CHECK: /u/s/f/t/w/sample_prompts