Skip to content

Commit 8afcc36

Browse files
authored
Allow parameters for todo.sh default action
Merge pull request #407 from chrysle/allow-params-default-action
2 parents 31d8772 + 84d18c1 commit 8afcc36

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
- `TODOTXT_DEFAULT_ACTION` now also allows action parameters ([#159], [#407])
12+
1013
## [2.13.0] - 2024-12-25
1114

1215
### Added
@@ -513,6 +516,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
513516
[#8]: https://github.com/todotxt/todo.txt-cli/pull/8
514517
[#148]: https://github.com/todotxt/todo.txt-cli/pull/148
515518
[#156]: https://github.com/todotxt/todo.txt-cli/pull/156
519+
[#159]: https://github.com/todotxt/todo.txt-cli/pull/159
516520
[#160]: https://github.com/todotxt/todo.txt-cli/pull/160
517521
[#169]: https://github.com/todotxt/todo.txt-cli/pull/169
518522
[#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.
536540
[#354]: https://github.com/todotxt/todo.txt-cli/pull/354
537541
[#359]: https://github.com/todotxt/todo.txt-cli/pull/359
538542
[#386]: https://github.com/todotxt/todo.txt-cli/pull/386
543+
[#407]: https://github.com/todotxt/todo.txt-cli/pull/407

tests/t0002-actions.sh

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,65 @@ echo "TODO: foo"
1616
EOF
1717
chmod +x foo
1818

19+
cat > foo2 << 'EOF'
20+
shift
21+
IFS=- # Print arguments separated with dashes to recognize the individual arguments.
22+
printf 'TODO: %s\n' "$*"
23+
EOF
24+
chmod +x foo2
25+
1926
test_expect_success 'custom action (default location 1)' '
20-
mkdir .todo.actions.d
21-
cp foo .todo.actions.d/
27+
mkdir -p .todo.actions.d && cp foo .todo.actions.d/
2228
todo.sh foo > output;
2329
test_cmp expect output && rm -rf .todo.actions.d
2430
'
2531

2632
test_expect_success 'custom action (default location 2)' '
27-
mkdir -p .todo/actions
28-
cp foo .todo/actions/
33+
mkdir -p .todo/actions && cp foo .todo/actions/
2934
todo.sh foo > output;
3035
test_cmp expect output && rm -rf .todo/actions
3136
'
3237

3338
test_expect_success 'custom action (env variable)' '
34-
mkdir myactions
35-
cp foo myactions/
39+
mkdir -p myactions && cp foo myactions/
3640
TODO_ACTIONS_DIR=myactions todo.sh foo > output;
3741
test_cmp expect output && rm -rf myactions
3842
'
3943

44+
test_expect_success 'custom action (default action)' '
45+
mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
46+
TODOTXT_DEFAULT_ACTION="foo2 foo" todo.sh > output;
47+
test_cmp expect output && rm -rf .todo.actions.d
48+
'
49+
50+
test_todo_session 'default built-in action with multiple arguments' <<EOF
51+
>>> TODOTXT_DEFAULT_ACTION='add +foo @bar baz' todo.sh
52+
1 +foo @bar baz
53+
TODO: 1 added.
54+
EOF
55+
56+
test_todo_session 'default custom action with multiple arguments' <<EOF
57+
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
58+
59+
>>> TODOTXT_DEFAULT_ACTION='foo2 foo bar baz' todo.sh
60+
TODO: foo-bar-baz
61+
EOF
62+
63+
: > todo.txt
64+
export TODOTXT_DEFAULT_ACTION="add foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\""
65+
test_todo_session 'default built-in action with arguments that have special characters' <<EOF
66+
>>> todo.sh
67+
1 foo bar \$HOSTNAME O'Really? "quoted"
68+
TODO: 1 added.
69+
EOF
70+
71+
: > todo.txt
72+
export TODOTXT_DEFAULT_ACTION="foo2 foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\""
73+
test_todo_session 'default custom action with arguments that have special characters' <<EOF
74+
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
75+
76+
>>> todo.sh
77+
TODO: foo bar-\$HOSTNAME-O'Really?-"quoted"
78+
EOF
79+
4080
test_done

todo.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ export REPORT_FILE="$TODO_DIR/report.txt"
9696
# just before the list output is displayed.
9797
#
9898
# export TODOTXT_FINAL_FILTER='cat'
99+
100+
## default actions
101+
# Set a default action for calling todo.sh without arguments.
102+
# Also allows for parameters for the action.
103+
# export TODOTXT_DEFAULT_ACTION=''

todo.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,13 @@ if [ -n "$OVR_TODOTXT_FINAL_FILTER" ] ; then
779779
TODOTXT_FINAL_FILTER="$OVR_TODOTXT_FINAL_FILTER"
780780
fi
781781

782-
ACTION=${1:-$TODOTXT_DEFAULT_ACTION}
782+
isDefaultAction=
783+
if [ -n "$1" ]; then
784+
ACTION=$1
785+
else
786+
ACTION=$TODOTXT_DEFAULT_ACTION
787+
isDefaultAction=t
788+
fi
783789

784790
[ -z "$ACTION" ] && usage
785791
[ -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"
10801086
then
10811087
"$TODO_ACTIONS_DIR/$action" "$@"
10821088
exit $?
1089+
elif [ "$isDefaultAction" ] && [ -n "$TODOTXT_DEFAULT_ACTION" ]; then
1090+
# Recursive invocation with the contents of the default action parsed as a
1091+
# command-line.
1092+
eval "exec \"\${BASH_SOURCE[0]}\" $TODOTXT_DEFAULT_ACTION"
10831093
fi
10841094

10851095
## Only run if $action isn't found in .todo.actions.d

0 commit comments

Comments
 (0)