Skip to content

Commit 033f5de

Browse files
committed
Add addp command for adding tasks with priority
- Implement addp PRIORITY "TASK" syntax to add tasks with specified priority (A-Z) - Priority is automatically converted to uppercase and formatted as (PRIORITY) - Add comprehensive error handling for invalid priorities (numbers, special chars, multiple chars) - Integrate with existing features like TODOTXT_DATE_ON_ADD and configuration options - Update help documentation in shorthelp() and actionsHelp() - Add complete documentation to USAGE.md with examples and usage patterns - Add comprehensive test suite (t1050-addp.sh) with 23 test cases covering: - Basic functionality and priority validation - Case conversion (lowercase to uppercase) - Integration with projects and contexts - Error handling for all invalid input scenarios - Configuration integration (date on add, priority on add overrides) - Edge cases with special characters and quotes Example usage: todo.sh addp A "High priority task" # Creates: (A) High priority task todo.sh addp c "Medium priority task" # Creates: (C) Medium priority task
1 parent b20f9b4 commit 033f5de

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

USAGE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ todo.sh addm "FIRST THING I NEED TO DO +project1 @context
2828
SECOND THING I NEED TO DO +project2 @context"
2929
```
3030

31+
### `addp`
32+
Adds TEXT TO ADD to your todo.txt file on its own line with the specified PRIORITY (A-Z) at the beginning.
33+
34+
The priority letter is automatically converted to uppercase and formatted as `(PRIORITY)`.
35+
36+
```shell
37+
todo.sh addp A "Important high-priority task"
38+
todo.sh addp e "Medium priority task"
39+
```
40+
41+
Example output:
42+
```
43+
(A) Important high-priority task
44+
(E) Medium priority task
45+
```
46+
3147
### `addto`
3248
Adds a line of text to any file located in the todo.txt directory.
3349

tests/t1050-addp.sh

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/env bash
2+
3+
test_description='addp command functionality
4+
5+
Tests the addp command which adds tasks with specified priority.
6+
'
7+
. ./test-lib.sh
8+
9+
# Test basic addp functionality
10+
test_todo_session 'basic addp functionality' <<EOF
11+
>>> todo.sh addp A "High priority task"
12+
1 (A) High priority task
13+
TODO: 1 added.
14+
15+
>>> todo.sh addp B "Medium priority task"
16+
2 (B) Medium priority task
17+
TODO: 2 added.
18+
19+
>>> todo.sh addp Z "Low priority task"
20+
3 (Z) Low priority task
21+
TODO: 3 added.
22+
23+
>>> todo.sh -p list
24+
1 (A) High priority task
25+
2 (B) Medium priority task
26+
3 (Z) Low priority task
27+
--
28+
TODO: 3 of 3 tasks shown
29+
EOF
30+
31+
# Test lowercase priority conversion
32+
cat > todo.txt <<EOF
33+
EOF
34+
test_todo_session 'lowercase priority conversion' <<EOF
35+
>>> todo.sh addp c "lowercase priority test"
36+
1 (C) lowercase priority test
37+
TODO: 1 added.
38+
39+
>>> todo.sh addp z "another lowercase test"
40+
2 (Z) another lowercase test
41+
TODO: 2 added.
42+
43+
>>> todo.sh -p list
44+
1 (C) lowercase priority test
45+
2 (Z) another lowercase test
46+
--
47+
TODO: 2 of 2 tasks shown
48+
EOF
49+
50+
# Test addp with projects and contexts
51+
cat > todo.txt <<EOF
52+
EOF
53+
test_todo_session 'addp with projects and contexts' <<EOF
54+
>>> todo.sh addp A "Important work task +project @work"
55+
1 (A) Important work task +project @work
56+
TODO: 1 added.
57+
58+
>>> todo.sh addp B "Call client +client @phone"
59+
2 (B) Call client +client @phone
60+
TODO: 2 added.
61+
62+
>>> todo.sh -p list
63+
1 (A) Important work task +project @work
64+
2 (B) Call client +client @phone
65+
--
66+
TODO: 2 of 2 tasks shown
67+
EOF
68+
69+
# Test error handling - no arguments
70+
cat > todo.txt <<EOF
71+
EOF
72+
test_todo_session 'addp error - no arguments' <<EOF
73+
>>> todo.sh addp
74+
usage: todo.sh addp PRIORITY "TODO ITEM"
75+
=== 1
76+
EOF
77+
78+
# Test error handling - missing task text
79+
test_todo_session 'addp error - missing task text' <<EOF
80+
>>> todo.sh addp A
81+
usage: todo.sh addp PRIORITY "TODO ITEM"
82+
=== 1
83+
EOF
84+
85+
# Test error handling - invalid priority (number)
86+
test_todo_session 'addp error - invalid priority number' <<EOF
87+
>>> todo.sh addp 1 "Invalid priority task"
88+
TODO: Priority must be a letter from A to Z (got: 1)
89+
=== 1
90+
EOF
91+
92+
# Test error handling - invalid priority (multiple characters)
93+
test_todo_session 'addp error - invalid priority multiple chars' <<EOF
94+
>>> todo.sh addp AA "Invalid priority task"
95+
TODO: Priority must be a letter from A to Z (got: AA)
96+
=== 1
97+
EOF
98+
99+
# Test error handling - invalid priority (special character)
100+
test_todo_session 'addp error - invalid priority special char' <<EOF
101+
>>> todo.sh addp @ "Invalid priority task"
102+
TODO: Priority must be a letter from A to Z (got: @)
103+
=== 1
104+
EOF
105+
106+
# Test addp integration with date_on_add
107+
cat > todo.txt <<EOF
108+
EOF
109+
echo "export TODOTXT_DATE_ON_ADD=1" >> todo.cfg
110+
111+
test_todo_session 'addp with date on add' <<EOF
112+
>>> todo.sh addp A "Task with date"
113+
1 (A) $(date '+%Y-%m-%d') Task with date
114+
TODO: 1 added.
115+
116+
>>> todo.sh -p list
117+
1 (A) $(date '+%Y-%m-%d') Task with date
118+
--
119+
TODO: 1 of 1 tasks shown
120+
EOF
121+
122+
# Reset config for next test
123+
echo "export TODOTXT_DATE_ON_ADD=0" >> todo.cfg
124+
125+
# Test that addp overrides TODOTXT_PRIORITY_ON_ADD
126+
cat > todo.txt <<EOF
127+
EOF
128+
echo "export TODOTXT_PRIORITY_ON_ADD=C" >> todo.cfg
129+
130+
test_todo_session 'addp overrides PRIORITY_ON_ADD' <<EOF
131+
>>> todo.sh addp A "Priority A task"
132+
1 (A) Priority A task
133+
TODO: 1 added.
134+
135+
>>> todo.sh add "Normal add task"
136+
2 (C) Normal add task
137+
TODO: 2 added.
138+
139+
>>> todo.sh -p list
140+
1 (A) Priority A task
141+
2 (C) Normal add task
142+
--
143+
TODO: 2 of 2 tasks shown
144+
EOF
145+
146+
# Test addp with various edge cases
147+
cat > todo.txt <<EOF
148+
EOF
149+
test_todo_session 'addp edge cases' <<EOF
150+
>>> todo.sh addp E "Task with special chars: !@#$%^&*()"
151+
1 (E) Task with special chars: !@#$%^&*()
152+
TODO: 1 added.
153+
154+
>>> todo.sh addp F "Task with 'single quotes' and \"double quotes\""
155+
2 (F) Task with 'single quotes' and "double quotes"
156+
TODO: 2 added.
157+
158+
>>> todo.sh -p list
159+
1 (E) Task with special chars: !@#$%^&*()
160+
2 (F) Task with 'single quotes' and "double quotes"
161+
--
162+
TODO: 2 of 2 tasks shown
163+
EOF
164+
165+
test_done

todo.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ shorthelp()
4545
add|a "THING I NEED TO DO +project @context"
4646
addm "THINGS I NEED TO DO
4747
MORE THINGS I NEED TO DO"
48+
addp PRIORITY "TEXT TO ADD"
4849
addto DEST "TEXT TO ADD"
4950
append|app NR "TEXT TO APPEND"
5051
archive
@@ -177,6 +178,11 @@ actionsHelp()
177178
Adds SECOND THING I NEED TO DO to you todo.txt on its own line.
178179
Project and context notation optional.
179180
181+
addp PRIORITY "TEXT TO ADD"
182+
Adds TEXT TO ADD to your todo.txt file on its own line with
183+
the specified PRIORITY (A-Z) at the beginning.
184+
For example, addp A "Important task" creates "(A) Important task".
185+
180186
addto DEST "TEXT TO ADD"
181187
Adds a line of text to any file located in the todo.txt directory.
182188
For example, addto inbox.txt "decide about vacation"
@@ -1120,6 +1126,29 @@ case $action in
11201126
IFS=$SAVEIFS
11211127
;;
11221128

1129+
"addp")
1130+
errmsg="usage: $TODO_SH addp PRIORITY \"TODO ITEM\""
1131+
[ -z "$2" ] && die "$errmsg"
1132+
priority="$2"
1133+
[ -z "$3" ] && die "$errmsg"
1134+
1135+
# Validate priority is a single letter A-Z
1136+
if ! echo "$priority" | grep -q '^[A-Za-z]$'; then
1137+
die "TODO: Priority must be a letter from A to Z (got: $priority)"
1138+
fi
1139+
1140+
# Convert to uppercase
1141+
priority=$(echo "$priority" | tr '[:lower:]' '[:upper:]')
1142+
1143+
shift 2
1144+
input="$*"
1145+
1146+
# Add priority prefix to input
1147+
input="($priority) $input"
1148+
1149+
_addto "$TODO_FILE" "$input"
1150+
;;
1151+
11231152
"addto" )
11241153
errmsg="usage: $TODO_SH addto DEST \"TODO ITEM\""
11251154
[ -z "$2" ] && die "$errmsg"

0 commit comments

Comments
 (0)