|
1 | 1 | function __fish_prompt_pwd --description 'Format the current directory for the prompt'
|
2 |
| - set -l max (math "round($COLUMNS / 8)") # maximum length = 1/8rd window width |
| 2 | + if not set -q __fish_prompt_pwd_string |
| 3 | + # Avoid repeated string operations by using a more direct approach |
| 4 | + set -l path_str |
| 5 | + if string match -q "$HOME/*" "$PWD" |
| 6 | + set path_str "~"(string sub -s (math 1 + (string length "$HOME")) "$PWD") |
| 7 | + else if test "$PWD" = "$HOME" |
| 8 | + set path_str "~" |
| 9 | + else |
| 10 | + set path_str "$PWD" |
| 11 | + end |
3 | 12 |
|
4 |
| - set -l short_path (pwd | string replace -r ".*/" "") # basename of current dir |
5 |
| - set -l short_path_length (string length $short_path) |
| 13 | + # Calculate max length |
| 14 | + set -l max (math "round($COLUMNS/6)") |
6 | 15 |
|
7 |
| - # if basename of $PWD is too long by itself, don't trim it |
8 |
| - if test $max -lt $short_path_length |
9 |
| - set max $short_path_length |
10 |
| - end |
| 16 | + # Quick return for short paths |
| 17 | + if test (string length -- $path_str) -le $max |
| 18 | + echo -ns (set_color $fish_color_cwd --bold) $path_str (set_color normal) |
| 19 | + return |
| 20 | + end |
| 21 | + |
| 22 | + # Split path into components only once |
| 23 | + set -l components (string split / $path_str) |
| 24 | + set -l num_components (count $components) |
| 25 | + set -l truncated |
| 26 | + set -l current_len 0 |
| 27 | + set -l i $num_components |
| 28 | + |
| 29 | + # Process components from end until we hit length limit |
| 30 | + while test $i -gt 0 |
| 31 | + set -l component $components[$i] |
| 32 | + set -l component_len (string length -- $component) |
| 33 | + |
| 34 | + # Add 1 for the slash unless it's the first component |
| 35 | + if test -n "$truncated" |
| 36 | + set component_len (math $component_len + 1) |
| 37 | + end |
11 | 38 |
|
12 |
| - # tilde-ify homedir |
13 |
| - set -l long_path (pwd | string replace -r "^$HOME" "~") |
| 39 | + if test (math $current_len + $component_len) -gt $max |
| 40 | + break |
| 41 | + end |
14 | 42 |
|
15 |
| - # is $PWD too long, and if so, by how much? |
16 |
| - set -l long_path_length (string length $long_path) |
17 |
| - set -l excess (math "round($long_path_length - $max)") |
| 43 | + if test -n "$truncated" |
| 44 | + set truncated "$component/$truncated" |
| 45 | + else |
| 46 | + set truncated $component |
| 47 | + end |
18 | 48 |
|
19 |
| - if test $excess -gt 0 |
20 |
| - # cut to $max chars long, trim leading detritus and add leader |
21 |
| - set cut_path (string sub --start "$excess" "$long_path") |
22 |
| - set path_parts (string split --max=1 / "{$cut_path") |
23 |
| - set trimmed_path $path_parts[2] |
| 49 | + set current_len (math $current_len + $component_len) |
| 50 | + set i (math $i - 1) |
| 51 | + end |
24 | 52 |
|
25 |
| - set long_path "…/$trimmed_path" |
| 53 | + # Add ellipsis if we didn't use all components |
| 54 | + if test $i -gt 0 |
| 55 | + set __fish_prompt_pwd_string "…/$truncated" |
| 56 | + end |
26 | 57 | end
|
27 | 58 |
|
28 |
| - echo -ns (set_color $fish_color_cwd --bold) $long_path (set_color normal) |
| 59 | + echo -ns (set_color $fish_color_cwd --bold) $__fish_prompt_pwd_string (set_color normal) |
29 | 60 | end
|
0 commit comments