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