diff --git a/share/functions/prompt_pwd.fish b/share/functions/prompt_pwd.fish index e80297b07..eb7ef6190 100644 --- a/share/functions/prompt_pwd.fish +++ b/share/functions/prompt_pwd.fish @@ -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 diff --git a/tests/checks/prompt.fish b/tests/checks/prompt.fish new file mode 100644 index 000000000..eb6d4825a --- /dev/null +++ b/tests/checks/prompt.fish @@ -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