all repos — scriptura @ v0.0.3

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
if not set --query SCRIPTURA_ROOT
    if set --query SD_ROOT
        set --global SCRIPTURA_ROOT $SD_ROOT
    else
        set --global SCRIPTURA_ROOT "$HOME/sd"
    end
end

function scriptura
    argparse --stop-nonopt h/help n/new e/edit w/which c/cat -- $argv
    or return

    if not path is $SCRIPTURA_ROOT
        if set --query _flag_new
            mkdir $SCRIPTURA_ROOT
        else
            echo \$SCRIPTURA_ROOT $SCRIPTURA_ROOT does not exist, use `$(status function) --new` to create it
            return 1
        end
    end

    if not count $argv >/dev/null
        __scriptura_usage
        return 0
    end

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

    for i in (seq 1 (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 valid $i
        else if path is --type file $target/$argv[$i]; or set --query _flag_new
            set --function target $target/$argv[$i]
            set --function seen_script 1
            set --function valid $i
            if not set --query _flag_new
                break
            end
        else
            break
        end
    end

    if test $valid -gt 0
        set --local remaining $argv[(math $valid + 1)..-1]

        if set --query _flag_help
            __scriptura_help $target
            return $status
        end

        if set --query _flag_new
            __scriptura_new $_flag_edit $target $remaining
            return $status
        end

        if set --query _flag_edit
            __scriptura_edit $target
            return $status
        end

        if set --query _flag_which
            __scriptura_which $target $remaining
            return $status
        end

        if set --query _flag_cat
            __scriptura_cat $target
            return $status
        end

        if set --query --function seen_script
            if path is --perm exec $target
                set --export SCRIPTURA_ROOT $SCRIPTURA_ROOT
                set --export SD (path dirname $target)
                $target $remaining
                return $status
            else
                echo "scriptura: $target is not executable" >&2
                __scriptura_cat $target
                return $status
            end
        else if path is --type dir $target; and test (count $remaining) -eq 0
            __scriptura_list $target
            return $status
        end
    end

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