diff --git a/CHANGELOG.md b/CHANGELOG.md index 73cf4d45..ed4ed586 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- `TODOTXT_DEFAULT_ACTION` now also allows action parameters ([#159], [#407]) + ## [2.13.0] - 2024-12-25 ### Added @@ -513,6 +516,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [#8]: https://github.com/todotxt/todo.txt-cli/pull/8 [#148]: https://github.com/todotxt/todo.txt-cli/pull/148 [#156]: https://github.com/todotxt/todo.txt-cli/pull/156 +[#159]: https://github.com/todotxt/todo.txt-cli/pull/159 [#160]: https://github.com/todotxt/todo.txt-cli/pull/160 [#169]: https://github.com/todotxt/todo.txt-cli/pull/169 [#217]: https://github.com/todotxt/todo.txt-cli/pull/217 @@ -536,3 +540,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [#354]: https://github.com/todotxt/todo.txt-cli/pull/354 [#359]: https://github.com/todotxt/todo.txt-cli/pull/359 [#386]: https://github.com/todotxt/todo.txt-cli/pull/386 +[#407]: https://github.com/todotxt/todo.txt-cli/pull/407 diff --git a/tests/t0002-actions.sh b/tests/t0002-actions.sh index 5cacb068..8022ed8d 100755 --- a/tests/t0002-actions.sh +++ b/tests/t0002-actions.sh @@ -16,25 +16,65 @@ echo "TODO: foo" EOF chmod +x foo +cat > foo2 << 'EOF' +shift +IFS=- # Print arguments separated with dashes to recognize the individual arguments. +printf 'TODO: %s\n' "$*" +EOF +chmod +x foo2 + test_expect_success 'custom action (default location 1)' ' - mkdir .todo.actions.d - cp foo .todo.actions.d/ + mkdir -p .todo.actions.d && cp foo .todo.actions.d/ todo.sh foo > output; test_cmp expect output && rm -rf .todo.actions.d ' test_expect_success 'custom action (default location 2)' ' - mkdir -p .todo/actions - cp foo .todo/actions/ + mkdir -p .todo/actions && cp foo .todo/actions/ todo.sh foo > output; test_cmp expect output && rm -rf .todo/actions ' test_expect_success 'custom action (env variable)' ' - mkdir myactions - cp foo myactions/ + mkdir -p myactions && cp foo myactions/ TODO_ACTIONS_DIR=myactions todo.sh foo > output; test_cmp expect output && rm -rf myactions ' +test_expect_success 'custom action (default action)' ' + mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/ + TODOTXT_DEFAULT_ACTION="foo2 foo" todo.sh > output; + test_cmp expect output && rm -rf .todo.actions.d +' + +test_todo_session 'default built-in action with multiple arguments' <>> TODOTXT_DEFAULT_ACTION='add +foo @bar baz' todo.sh +1 +foo @bar baz +TODO: 1 added. +EOF + +test_todo_session 'default custom action with multiple arguments' <>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/ + +>>> TODOTXT_DEFAULT_ACTION='foo2 foo bar baz' todo.sh +TODO: foo-bar-baz +EOF + +: > todo.txt +export TODOTXT_DEFAULT_ACTION="add foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\"" +test_todo_session 'default built-in action with arguments that have special characters' <>> todo.sh +1 foo bar \$HOSTNAME O'Really? "quoted" +TODO: 1 added. +EOF + +: > todo.txt +export TODOTXT_DEFAULT_ACTION="foo2 foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\"" +test_todo_session 'default custom action with arguments that have special characters' <>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/ + +>>> todo.sh +TODO: foo bar-\$HOSTNAME-O'Really?-"quoted" +EOF + test_done diff --git a/todo.cfg b/todo.cfg index 3ccd2181..8f50e4b8 100644 --- a/todo.cfg +++ b/todo.cfg @@ -96,3 +96,8 @@ export REPORT_FILE="$TODO_DIR/report.txt" # just before the list output is displayed. # # export TODOTXT_FINAL_FILTER='cat' + +## default actions +# Set a default action for calling todo.sh without arguments. +# Also allows for parameters for the action. +# export TODOTXT_DEFAULT_ACTION='' diff --git a/todo.sh b/todo.sh index fa4601a6..6f1163c1 100755 --- a/todo.sh +++ b/todo.sh @@ -779,7 +779,13 @@ if [ -n "$OVR_TODOTXT_FINAL_FILTER" ] ; then TODOTXT_FINAL_FILTER="$OVR_TODOTXT_FINAL_FILTER" fi -ACTION=${1:-$TODOTXT_DEFAULT_ACTION} +isDefaultAction= +if [ -n "$1" ]; then + ACTION=$1 +else + ACTION=$TODOTXT_DEFAULT_ACTION + isDefaultAction=t +fi [ -z "$ACTION" ] && usage [ -d "$TODO_DIR" ] || mkdir -p "$TODO_DIR" 2> /dev/null || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory" @@ -1080,6 +1086,10 @@ elif hasCustomAction "$TODO_ACTIONS_DIR" "$action" then "$TODO_ACTIONS_DIR/$action" "$@" exit $? +elif [ "$isDefaultAction" ] && [ -n "$TODOTXT_DEFAULT_ACTION" ]; then + # Recursive invocation with the contents of the default action parsed as a + # command-line. + eval "exec \"\${BASH_SOURCE[0]}\" $TODOTXT_DEFAULT_ACTION" fi ## Only run if $action isn't found in .todo.actions.d