fork/fish-shell

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

Johannes Altmanningerfish_add_path: remove extra argument to printfc2e2fd643

master
4.3 KiB110 linesraw
1function fish_add_path --description "Add paths to the PATH"
2    # This is meant to be the easy one-stop shop to adding stuff to $PATH.
3    # By default it'll prepend the given paths to a universal $fish_user_paths, excluding the already-included ones.
4    #
5    # That means it can be executed once in an interactive session, or stuffed in config.fish,
6    # and it will do The Right Thing.
7    #
8    # The options:
9    # --prepend or --append to select whether to put the new paths first or last
10    # --global or --universal to decide whether to use a universal or global fish_user_paths
11    # --path to set $PATH instead
12    # --move to move existing entries instead of ignoring them
13    # --verbose to print the set-command used
14    # --dry-run to print the set-command without running it
15    # We do not allow setting $PATH universally.
16    #
17    # It defaults to keeping $fish_user_paths or creating a universal, prepending and ignoring existing entries.
18    argparse -x g,U -x P,U -x a,p g/global U/universal P/path p/prepend a/append h/help m/move v/verbose n/dry-run -- $argv
19    or return
20
21    if set -q _flag_help
22        __fish_print_help fish_add_path
23        return 0
24    end
25
26    set -l scope $_flag_global $_flag_universal
27    if not set -q scope[1]; and not set -q fish_user_paths
28        set scope -U
29    end
30
31    set -l var fish_user_paths
32    set -q _flag_path
33    and set var PATH
34    # $PATH should be global
35    and set scope -g
36    set -l mode $_flag_prepend $_flag_append
37    set -q mode[1]; or set mode -p
38
39    # Enable verbose mode if we're interactively used
40    status current-command | string match -rq '^fish_add_path$'
41    and isatty stdout
42    and set -l _flag_verbose yes
43
44    # To keep the order of our arguments, go through and save the ones we want to keep.
45    set -l newpaths
46    set -l indexes
47    for path in $argv
48        # Realpath allows us to canonicalize the path, which is needed for deduplication.
49        # We could add a non-canonical version of the given path if no duplicate exists, but tbh that's a recipe for disaster.
50
51        # realpath complains if a parent directory does not exist, so we silence stderr.
52        set -l p (builtin realpath -s -- $path 2>/dev/null)
53
54        # Ignore non-existing paths
55        if not test -d "$p"
56            # path does not exist
57            if set -q _flag_verbose
58                # print a message in verbose mode
59                if test -f "$p"
60                    printf (_ "Skipping path because it is a file instead of a directory: %s\n") "$p"
61                else
62                    printf (_ "Skipping non-existent path: %s\n") "$p"
63                end
64            end
65            continue
66        end
67
68        if set -l ind (contains -i -- $p $$var)
69            # In move-mode, we remove it from its current position and add it back.
70            if set -q _flag_move; and not contains -- $p $newpaths
71                set -a indexes $ind
72                set -a newpaths $p
73            else if set -q _flag_verbose
74                printf (_ "Skipping already included path: %s\n") "$p"
75            end
76        else if not contains -- $p $newpaths
77            # Without move, we only add it if it's not in.
78            set -a newpaths $p
79        end
80    end
81
82    # Ensure the variable is only set once, by constructing a new variable before.
83    # This is to stop any handlers or anything from firing more than once.
84    set -l newvar $$var
85    if set -q _flag_move; and set -q indexes[1]
86        # We remove in one step, so the indexes don't move.
87        set -e newvar["$indexes"]
88    end
89    set $mode newvar $newpaths
90
91    # Finally, only set if there is anything *to* set.
92    # This saves us from setting, especially in the common case of someone putting this in config.fish
93    # to ensure a path is in $PATH.
94    if set -q newpaths[1]; or set -q indexes[1]
95        if set -q _flag_verbose; or set -q _flag_n
96            # The escape helps make it unambiguous - so you see whether an argument includes a space or something.
97            echo (string escape -- set $scope $var $newvar)
98        end
99
100        not set -q _flag_n
101        and set $scope $var $newvar
102        return 0
103    else
104        if set -q _flag_verbose
105            # print a message in verbose mode
106            printf (_ "No paths to add, not setting anything.\n")
107        end
108        return 1
109    end
110end