fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Peter Ammonprompt_pwd: strip control characters2bab8b583

master
1.7 KiB51 linesraw
1# localization: tier1
2function prompt_pwd --description 'short CWD for the prompt'
3    set -l options h/help d/dir-length= D/full-length-dirs=
4    argparse -n prompt_pwd $options -- $argv
5    or return
6
7    if set -q _flag_help
8        __fish_print_help prompt_pwd
9        return 0
10    end
11
12    set -q argv[1]
13    or set argv $PWD
14
15    set -ql _flag_d
16    and set -l fish_prompt_pwd_dir_length $_flag_d
17
18    set -q fish_prompt_pwd_dir_length
19    or set -l fish_prompt_pwd_dir_length 1
20
21    set -l fulldirs 0
22    set -ql _flag_D
23    and set -l fish_prompt_pwd_full_dirs $_flag_D
24
25    set -q fish_prompt_pwd_full_dirs
26    or set -l fish_prompt_pwd_full_dirs 1
27
28    for path in $argv
29        # Strip control characters to avoid injecting terminal escape sequences into the prompt.
30        set -l tmp (__fish_unexpand_tilde $path | string replace -ra '[[:cntrl:]]' '')
31
32        if test "$fish_prompt_pwd_dir_length" -eq 0
33            echo $tmp
34        else
35            # Shorten to at most $fish_prompt_pwd_dir_length characters per directory
36            # with full-length-dirs components left at full length.
37            set -l full
38            if test $fish_prompt_pwd_full_dirs -gt 0
39                set -l all (string split -m (math $fish_prompt_pwd_full_dirs - 1) -r / $tmp)
40                set tmp $all[1]
41                set full $all[2..]
42            else if test $fish_prompt_pwd_full_dirs -eq 0
43                # 0 means not even the last component is kept
44                string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*' '$1' $tmp
45                continue
46            end
47
48            string join / -- (string replace -ar -- '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp) $full
49        end
50    end
51end