all repos — scriptura @ 70e83b0f00a81744295e4335c4db3ef4ad6e9d01

a corner in $HOME for your scripts

initial commit

Alan Pearce
commit

70e83b0f00a81744295e4335c4db3ef4ad6e9d01

2 files changed, 248 insertions(+), 0 deletions(-)

changed files
A completions/scriptura.fish
@@ -0,0 +1,75 @@
+set --query SD_ROOT or set --local SD_ROOT "~/sd" + +complete --command scriptura --no-files + +complete --command scriptura \ + --condition 'not __fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --short-option h --long-option help \ + --description 'Display help information' + +complete --command scriptura \ + --condition 'not __fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --short-option n --long-option new \ + --description 'Create new script' + +complete --command scriptura \ + --condition 'not __fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --short-option e --long-option edit \ + --description 'Edit existing script' + +complete --command scriptura \ + --condition 'not __fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --short-option c --long-option cat \ + --description 'Display script content' + +complete --command scriptura \ + --condition 'not __fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --short-option w --long-option which \ + --description 'Display script path' + +complete --command scriptura \ + --condition 'not __fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --arguments '$(__scriptura_complete $SD_ROOT)' + +complete --command scriptura \ + --condition '__fish_seen_subcommand_from $(__scriptura_print $SD_ROOT)' \ + --condition 'not __scriptura_seen_script $(commandline -xpc)' \ + --condition 'not __fish_seen_argument --short n --long new' \ + --arguments '$(__scriptura_complete $SD_ROOT/$(commandline -xpc)[-1])' + +complete --command scriptura \ + --condition '__scriptura_seen_script $(commandline -xpc)' \ + --force-files + +function __scriptura_print --argument-names from + path basename $(path filter --type file,dir --perm exec $from/*) +end + +function __scriptura_complete --argument-names from + for entry in $(path filter --type file,dir --perm exec $from/*) + printf "%s\t%s\n" $(path basename $entry) $(__scriptura_help $entry) + end +end + +function __scriptura_seen_script + set --function argv $argv[2..-1] + set --function target $SD_ROOT + + if not set --query argv[1] + return 1 + end + + 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 --perm exec $target/$argv[$i] + return 0 + else + break + end + end + + return 1 +end
A functions/scriptura.fish
@@ -0,0 +1,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