all repos — scriptura @ eea12c75e45b3ad7b76dfb89861fea82cad01c04

a corner in $HOME for your scripts

split helpers into files

Alan Pearce
commit

eea12c75e45b3ad7b76dfb89861fea82cad01c04

parent

fd61ac887fd4160094757c843ca55cbe1a49f552

A conf.d/scriptura-config.fish
@@ -0,0 +1,7 @@
+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
A functions/__scriptura_cat.fish
@@ -0,0 +1,11 @@
+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
A functions/__scriptura_desc.fish
@@ -0,0 +1,3 @@
+function __scriptura_desc --argument-names file + string match --groups-only --regex '^#\s*([^!].*)$' <$file | string join "\n" +end
A functions/__scriptura_edit.fish
@@ -0,0 +1,24 @@
+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
A functions/__scriptura_help.fish
@@ -0,0 +1,18 @@
+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 +
A functions/__scriptura_list.fish
@@ -0,0 +1,8 @@
+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
A functions/__scriptura_new.fish
@@ -0,0 +1,16 @@
+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 +
A functions/__scriptura_usage.fish
@@ -0,0 +1,7 @@
+function __scriptura_usage + echo "Usage: scriptura [script_name]" + echo "SCRIPTURA_ROOT: $SCRIPTURA_ROOT" + echo "" + __scriptura_list $SCRIPTURA_ROOT +end +
A functions/__scriptura_which.fish
@@ -0,0 +1,7 @@
+function __scriptura_which --argument-names script_path + if path is --type file --perm exec $script_path + echo $script_path + else + return 1 + end +end
M functions/scriptura.fishfunctions/scriptura.fish
@@ -2,13 +2,6 @@ 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
@@ -24,9 +17,11 @@ return 0
end set --function target $SCRIPTURA_ROOT + set --function stop_search 0 for i in (seq 1 (count $argv)) if test "$argv[$i]" = -- + set --function stop_search $i break else if path is --type dir $target/$argv[$i] set --function target $target/$argv[$i]
@@ -77,112 +72,9 @@ 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" + else if path is --type dir $target; and test $stop_search -gt 0 __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
M tests/scriptura.fishtests/scriptura.fish
@@ -4,6 +4,7 @@ # Setup test environment
set --global test_root (mktemp -d) set --global original_scriptura_root $SCRIPTURA_ROOT set --global original_sd_root $SD_ROOT +set --prepend fish_function_path (status dirname)/../functions # Helper function to clean up function cleanup
@@ -24,6 +25,7 @@ # Test: SCRIPTURA_ROOT defaults to ~/sd when not set
@test "SCRIPTURA_ROOT defaults to ~/sd when not set" ( set --erase SCRIPTURA_ROOT set --erase SD_ROOT + source $(status dirname)/../conf.d/scriptura-config.fish source $(status dirname)/../functions/scriptura.fish scriptura ) = "\$SCRIPTURA_ROOT $HOME/sd does not exist, use `scriptura --new` to create it"
@@ -32,6 +34,7 @@ # Test: SCRIPTURA_ROOT uses SD_ROOT when set
@test "SCRIPTURA_ROOT uses SD_ROOT when set" ( set --erase SCRIPTURA_ROOT set --global --export SD_ROOT "/custom/sd" + source $(status dirname)/../conf.d/scriptura-config.fish source $(status dirname)/../functions/scriptura.fish scriptura ) = "\$SCRIPTURA_ROOT /custom/sd does not exist, use `scriptura --new` to create it"
@@ -40,6 +43,7 @@ # Test: SCRIPTURA_ROOT uses existing value when set
@test "SCRIPTURA_ROOT uses existing value when set" ( set --global --export SCRIPTURA_ROOT "/existing/root" set --global --export SD_ROOT "/custom/sd" + source $(status dirname)/../conf.d/scriptura-config.fish source $(status dirname)/../functions/scriptura.fish scriptura ) = "\$SCRIPTURA_ROOT /existing/root does not exist, use `scriptura --new` to create it"
@@ -195,7 +199,7 @@ set --global --export SCRIPTURA_ROOT $test_root
mkdir -p "$test_root/dash_test" printf "#!/usr/bin/env fish\necho dash_dir\n" > "$test_root/dash_test/script" chmod +x "$test_root/dash_test/script" - scriptura dash_test -- script 2>&1 | string match --regex "^script\s+ --" + scriptura dash_test -- script 2>&1 | string match "script*" ) # Test: SCRIPTURA_CAT is used when set
@@ -216,10 +220,10 @@ scriptura --cat sd_cat_test
) = line1 # Test: unknown target shows error -@test "unknown target shows error" -n "$( +@test "unknown target shows error" "$( set --global --export SCRIPTURA_ROOT $test_root - scriptura nonexistent_target 2>&1 | string match --regex "don't know what to do" -)" + scriptura nonexistent_target 2>&1 +)" = 'scriptura: don\'t know what to do with target "nonexistent_target"' # Test: help for directory without help file shows default @test "help for directory without help file shows default" (