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' 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= 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 or return
if set -q _flag_help if set -q _flag_help
@ -8,6 +8,9 @@ function prompt_pwd --description 'Print the current working directory, shortene
return 0 return 0
end end
set -q argv[1]
or set argv $PWD
set -ql _flag_d set -ql _flag_d
and set -l fish_prompt_pwd_dir_length $_flag_d and set -l fish_prompt_pwd_dir_length $_flag_d
@ -19,11 +22,12 @@ function prompt_pwd --description 'Print the current working directory, shortene
and set fish_prompt_pwd_full_dirs $_flag_D and set fish_prompt_pwd_full_dirs $_flag_D
set -q fish_prompt_pwd_full_dirs 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
for path in $argv
# Replace $HOME with "~" # Replace $HOME with "~"
set -l realhome ~ set -l realhome ~
set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD) set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $path)
if test "$fish_prompt_pwd_dir_length" -eq 0 if test "$fish_prompt_pwd_dir_length" -eq 0
echo $tmp echo $tmp
@ -39,4 +43,5 @@ function prompt_pwd --description 'Print the current working directory, shortene
string join / (string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full string join / (string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full
end end
end
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