all repos — scriptura @ e2bef6abcb519abcc37d0024d08319b20f76d6b1

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
function scriptura
    set --function valid_subcommands run help new edit which cat

    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
        printf 'Usage: %s [run] <script_name>\n' (status current-command)
        printf '%s: %s\n' $scriptura_root_var $SCRIPTURA_ROOT
        printf '\n'

        __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
        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
    end

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