From ea0cf2c8847d3d7459175e40da86d5b9721ab1fb Mon Sep 17 00:00:00 2001 From: Roy Dumblauskas Date: Sat, 5 Jul 2025 13:33:24 -0500 Subject: [PATCH] don't shorten pwd --- roy-config/fish/fish_variables | 1 + roy-config/fish/functions/fish_prompt.fish | 54 +++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/roy-config/fish/fish_variables b/roy-config/fish/fish_variables index 74b1ee1..84c8273 100644 --- a/roy-config/fish/fish_variables +++ b/roy-config/fish/fish_variables @@ -29,3 +29,4 @@ SETUVAR fish_pager_color_description:yellow\x1e\x2di SETUVAR fish_pager_color_prefix:normal\x1e\x2d\x2dbold\x1e\x2d\x2dunderline SETUVAR fish_pager_color_progress:brwhite\x1e\x2d\x2dbackground\x3dcyan SETUVAR fish_pager_color_selected_background:\x2dr +SETUVAR fish_prompt_pwd_dir_length:100 diff --git a/roy-config/fish/functions/fish_prompt.fish b/roy-config/fish/functions/fish_prompt.fish index 33ee160..e436fa1 100644 --- a/roy-config/fish/functions/fish_prompt.fish +++ b/roy-config/fish/functions/fish_prompt.fish @@ -27,5 +27,57 @@ function fish_prompt --description 'Write out the prompt' set -l statusb_color (set_color $bold_flag $fish_color_status) set -l prompt_status (__fish_print_pipestatus "[" "]" "|" "$status_color" "$statusb_color" $last_pipestatus) - echo -n -s (prompt_login)' ' (set_color $color_cwd) (pwd) $normal (fish_vcs_prompt) $normal " "$prompt_status $suffix " " + echo -n -s (prompt_login)' ' (set_color $color_cwd) (prompt_pwd) $normal (fish_vcs_prompt) $normal " "$prompt_status $suffix " " +end + +function prompt_pwd --description 'short CWD for the prompt' + set -l options h/help d/dir-length= D/full-length-dirs= + argparse -n prompt_pwd $options -- $argv + or return + + if set -q _flag_help + __fish_print_help prompt_pwd + 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 + + set -q fish_prompt_pwd_dir_length + or set -l fish_prompt_pwd_dir_length 1 + + set -l fulldirs 0 + set -ql _flag_D + and set -l fish_prompt_pwd_full_dirs $_flag_D + + set -q fish_prompt_pwd_full_dirs + or set -l fish_prompt_pwd_full_dirs 1 + + for path in $argv + # Replace $HOME with "~" + set -l realhome (string escape --style=regex -- ~) + 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..] + 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 + end + + string join / -- (string replace -ar -- '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full + end + end end