all repos — scriptura @ 70e83b0f00a81744295e4335c4db3ef4ad6e9d01

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
set --query SD_ROOT or set --local SD_ROOT "$HOME/sd"

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

    if not path is $SD_ROOT
        if set --query _flag_new
            mkdir $SD_ROOT
        else
            echo \$SD_ROOT $SD_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 $SD_ROOT

    for i in (seq 1 (count $argv))
        if test "$argv[$i]" = --
            break
        else if path is --type dir $target/$argv[$i]
            set --function target $target/$argv[$i]
        else if path is --type file $target/$argv[$i]; or set --query _flag_new
            set --function script $target/$argv[$i]
            set --function target $target/$argv[$i]
            break
        else
            break
        end
    end

    set argv $argv[(math $i + 1)..-1]

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

    if set --query _flag_new
        __scriptura_new $target
        return 0
    end

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

    if set --query _flag_which
        __scriptura_which $target $argv
        return 0
    end

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

    if set --query script
        if path is --perm exec $target
            $target $argv
        else
            echo "scriptura: $target is not executable" >&2
            __scriptura_cat $target
        end
    else if path is --type dir $target
        __scriptura_list $target
    else
        echo "scriptura: don't know what to do with target \"$target\"" >&2
    end
end

function __scriptura_usage
    echo "Usage: scriptura [script_name]"
    echo "SD_ROOT: $SD_ROOT"
    echo ""
    __scriptura_list $SD_ROOT
end

function __scriptura_list --argument-names from
    for dir in $(path filter --type dir $from/*)
        printf "%-12s -- %-64s\n" "$(path basename $dir) ..." $(__scriptura_help $dir)
    end
    for file in $(path filter --type file --perm exec $from/*)
        printf "%-12s -- %-64s\n" $(path basename $file) $(__scriptura_help $file)
    end
end

function __scriptura_help --argument-names file_or_dir
    if path is --type dir $file_or_dir
        set --function help_file $file_or_dir/help
    else
        set --function help_file $file_or_dir.help
    end

    if path is --type file $help_file
        __scriptura_cat $help_file
    else if path is --type dir $file_or_dir
        echo "$(path basename $file_or_dir) commands"
    else if path is --type file $file_or_dir
        echo $(__scriptura_desc $file_or_dir)
    else
        echo "Unknown type"
    end
end

function __scriptura_desc --argument-names file
    string match --groups-only --regex '^#\s*([^!].*)$' <$file
end

function __scriptura_file_help --argument-names file
    string match --all --groups-only --regex '^#\s*([^!].*)$' <$file
end

function __scriptura_new --argument-names target
    if not path is --type dir (path dirname $target)
        mkdir -p $target
    end

    if path is --type file $target
        echo "Error: $target already exists"
        return 1
    end

    printf "%s\n\n" "#!/usr/bin/env fish" >$target
    chmod +x $target

    __scriptura_edit $target
end

function __scriptura_edit --argument-names script_path
    if not set --query SD_EDITOR
        set --function SD_EDITOR $(
            if set --query VISUAL
                echo $VISUAL
            else if set --query EDITOR
                echo $EDITOR
            else if command --query vim
                echo vim
            else if command --query nano
                echo nano
            else if command --query vi
                echo vi
            else
                echo "Don't know which editor to use, set \$SD_EDITOR, \$VISUAL or \$EDITOR!" >& 2
                return 1
            end
        )
    end

    eval $SD_EDITOR $script_path
end

function __scriptura_cat --argument-names script_path
    if not set --query SD_CAT
        set --function SD_CAT cat
    end

    eval $SD_CAT $script_path
end

function __scriptura_which --argument-names script_path
    if path is --type file --perm exec $script_path
        echo $script_path
    else
        return 1
    end
end