fork/fish-shell

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

Nahorcolor: prefer `set_color --reset` over `set_color normal`867946468

master
6.4 KiB169 linesraw
1function fish_delta
2    argparse h/help f/no-functions c/no-completions C/no-config d/no-diff n/new V/vendor= -- $argv
3
4    if set -q _flag_help
5        __fish_print_help fish_delta
6        return 0
7    end
8
9    type -q diff
10    or set -l _flag_d --no-diff
11
12    set -l vendormode default
13    if set -ql _flag_vendor[1]
14        if not contains -- $_flag_vendor[-1] default ignore user
15            echo "Wrong vendor mode '$_flag_vendor[-1]'." >&2
16            echo "Valid values are: default, ignore, new" >&2
17            return 2
18        end
19        set vendormode $_flag_vendor[-1]
20    end
21
22    # TODO: Do we want to keep the vendor dirs in here?
23    set -l default_function_path
24    test "$vendormode" = default && set -a default_function_path $__fish_vendor_functionsdirs
25
26    set -l default_complete_path
27    test "$vendormode" = default && set -a default_complete_path $__fish_vendor_completionsdirs
28
29    set -l default_conf_path
30    test "$vendormode" = default && set -a default_conf_path $__fish_vendor_confdirs
31
32    set -l user_function_path
33    set -l user_complete_path
34    set -l user_conf_path $__fish_config_dir/conf.d $__fish_sysconf_dir/conf.d
35    test "$vendormode" = user && set -a user_conf_path $__fish_vendor_confdirs
36
37    for dir in $fish_function_path
38        if contains -- $vendormode ignore default
39            contains -- $dir $__fish_vendor_functionsdirs
40            and continue
41        end
42        contains -- $dir $default_function_path
43        or set -a user_function_path $dir
44    end
45    for dir in $fish_complete_path
46        if contains -- $vendormode ignore default
47            contains -- $dir $__fish_vendor_completionsdirs
48            and continue
49        end
50        # We don't care about generated completions.
51        # They shouldn't be compared at all.
52        contains -- $dir $default_complete_path $__fish_cache_dir/generated_completions
53        or set -a user_complete_path $dir
54    end
55
56    set -l vars
57    not set -ql _flag_f[1]
58    and set -a vars user_function_path default_function_path
59    not set -ql _flag_c[1]
60    and set -a vars user_complete_path default_complete_path
61    not set -ql _flag_C[1]
62    and set -a vars user_conf_path default_conf_path
63
64    if not set -q vars[1]
65        echo No directories to check! >&2
66        return 2
67    end
68
69    set -l have_diff 0
70
71    if isatty stdout
72        set -f colors "$(set_color --reset)" "$(set_color brblue)" "$(set_color bryellow)" "$(set_color green)" "$(set_color red)"
73        set -f pager (__fish_anypager)
74        or set pager cat
75
76        if type -q less
77            set -l lessopts isRF
78            if test (less --version | string match -r 'less (\d+)')[2] -lt 530 2>/dev/null
79                set lessopts "$lessopts"X
80            end
81
82            not set -qx LESS
83            and set -xf LESS $lessopts
84        end
85    else
86        set -f colors "" "" "" "" ""
87        set -f pager cat # cannot use a builtin here
88    end
89
90    begin
91        while set -q vars[1]
92            set -l user_var $$vars[1]
93            set -l default_var $$vars[2]
94            set -l all_changed 0
95            # We count config files as "changed" compared to /dev/null
96            # because they are being run.
97            test "$vars[1]" = user_conf_path
98            and set all_changed 1
99            set -l dir
100            test "$vars[2]" = default_complete_path
101            and set dir completions
102            test "$vars[2]" = default_function_path
103            and set dir functions
104            set -e vars[..2]
105            set -l files (path filter -rf -- $user_var/$argv.fish)
106            set -q argv[1]
107            or set files (path filter -rf -- $user_var/*)
108            set -q files[1]
109            and set have_diff 1
110
111            for file in $files
112                set -l bn (path basename -- $file)
113                set -l default_exists false
114                if set -q dir[1]; and contains $dir/$bn (status list-files $dir)
115                    set default_exists true
116                else if test $all_changed = 0
117                    set -ql _flag_n
118                    and printf (_ "%sNew%s: %s\n") $colors[2] $colors[1] $file
119                    continue
120                end
121                if type -q diff
122                    function __fish_delta_diff -V _flag_d -V colors -V file -a default_file
123                        # We execute diff twice - once to figure out if it's changed,
124                        # so we can get nicer output.
125                        #
126                        if not diff -q -- $file $default_file >/dev/null 2>&1
127                            printf (_ "%sChanged%s: %s\n") $colors[3] $colors[1] $file
128                            not set -ql _flag_d[1]
129                            and diff -u -- $default_file $file
130                        else
131                            printf (_ "%sUnmodified%s: %s\n") $colors[4] $colors[1] $file
132                        end
133                    end
134                    if $default_exists
135                        set -l tmpfile (__fish_mktemp_relative fish-delta)
136                        status get-file $dir/$bn >$tmpfile
137                        __fish_delta_diff $tmpfile
138                        command rm $tmpfile
139                    else
140                        __fish_delta_diff /dev/null
141                    end
142                    functions --erase __fish_delta_diff
143                else
144                    # Without diff, we can't really tell if the contents are the same.
145                    printf (_ "%sPossibly changed%s: %s\n") $colors[3] $colors[1] $file
146                end
147            end
148        end
149        # config.fish is special - it's a single file, not a directory,
150        # and it's not precedenced - *both* ~/.config/fish/config.fish and /etc/fish/config.fish
151        # are executed.
152        if not set -ql _flag_C[1]; and begin
153                not set -q argv[1]
154                or contains -- config $argv
155            end
156            for file in (path filter -rf -- $__fish_sysconf_dir/config.fish $__fish_config_dir/config.fish)
157                # We count these as "changed" so they show up.
158                printf (_ "%sChanged%s: %s\n") $colors[3] $colors[1] $file
159                not set -ql _flag_d[1]
160                and diff -u -- /dev/null $file
161            end
162        end
163        # Instead of using diff's --color=always, just do the coloring ourselves.
164    end | string replace -r -- '^(-.*)$' "$colors[5]\$1$colors[1]" |
165        string replace -r -- '^(\+.*)$' "$colors[4]\$1$colors[1]" |
166        $pager
167
168    return $have_diff
169end