fork/fish-shell

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

Johannes AltmanningerMerge changes to config paths6d0cd3d9b

master
18.4 KiB456 linesraw
1function fish_config --description "Launch fish's web based configuration"
2    set -l _flag_color_theme
3    set -l _flag_no_override
4
5    argparse h/help color-theme= no-override -- $argv
6    or return
7
8    if set -q _flag_help
9        __fish_print_help fish_config
10        return 0
11    end
12
13    set -l cmd $argv[1]
14    set -e argv[1]
15
16    set -q cmd[1]
17    or set cmd browse
18
19    if set -q _flag_color_theme[1]
20        if test $cmd != theme
21            or not contains -- "$argv[1]" choose save
22            echo >&2 "fish_config: --color-theme: unknown option"
23            return 1
24        end
25        if not contains -- $_flag_color_theme dark light unknown
26            echo >&2 "fish_config theme: --color-theme argument must be one of 'dark', 'light' or 'unknown', got: '$_flag_color_theme'"
27            return 1
28        end
29    end
30    if set -q _flag_no_override[1]
31        if test $cmd != theme
32            or test "$argv[1]" != choose
33            echo >&2 "fish_config: --no-override: unknown option"
34            return 1
35        end
36    end
37
38    # The web-based configuration UI
39    # Also opened with just `fish_config` or `fish_config browse`.
40    if test $cmd = browse
41        if set -l python (__fish_anypython)
42            function __fish_config_webconfig -V python -a web_config
43                set -lx fish_bin_dir $__fish_bin_dir
44                set -lx __fish_terminal_color_theme $fish_terminal_color_theme
45                $python $web_config/webconfig.py
46            end
47            __fish_data_with_directory tools/web_config '.*' __fish_config_webconfig
48            __fish_with_status functions --erase __fish_config_webconfig
49
50            # If the execution of 'webconfig.py' fails, display python location and return.
51            or begin
52                echo "Please check if Python has been installed successfully."
53                echo "You can find the location of Python by executing the 'command -s $python' command."
54                return 1
55            end
56
57        else
58            echo (set_color $fish_color_error)Cannot launch the web configuration tool:(set_color --reset)
59            echo (set_color -o)"fish_config browse"(set_color --reset) requires Python.
60            echo Installing python will fix this, and also enable completions to be
61            echo automatically generated from man pages.\n
62            echo To change your prompt, use (set_color -o)"fish_config prompt"(set_color --reset) or create a (set_color -o)"fish_prompt"(set_color --reset) function.
63            echo To list the samples use (set_color -o)"fish_config prompt show"(set_color --reset).\n
64
65            echo You can tweak your colors by setting the (set_color $fish_color_search_match)\$fish_color_\*(set_color --reset) variables.
66        end
67        return 0
68    end
69
70    if not contains -- $cmd prompt theme
71        echo No such subcommand: $cmd >&2
72        return 1
73    end
74
75    switch $cmd
76        case prompt
77            # prompt - for prompt switching
78            set -l cmd $argv[1]
79            set -e argv[1]
80
81            if contains -- $cmd list; and set -q argv[1]
82                echo "Too many arguments" >&2
83                return 1
84            end
85
86            switch $cmd
87                case show
88                    set -l fish (status fish-path)
89                    for p in (__fish_config_list_prompts $argv)
90                        set -l promptname (string replace -r '.*/([^/]*).fish$' '$1' $p)
91                        echo -s (set_color --underline) $promptname (set_color --reset)
92                        $fish -c '
93                            functions -e fish_right_prompt
94                            __fish_config_with_file $argv[1] source
95                            false
96                            fish_prompt
97                            echo (set_color --reset)
98                            if functions -q fish_right_prompt
99                                echo right prompt: (false; fish_right_prompt)
100                            end' $p
101                        echo
102                    end
103                case list ''
104                    __fish_config_list_prompts |
105                        string replace -r '.*/([^/]*).fish$' '$1'
106                    return
107                case choose
108                    if set -q argv[2]
109                        echo "Too many arguments" >&2
110                        return 1
111                    end
112                    if not set -q argv[1]
113                        echo "Too few arguments" >&2
114                        return 1
115                    end
116
117                    set -l prompt_path (__fish_config_list_prompts $argv[1])
118                    if not set -q prompt_path[1]
119                        echo "No such prompt: '$argv[1]'" >&2
120                        return 1
121                    end
122                    __fish_config_prompt_reset
123                    __fish_config_with_file $prompt_path source
124                    if not functions -q fish_mode_prompt
125                        status get-file functions/fish_mode_prompt.fish | source
126                    end
127                case save
128                    if begin
129                            read -P"Overwrite prompt? [y/N]" -l yesno
130                            not string match -riq 'y(es)?' -- $yesno
131                        end
132                        echo Not overwriting
133                        return 1
134                    end
135                    echo Overwriting
136                    __fish_backup_config_files functions/{fish_prompt,fish_right_prompt,fish_mode_prompt}.fish
137
138                    if set -q argv[1]
139                        set -l prompt_path (__fish_config_list_prompts $argv[1])
140                        if not set -q prompt_path[1]
141                            echo "No such prompt: '$argv[1]'" >&2
142                            return 1
143                        end
144                        __fish_config_prompt_reset
145                        __fish_config_with_file $prompt_path source
146                    end
147
148                    funcsave fish_prompt
149                    or return
150
151                    for func in fish_right_prompt fish_mode_prompt
152                        if functions -q $func
153                            funcsave $func
154                        else
155                            rm -f $__fish_config_dir/functions/$func.fish
156                        end
157                    end
158                    if not functions -q fish_mode_prompt
159                        status get-file functions/fish_mode_prompt.fish | source
160                    end
161                    return
162            end
163
164            return 0
165        case theme
166            # Selecting themes
167            set -l cmd $argv[1]
168            set -e argv[1]
169
170            if contains -- $cmd list; and set -q argv[1]
171                echo "Too many arguments" >&2
172                return 1
173            end
174
175            switch $cmd
176                case list ''
177                    __fish_theme_names
178                    return
179                case demo
180                    echo -ns (set_color $fish_color_command || set_color $fish_color_normal) /bright/vixens
181                    echo -ns (set_color --reset) ' '
182                    echo -ns (set_color $fish_color_param || set_color $fish_color_normal) jump
183                    echo -ns (set_color --reset) ' '
184                    echo -ns (set_color $fish_color_redirection || set_color $fish_color_normal) '|'
185                    echo -ns (set_color --reset) ' '
186                    echo -ns (set_color $fish_color_quote || set_color $fish_color_normal) '"fowl"'
187                    echo -ns (set_color --reset) ' '
188                    echo -ns (set_color $fish_color_redirection || set_color $fish_color_normal) '> quack'
189                    echo -ns (set_color --reset) ' '
190                    echo -ns (set_color $fish_color_end || set_color $fish_color_normal) '&'
191                    set_color --reset
192                    echo -s (set_color $fish_color_comment || set_color $fish_color_normal) ' # This is a comment'
193                    set_color --reset
194                    echo -ns (set_color $fish_color_command || set_color $fish_color_normal) echo
195                    echo -ns (set_color --reset) ' '
196                    echo -s (set_color $fish_color_error || set_color $fish_color_normal) "'" (set_color $fish_color_quote || set_color $fish_color_normal) "Errors are the portal to discovery"
197                    set_color --reset
198                    echo -ns (set_color $fish_color_command || set_color $fish_color_normal) Th
199                    set_color --reset
200                    set_color $fish_color_autosuggestion || set_color $fish_color_normal
201                    echo is an autosuggestion
202                    echo
203                case show
204                    echo -s (set_color --reset; set_color --underline) Current (set_color --reset)
205                    fish_config theme demo
206                    __fish_theme_for_each __fish_config_theme_demo $argv
207                case choose save
208                    __fish_config_theme_choose $cmd $argv --color-theme=$_flag_color_theme $_flag_no_override
209                    return
210                case dump
211                    if set -q argv[1]
212                        echo "Too many arguments" >&2
213                        return 1
214                    end
215                    # Write some of the current theme in .theme format, to stdout.
216                    for varname in (__fish_theme_variables)
217                        __fish_posix_quote $varname $$varname
218                        echo
219                    end
220                case '*'
221                    echo "No such command: $cmd" >&2
222                    return 1
223            end
224    end
225end
226
227function __fish_config_list_prompts
228    set -lx dir
229    set -l prompt_paths (__fish_config_files prompts .fish $argv)
230    if [ (count $argv) = 1 ] && set -q prompt_paths[2]
231        echo >&2 "fish_config: internal error: multiple prompts matching '$argv' ??"
232        set --erase prompt_paths[2..]
233    end
234    string join \n -- $prompt_paths
235end
236
237function __fish_config_theme_choose_bad_color_theme -a theme_name desired_color_theme source
238    echo >&2 "fish_config theme choose: failed to find '[$desired_color_theme]' section (implied by $source) in '$theme_name' theme"
239end
240
241function __fish_config_theme_choose
242    set -l cmd $argv[1]
243    set -e argv[1]
244    set -l _flag_color_theme
245    set -l _flag_no_override
246    argparse color-theme= no-override -- $argv
247    or return
248    if set -q argv[2]
249        echo "Too many arguments" >&2
250        return 1
251    end
252    # The name of the theme to save *from* is optional for `fish_config theme save`
253    if not set -q argv[1] && contains -- $cmd choose
254        echo "Too few arguments" >&2
255        return 1
256    end
257
258    set -l scope -g
259
260    if test $cmd = save
261        read -P"Overwrite your current theme? [y/N] " -l yesno
262        if not string match -riq 'y(es)?' -- $yesno
263            echo Not overwriting >&2
264            return 1
265        end
266        set scope -U
267    end
268
269    set -l theme_name $argv[1]
270    set -l desired_color_theme $_flag_color_theme
271
272    # Persist the currently loaded/themed variables (in case of `theme save`).
273    if not set -q theme_name[1]
274        # We're persisting whatever current colors are loaded (maybe in the global scope)
275        # to the universal scope, without overriding them from a theme file.
276        # Like above, make sure to erase from other scopes first and ensure known color
277        # variables are defined, even if empty.
278        # This branch is only reachable in the case of `theme save` so $scope is always `-U`.
279        for color in (__fish_theme_variables)
280            # Cache the value from whatever scope currently defines it
281            set -l value $$color
282            set -eg $color
283            set -U $color $value
284        end
285        return 0
286    end
287
288    set -l color_theme
289    __fish_config_theme_canonicalize
290    set -l theme_data (__fish_theme_cat $theme_name)
291    or return
292    set -l color_themes dark light unknown
293    set -l theme_is_color_theme_aware false
294    for ct in $color_themes
295        if contains -- [$ct] $theme_data
296            set theme_is_color_theme_aware true
297        end
298    end
299    set -l need_hook true
300    if not $theme_is_color_theme_aware || set -q desired_color_theme[1] || set -q color_theme[1] || test $cmd = save
301        set need_hook false
302    end
303    function __fish_apply_theme ($need_hook && printf %s\n --on-variable fish_terminal_color_theme) \
304        -V color_themes -V theme_name -V color_theme -V theme_data -V theme_is_color_theme_aware \
305        -V desired_color_theme -V scope
306        if set -q __fish_color_theme[1]
307            set desired_color_theme $__fish_color_theme
308        end
309
310        if set -q color_theme[1] && not set -q desired_color_theme[1]
311            set desired_color_theme $color_theme
312        end
313        set -l override (test -n "$__fish_override" && builtin echo true || builtin echo false)
314
315        if $theme_is_color_theme_aware
316            if set -q desired_color_theme[1]
317                if not contains -- "[$desired_color_theme]" $theme_data
318                    __fish_config_theme_choose_bad_color_theme $theme_name "$desired_color_theme" --color-theme=$desired_color_theme
319                    return 1
320                end
321            else
322                set desired_color_theme $fish_terminal_color_theme
323                if not set -q desired_color_theme[1]
324                    if test $scope = -U
325                        echo >&2 "fish_config theme save: error: \$fish_terminal_color_theme not yet initialized"
326                        return 1
327                    end
328                else if not contains -- "[$desired_color_theme]" $theme_data
329                    __fish_config_theme_choose_bad_color_theme $theme_name "$desired_color_theme" \$fish_terminal_color_theme = $desired_color_theme
330                    echo >&2 "fish_config theme choose: hint: if your terminal does not report colors, pass --color-theme=light or --color-theme=dark when using color-theme-aware themes"
331                    return 1
332                end
333            end
334        else
335            if set -q desired_color_theme[1]
336                and test "$desired_color_theme" != unknown
337                and not contains -- "[$desired_color_theme]" $theme_data
338                __fish_config_theme_choose_bad_color_theme $theme_name "$desired_color_theme" --color-theme=$desired_color_theme
339                return 1
340            end
341        end
342
343        set -l color_theme
344        string match -re -- (__fish_theme_variable_filter)'|^\[.*\]$' $theme_data |
345            while read -lat toks
346                if $theme_is_color_theme_aware && set -q desired_color_theme[1]
347                    for ct in $color_themes
348                        if test "$toks" = [$ct]
349                            set color_theme $ct
350                            break
351                        end
352                    end
353                    if test "$color_theme" != $desired_color_theme
354                        continue
355                    end
356                end
357                set -l varname $toks[1]
358                string match -q '[*' -- $varname
359                and continue
360                # If we're supposed to set universally, remove any shadowing globals
361                # so the change takes effect immediately (and there's no warning).
362                if test $scope = -U; and set -qg $varname
363                    set -eg $varname
364                end
365                if $override || not set -q $varname || string match -rq -- '--theme=.*' $$varname
366                    set $scope $toks[1] (
367                        if not $theme_is_color_theme_aware || set -q desired_color_theme[1]
368                            string join \n -- $toks[2..]
369                        end
370                    ) (test $scope != -U && echo --theme=$theme_name)
371                end
372            end
373        if $override
374            for c in (__fish_theme_variables)
375                string match -rq -- "^--theme=(?!$(string escape --style=regex -- $theme_name)\$).*" $$c
376                or continue
377                # Erase conflicting global variables so we don't get a warning and
378                # so changes are observed immediately.
379                set -eg $c
380                set $scope $c
381            end
382        end
383    end
384    if not $need_hook || test -n "$fish_terminal_color_theme" ||
385            {
386                $theme_is_color_theme_aware && test -z "$fish_terminal_color_theme"
387            }
388        if not set -q _flag_no_override[1]
389            set -fx __fish_override true
390        end
391        __fish_apply_theme
392    end
393end
394
395function __fish_config_theme_canonicalize --no-scope-shadowing
396    # theme_name
397    # color_theme
398    if path is (__fish_theme_dir)/$theme_name.theme
399        return
400    end
401    switch $theme_name
402        case 'fish default'
403            set theme_name default
404        case 'ayu Dark' 'ayu Light' 'ayu Mirage' \
405            'Base16 Default Dark' 'Base16 Default Light' 'Base16 Eighties' \
406            'Bay Cruise' Dracula Fairground 'Just a Touch' Lava \
407            'Mono Lace' 'Mono Smoke' \
408            None Nord 'Old School' Seaweed 'Snow Day' \
409            'Solarized Dark' 'Solarized Light' \
410            'Tomorrow Night Bright' 'Tomorrow Night' Tomorrow
411            if test $theme_name = Tomorrow
412                set color_theme light
413            end
414            set theme_name (string lower (string replace -a " " "-" $theme_name))
415    end
416    switch $theme_name
417        case \
418            ayu-dark ayu-light \
419            base16-default-dark base16-default-light \
420            solarized-dark solarized-light
421            string match -rq -- '^(?<theme_name>.*)-(?<color_theme>dark|light)$' $theme_name
422        case tomorrow-night
423            set theme_name tomorrow
424            set color_theme dark
425    end
426end
427
428function __fish_config_theme_demo
429    argparse name= data=+ color-themes=+ -- $argv
430    or return
431    set -l name $_flag_name
432    set -l color_themes $_flag_color_themes
433    # Use a new, --no-config, fish to display the theme.
434    set -l fish (status fish-path)
435    $fish --no-config -c '
436        set -l name $argv[1]
437        for color_theme in $argv[2..]
438            echo -s (set_color --reset; set_color --underline) "$name" \
439                " ($color_theme color theme)" (set_color --reset)
440            fish_config theme choose $name --color-theme=$color_theme
441            fish_config theme demo
442        end
443    ' $name $color_themes
444end
445
446function __fish_config_prompt_reset
447    # Set the functions to empty so we empty the file
448    # if necessary.
449    function fish_prompt
450    end
451    for func in fish_right_prompt fish_mode_prompt
452        if functions -q $func
453            functions --erase $func
454        end
455    end
456end