all repos — scriptura @ v0.3.1

a corner in $HOME for your scripts

functions/scriptura.fish (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function scriptura --description 'Manage and execute scripts'
    set --function valid_subcommands run help new edit which cat cmd

    if not set --query --function scriptura_root_var
        if not set --query SCRIPTURA_ROOT; and set --query SD_ROOT
            set --function scriptura_root_var SD_ROOT
        else
            set --function scriptura_root_var SCRIPTURA_ROOT
        end
    end

    set --function SCRIPTURA_ROOT $$scriptura_root_var
    if test -z "$SCRIPTURA_ROOT"
        set --function SCRIPTURA_ROOT $HOME/sd
    end

    if not path is $SCRIPTURA_ROOT
        if test "$argv[1]" = new
            mkdir $SCRIPTURA_ROOT
            return 0
        else
            echo $scriptura_root_var $SCRIPTURA_ROOT does not exist, use `$(status function) new` to create it
            return 1
        end
    end

    if not count $argv >/dev/null
        echo "Usage: $(status current-command) <subcommand> <script_name>"
        echo "$scriptura_root_var: $SCRIPTURA_ROOT"
        echo ""
        echo "Subcommands:"
        echo ""
        for subcommand in $valid_subcommands
            printf '  %-12s %s\n' $subcommand (__scriptura_describe_function __scriptura_$subcommand)
        end
        set --local user_subcommands (__scriptura_subcommands scriptura)
        if count $user_subcommands >/dev/null
            echo ""
            echo "User Subcommands:"
            echo ""
            for subcommand in $user_subcommands
                printf '  %-12s %s\n' $subcommand (__scriptura_desc $SCRIPTURA_ROOT/scriptura/$subcommand)
            end
        end
        echo ""
        echo "Scripts:"
        echo ""

        __scriptura_list $SCRIPTURA_ROOT
        return 0
    end

    set --function target $SCRIPTURA_ROOT
    set --function valid 0

    set --function subcommand $argv[1]

    if contains $subcommand $valid_subcommands
        set --erase argv[1]
    else if contains $subcommand (__scriptura_subcommands scriptura)
        set --function cmd $subcommand
        set --erase argv[1]
    else
        echo Invalid subcommand: $subcommand >&2
        echo Expected one of: $valid_subcommands >&2
        return 1
    end

    for i in (seq (count $argv))
        if test "$argv[$i]" = --
            set --function valid $i
            break
        else if path is --type dir $target/$argv[$i]
            set --function target $target/$argv[$i]
            set --function seen_dir 1
            set --function valid $i
        else if path is --type file $target/$argv[$i]; or test "$subcommand" = new
            set --function target $target/$argv[$i]
            set --function seen_script 1
            set --function valid $i
            if not test "$subcommand" = new
                break
            end
        else
            break
        end
    end

    set --local remaining $argv[(math $valid + 1)..-1]

    switch $subcommand
        case run
            if set --query --function seen_script
                __scriptura_run $target $remaining
                return $status
            else if path is --type dir $target;
                and test (count $remaining) -eq 0;
                or set --query --function seen_dir

                __scriptura_list $target
                return $status
            end
        case help
            __scriptura_help $target
            return $status
        case new
            __scriptura_new $target $remaining
            return $status
        case edit
            __scriptura_edit $target $remaining
            return $status
        case which
            __scriptura_which $target $remaining
            return $status
        case cat
            __scriptura_cat $target $remaining
            return $status
        case cmd
            __scriptura_cmd $target $remaining
            return $status
        case (__scriptura_subcommands scriptura)
            __scriptura_cmd $target -- $cmd $remaining
            return $status
    end

    echo "scriptura: don't know what to do with target \"$argv\"" >&2
    return 1
end