fork/fish-shell

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

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

master
2.1 KiB47 linesraw
1# localization: tier1
2
3# This is a neat function, stolen from zsh. It allows you to edit the
4# value of a variable interactively.
5#
6
7function vared --description "Edit variable value"
8    if test (count $argv) = 1
9        switch $argv
10
11            case -h --h --he --hel --help
12                __fish_print_help vared
13                return 0
14
15            case '-*'
16                printf (_ "%s: Unknown option %s\n") vared $argv >&2
17                return 1
18
19            case '*'
20                if test (count $$argv ) -lt 2
21                    # Try to avoid using local variables in this function, otherwise they can't be edited.
22                    # https://github.com/fish-shell/fish-shell/issues/8836
23                    # However we need to use one local, as you can't read directly into an index (like vared PATH[4]).
24                    set -l __fish_vared_temp_value
25
26                    # The command substitution in this line controls the scope.
27                    # If variable already exists, do not add any switches, so we don't change
28                    # scoping or export rules. But if it does not exist, we make the variable
29                    # global, so that it will not die when this function dies.
30
31                    read -p 'set_color green; echo '$argv'; set_color --reset; echo "> "' \
32                        (if not set -q $argv; echo -g; end) \
33                        -c "$$argv" \
34                        __fish_vared_temp_value
35                    if test -n "$$argv"
36                        set $argv $__fish_vared_temp_value
37                    else
38                        set -g $argv $__fish_vared_temp_value
39                    end
40                else
41                    printf (_ '%s: %s is an array variable. Use %svared%s %s[n]%s to edit the n:th element of %s\n') vared $argv (set_color $fish_color_command; echo) (set_color $fish_color_normal; echo) $argv (set_color --reset; echo) $argv >&2
42                end
43        end
44    else
45        printf (_ '%s: Expected exactly one argument, got %s.\n\nSynopsis:\n\t%svared%s VARIABLE\n') vared (count $argv) (set_color $fish_color_command; echo) (set_color $fish_color_normal; echo) >&2
46    end
47end