Skip to content

Commit

Permalink
Allow parameters for todo.sh default action
Browse files Browse the repository at this point in the history
Merge pull request #407 from chrysle/allow-params-default-action
  • Loading branch information
inkarkat authored Dec 26, 2024
2 parents 31d8772 + 84d18c1 commit 8afcc36
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
52 changes: 46 additions & 6 deletions tests/t0002-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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' <<EOF
>>> 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' <<EOF
>>> 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' <<EOF
>>> 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' <<EOF
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
>>> todo.sh
TODO: foo bar-\$HOSTNAME-O'Really?-"quoted"
EOF

test_done
5 changes: 5 additions & 0 deletions todo.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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=''
12 changes: 11 additions & 1 deletion todo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8afcc36

Please sign in to comment.