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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | function scriptura
argparse --stop-nonopt h/help n/new e/edit w/which c/cat -- $argv
or return
set --query SCRIPTURA_ROOT
or if set --query SD_ROOT
set --function SCRIPTURA_ROOT $SD_ROOT
else
set --function SCRIPTURA_ROOT "$HOME/sd"
end
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
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 --local begin (math $i + 1)
if test $begin -gt (count $argv)
set begin 1
end
set --local remaining $argv[$begin..-1]
if set --query _flag_help
__scriptura_help $target
return $status
end
if set --query _flag_new
__scriptura_new $target
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 script
if path is --perm exec $target
$target $remaining
else
echo "scriptura: $target is not executable" >&2
__scriptura_cat $target
end
else if path is --type dir $target; and not test "$target" = "$SCRIPTURA_ROOT"
__scriptura_list $target
else
echo "scriptura: don't know what to do with target \"$remaining\"" >&2
end
end
function __scriptura_usage
echo "Usage: scriptura [script_name]"
echo "SCRIPTURA_ROOT: $SCRIPTURA_ROOT"
echo ""
__scriptura_list $SCRIPTURA_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 | string join "\n"
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 SCRIPTURA_EDITOR
set --function SCRIPTURA_EDITOR $(
if set --query SD_EDITOR
echo $SD_EDITOR
else 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 \$SCRIPTURA_EDITOR, \$VISUAL or \$EDITOR!" >& 2
return 1
end
)
end
eval $SCRIPTURA_EDITOR $script_path
end
function __scriptura_cat --argument-names script_path
if not set --query SCRIPTURA_CAT
if set --query SD_CAT
set --function SCRIPTURA_CAT $SD_CAT
else
set --function SCRIPTURA_CAT cat
end
end
eval $SCRIPTURA_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
|