fork/fish-shell

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

Johannes AltmanningerPrepare to not localize private function descriptionsd835c5252

master
2.3 KiB83 linesraw
1# localization: tier1
2
3# This defines a compatibility shim for the `trap` command found in other shells like bash and zsh.
4function trap -d 'Perform an action when the shell receives a signal'
5    set -l options h/help l/list-signals p/print
6    argparse -n trap $options -- $argv
7    or return
8
9    if set -q _flag_help
10        __fish_print_help trap
11        return 0
12    end
13
14    set -l mode
15    set -l cmd
16    set -l sig
17
18    # Determine the mode based on either an explicit flag or the non-flag args.
19    if set -q _flag_print
20        set mode print
21    else if set -q _flag_list_signals
22        set mode list
23    else
24        switch (count $argv)
25            case 0
26                set mode print
27            case 1
28                set mode clear
29            case '*'
30                if test $argv[1] = -
31                    set -e argv[1]
32                    set mode clear
33                else
34                    set mode set
35                end
36        end
37    end
38
39    switch $mode
40        case clear
41            for sig in (string upper -- $argv | string replace -r '^SIG' '')
42                if test -n "$sig"
43                    functions -e __trap_handler_$sig
44                end
45            end
46
47        case set
48            set -l cmd $argv[1]
49            set -e argv[1]
50
51            for sig in (string upper -- $argv | string replace -r '^SIG' '')
52                if test -n "$sig"
53                    set -l sw --on-signal $sig
54                    if string match -qi exit -- $sig
55                        set sw --on-event fish_exit
56                    end
57                    echo "function __trap_handler_$sig $sw; $cmd; end" | source
58                else
59                    return 1
60                end
61            end
62
63        case print
64            set -l names
65            if set -q argv[1]
66                set names $argv
67            else
68                set names (functions -a | string split ',' | string match "__trap_handler_*" | string replace '__trap_handler_' '')
69            end
70
71            for sig in (string upper -- $names | string replace -r '^SIG' '')
72                if test -n "$sig"
73                    functions __trap_handler_$sig
74                    echo ""
75                else
76                    return 1
77                end
78            end
79
80        case list
81            kill -l
82    end
83end