-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannn
More file actions
executable file
·915 lines (772 loc) · 28 KB
/
annn
File metadata and controls
executable file
·915 lines (772 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
#!/usr/bin/env bash
# annn - Annotation helper for Taskwarrior
# version 0.5.2
#
# Usage:
# annn <id> Open $EDITOR for a new annotation
# annn <id>. List all annotations with index numbers
# annn <id>.<N> Edit annotation N in $EDITOR
# annn <id>.<N> text... Edit annotation N, pre-populated with text
# annn -<id>.<N> Remove annotation N (with confirmation)
# annn <id> text... Annotate directly, no editor
# echo "text" | annn <id> Annotate from pipe, no editor
#
# Designed for Taskwarrior 2.6.2
# License: MIT
set -euo pipefail
readonly VERSION="0.7.0"
readonly SCRIPT_NAME="$(basename "$0")"
readonly EDITOR="${EDITOR:-vim}"
# Global tmpfile for trap cleanup (avoids "unbound variable" with set -u)
tmpfile=""
trap 'rm -f "$tmpfile"' EXIT
# ============================================================================
# Helpers
# ============================================================================
usage() {
cat <<EOF
Usage: $SCRIPT_NAME <id>[.<N>] [text...]
$SCRIPT_NAME <id> New annotation (opens \$EDITOR)
$SCRIPT_NAME <id> text... New annotation, no editor
$SCRIPT_NAME <id>. List annotations
$SCRIPT_NAME <id>.<N> Edit annotation N (opens \$EDITOR)
$SCRIPT_NAME <id>.<N> text... Edit annotation N, pre-populated with text
$SCRIPT_NAME -<id>.<N> Remove annotation N (requires index)
echo "text" | $SCRIPT_NAME <id> Annotate from pipe, no editor
<id> must be a plain integer or UUID — no filters, tags, or project expressions.
Examples:
$SCRIPT_NAME 42 Add new annotation to task 42
$SCRIPT_NAME 42 Fixed the bug Inline annotation, no editor
$SCRIPT_NAME 42. List annotations on task 42
$SCRIPT_NAME 42.1 Edit first annotation on task 42
$SCRIPT_NAME 42.1 see ticket #99 Edit annotation 1, pre-populated
$SCRIPT_NAME -42.1 Remove first annotation from task 42
echo "quick note" | $SCRIPT_NAME 42 Pipe a one-liner
Options:
-h, --help Show this help
-v, --version Show version
EOF
exit 0
}
die() {
echo "[annn] ERROR: $*" >&2
exit 1
}
msg() {
echo "[annn] $*"
}
# ============================================================================
# Load label template into tmpfile, or fall back to bare "label: " prefix
# Template file: ~/.task/templates/<label>.md (case-insensitive label match)
# ============================================================================
load_template() {
local label="${1,,}" # lowercase
local tfile="${HOME}/.task/templates/${label}.md"
if [[ -f "$tfile" ]]; then
echo "$tfile"
fi
}
populate_tmpfile_for_label() {
local label="$1"
local template
template=$(load_template "$label")
if [[ -n "$template" ]]; then
cp "$template" "$tmpfile"
else
printf '%s: ' "$label" > "$tmpfile"
fi
}
# ============================================================================
# Read-only task query — rc.hooks=off prevents cascading hook invocations
# ============================================================================
task_get() {
task rc.hooks=off rc.verbose=nothing _get "$@" 2>/dev/null
}
# ============================================================================
# Validate that a value is a plain integer (task ID)
# ============================================================================
is_integer() {
[[ "$1" =~ ^[0-9]+$ ]]
}
# ============================================================================
# Validate that a value is a UUID
# ============================================================================
is_uuid() {
[[ "$1" =~ ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$ ]]
}
# ============================================================================
# Validate a label name: letters/digits/hyphens, starting with a letter.
# 'note' is reserved for tasknotes.
# ============================================================================
is_label() {
[[ "$1" =~ ^[a-zA-Z][a-zA-Z0-9_-]*$ ]] && [[ "${1,,}" != "note" ]]
}
# ============================================================================
# Validate arg1 is a strict ID or ID.N form — no filters, tags, or keywords
# Accepts: 42 42. 42.3 -42.3 42.label -42.label <uuid>
# ============================================================================
validate_id_arg() {
local arg="$1"
# Remove leading minus for remove-mode check
local bare="${arg#-}"
# Numeric dot forms: 42. 42.3 -42.3
if [[ "$bare" =~ ^([0-9]+)(\.([0-9]*))?$ ]]; then
return 0
fi
# Label dot form: 42.label -42.label
if [[ "$bare" =~ ^([0-9]+)\.([a-zA-Z][a-zA-Z0-9_-]*)$ ]]; then
return 0
fi
# UUID (with optional leading minus — unusual but handle gracefully)
if is_uuid "$bare"; then
return 0
fi
die "Invalid ID: '$arg' — must be a plain integer or UUID (no filters, tags, or project expressions)"
}
# ============================================================================
# Sanitize task description for use in temp filename
# ============================================================================
sanitize_for_filename() {
echo "$1" | tr '[:upper:]' '[:lower:]' \
| sed 's/[^a-z0-9]/-/g; s/--*/-/g; s/^-//; s/-$//' \
| cut -c1-40
}
# ============================================================================
# Make a descriptive temp file for a given task ID
# ============================================================================
make_tmpfile() {
local id="$1"
local desc
desc=$(task_get "$id.description" 2>/dev/null || echo "")
local slug
slug=$(sanitize_for_filename "${desc:-task}")
tmpfile=$(mktemp "/tmp/annn_${id}_${slug}.XXXXXX.md")
}
# ============================================================================
# Get annotation count via DOM
# ============================================================================
get_annotation_count() {
local id="$1"
local count=0
while true; do
local next=$(( count + 1 ))
local desc
desc=$(task_get "$id.annotations.${next}.description" 2>/dev/null) || break
[[ -z "$desc" ]] && break
count=$next
done
echo "$count"
}
# ============================================================================
# Resolve a strict ID (integer or UUID) to confirmed task ID
# ============================================================================
resolve_id() {
local raw="$1"
local id
# Confirm the task exists
id=$(task rc.hooks=off rc.verbose=nothing rc.color=off "$raw" _ids 2>/dev/null | head -1)
if [[ -z "$id" ]]; then
die "No task found with ID: $raw"
fi
echo "$id"
}
# ============================================================================
# Show task context line with annotation count
# ============================================================================
show_task_context() {
local id="$1"
local desc
desc=$(task_get "$id.description" 2>/dev/null || echo "")
local count
count=$(get_annotation_count "$id")
if [[ "$count" -eq 0 ]]; then
msg "Task $id: $desc (no annotations)"
elif [[ "$count" -eq 1 ]]; then
msg "Task $id: $desc (1 annotation)"
else
msg "Task $id: $desc ($count annotations)"
fi
}
# ============================================================================
# List annotations
# ============================================================================
list_annotations() {
local id="$1"
local count
count=$(get_annotation_count "$id")
if [[ "$count" -eq 0 ]]; then
msg "No annotations on task $id"
return
fi
local desc
desc=$(task_get "$id.description" 2>/dev/null)
echo "Task $id: $desc"
echo "---"
local i=1
while [[ $i -le $count ]]; do
local ann_desc ann_entry
ann_desc=$(task_get "$id.annotations.${i}.description" 2>/dev/null)
ann_entry=$(task_get "$id.annotations.${i}.entry" 2>/dev/null)
local date_str="$ann_entry"
if command -v date &>/dev/null && [[ -n "$ann_entry" ]]; then
date_str=$(date -d "$ann_entry" "+%Y-%m-%d %H:%M" 2>/dev/null) || date_str="$ann_entry"
fi
printf " .%-3d [%s]\n" "$i" "$date_str"
# Highlight label: prefix if present
if [[ "$ann_desc" =~ ^([a-zA-Z][a-zA-Z0-9_-]*):\ (.*)$ ]]; then
printf " \033[1m%s:\033[0m %s\n" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
else
echo "$ann_desc" | sed 's/^/ /'
fi
echo ""
i=$(( i + 1 ))
done
}
# ============================================================================
# New annotation — editor, inline text, or pipe
# ============================================================================
new_annotation() {
local id="$1"
local inline_text="${2:-}" # optional pre-supplied text
local annot
if [[ ! -t 0 ]]; then
# Piped input — read stdin directly, skip editor
annot="$(cat)"
if [[ -z "$annot" ]]; then
msg "Empty input, nothing saved."
return
fi
elif [[ -n "$inline_text" ]]; then
# Inline text supplied on command line — no editor
annot="$inline_text"
else
# Interactive — optionally prompt for a label, then open editor
show_task_context "$id"
local label_prompt=""
read -rp "[annn] Label (optional, e.g. status, link — Enter to skip): " label_prompt
if [[ -n "$label_prompt" ]]; then
if [[ "${label_prompt,,}" == "note" ]]; then
note_action "$id" ""
return
fi
if ! is_label "$label_prompt"; then
die "Invalid label '$label_prompt' — use letters, digits, hyphens only"
fi
fi
make_tmpfile "$id"
[[ -n "$label_prompt" ]] && populate_tmpfile_for_label "$label_prompt"
$EDITOR "$tmpfile"
if [[ ! -s "$tmpfile" ]]; then
msg "Empty annotation, nothing saved."
return
fi
annot="$(cat "$tmpfile" | sed 's/[[:space:]]*$//')"
# Reject if only the pre-populated "label: " remains with no content
if [[ -n "$label_prompt" && "$annot" == "${label_prompt}:" ]]; then
msg "Empty annotation, nothing saved."
return
fi
fi
if task rc.verbose=nothing rc.confirmation=off "$id" annotate "$annot"; then
msg "Annotation added to task $id"
else
echo ""
msg "Error saving annotation."
if [[ -n "${tmpfile:-}" && -f "${tmpfile:-}" ]]; then
msg "Your text:"
echo "---"
cat "$tmpfile"
echo "---"
fi
fi
}
# ============================================================================
# Edit existing annotation — with optional pre-populated text appended
# ============================================================================
edit_annotation() {
local id="$1"
local index="$2"
local inline_text="${3:-}" # optional text to append before editing
local count
count=$(get_annotation_count "$id")
if [[ "$count" -eq 0 ]]; then
die "No annotations on task $id"
fi
if [[ "$index" -lt 1 || "$index" -gt "$count" ]]; then
die "Annotation .$index out of range (task has $count annotation(s))"
fi
local old_text
old_text=$(task_get "$id.annotations.${index}.description" 2>/dev/null)
if [[ -z "$old_text" ]]; then
die "Could not read annotation .$index"
fi
show_task_context "$id"
make_tmpfile "$id"
# Write existing annotation text
echo "$old_text" > "$tmpfile"
# Append inline text on a new line if supplied
if [[ -n "$inline_text" ]]; then
echo "" >> "$tmpfile"
echo "$inline_text" >> "$tmpfile"
fi
$EDITOR "$tmpfile"
local new_text
new_text=$(cat "$tmpfile")
if [[ "$new_text" == "$old_text" ]]; then
msg "No changes made."
return
fi
if [[ -z "$new_text" ]]; then
msg "Annotation emptied — removing it."
task rc.verbose=nothing rc.confirmation=off "$id" denotate "$old_text"
return
fi
if task rc.verbose=nothing rc.confirmation=off "$id" denotate "$old_text" 2>/dev/null; then
if task rc.verbose=nothing rc.confirmation=off "$id" annotate "$new_text"; then
msg "Annotation .$index updated on task $id"
else
echo ""
msg "Error saving new annotation. Old was removed. Your text:"
echo "---"
cat "$tmpfile"
echo "---"
fi
else
msg "Warning: could not remove old annotation. No changes made."
fi
}
# ============================================================================
# Find annotations by label — returns matching index numbers (one per line)
# ============================================================================
find_by_label() {
local id="$1"
local label="${2,,}" # lowercase for case-insensitive match
local count
count=$(get_annotation_count "$id")
local i=1
while [[ $i -le $count ]]; do
local desc
desc=$(task_get "$id.annotations.${i}.description" 2>/dev/null)
if [[ "${desc,,}" =~ ^${label}:[[:space:]] ]]; then
echo "$i"
fi
i=$(( i + 1 ))
done
}
# ============================================================================
# Create a new annotation pre-populated with a label prefix
# ============================================================================
new_labeled_annotation() {
local id="$1"
local label="$2"
local inline_text="${3:-}"
if [[ ! -t 0 ]]; then
# Pipe: prepend label to piped content
local content
content="$(cat)"
if [[ -z "$content" ]]; then
msg "Empty input, nothing saved."
return
fi
if task rc.verbose=nothing rc.confirmation=off "$id" annotate "${label}: ${content}"; then
msg "Annotation added to task $id"
else
msg "Error saving annotation."
fi
return
fi
if [[ -n "$inline_text" ]]; then
# Inline text: prepend label
if task rc.verbose=nothing rc.confirmation=off "$id" annotate "${label}: ${inline_text}"; then
msg "Annotation added to task $id"
else
msg "Error saving annotation."
fi
return
fi
# Interactive: pre-populate editor with template or bare "label: "
show_task_context "$id"
make_tmpfile "$id"
populate_tmpfile_for_label "$label"
$EDITOR "$tmpfile"
local annot
annot="$(cat "$tmpfile" | sed 's/[[:space:]]*$//')"
if [[ -z "$annot" || "$annot" == "${label}:" ]]; then
msg "Empty annotation, nothing saved."
return
fi
if task rc.verbose=nothing rc.confirmation=off "$id" annotate "$annot"; then
msg "Annotation added to task $id"
else
msg "Error saving annotation."
fi
}
# ============================================================================
# Dispatch action by label — create, edit, or select from duplicates
# ============================================================================
label_action() {
local id="$1"
local label="$2"
local inline_text="${3:-}"
mapfile -t matches < <(find_by_label "$id" "$label")
local count="${#matches[@]}"
if [[ $count -eq 0 ]]; then
new_labeled_annotation "$id" "$label" "$inline_text"
elif [[ $count -eq 1 ]]; then
edit_annotation "$id" "${matches[0]}" "$inline_text"
else
# Multiple — list and prompt for selection
local desc
desc=$(task_get "$id.description" 2>/dev/null)
echo "Task $id: $desc"
echo "Multiple '${label}:' annotations — select one:"
echo "---"
for idx in "${matches[@]}"; do
local ann_desc ann_entry date_str
ann_desc=$(task_get "$id.annotations.${idx}.description" 2>/dev/null)
ann_entry=$(task_get "$id.annotations.${idx}.entry" 2>/dev/null)
date_str=$(date -d "$ann_entry" "+%Y-%m-%d %H:%M" 2>/dev/null) || date_str="$ann_entry"
printf " .%-3d [%s]\n" "$idx" "$date_str"
echo "$ann_desc" | sed 's/^/ /'
echo ""
done
read -rp "Edit which? [.N or Enter to cancel]: " sel
sel="${sel#.}"
[[ -z "$sel" ]] && { msg "Cancelled."; return; }
local valid=false
for idx in "${matches[@]}"; do
[[ "$idx" == "$sel" ]] && valid=true && break
done
$valid || die ".$sel is not one of the matching annotations"
edit_annotation "$id" "$sel" "$inline_text"
fi
}
# ============================================================================
# Remove annotation by label (with confirmation) — handles duplicates
# ============================================================================
remove_by_label() {
local id="$1"
local label="$2"
mapfile -t matches < <(find_by_label "$id" "$label")
local count="${#matches[@]}"
if [[ $count -eq 0 ]]; then
die "No '${label}:' annotation found on task $id"
elif [[ $count -eq 1 ]]; then
remove_annotation "$id" "${matches[0]}"
else
local desc
desc=$(task_get "$id.description" 2>/dev/null)
echo "Task $id: $desc"
echo "Multiple '${label}:' annotations — select one to remove:"
echo "---"
for idx in "${matches[@]}"; do
local ann_desc ann_entry date_str
ann_desc=$(task_get "$id.annotations.${idx}.description" 2>/dev/null)
ann_entry=$(task_get "$id.annotations.${idx}.entry" 2>/dev/null)
date_str=$(date -d "$ann_entry" "+%Y-%m-%d %H:%M" 2>/dev/null) || date_str="$ann_entry"
printf " .%-3d [%s]\n" "$idx" "$date_str"
echo "$ann_desc" | sed 's/^/ /'
echo ""
done
read -rp "Remove which? [.N or Enter to cancel]: " sel
sel="${sel#.}"
[[ -z "$sel" ]] && { msg "Cancelled."; return; }
local valid=false
for idx in "${matches[@]}"; do
[[ "$idx" == "$sel" ]] && valid=true && break
done
$valid || die ".$sel is not one of the matching annotations"
remove_annotation "$id" "$sel"
fi
}
# ============================================================================
# Remove annotation (with confirmation) — index required
# ============================================================================
remove_annotation() {
local id="$1"
local index="$2"
local count
count=$(get_annotation_count "$id")
if [[ "$count" -eq 0 ]]; then
die "No annotations on task $id"
fi
if [[ "$index" -lt 1 || "$index" -gt "$count" ]]; then
die "Annotation .$index out of range (task has $count annotation(s))"
fi
local ann_text
ann_text=$(task_get "$id.annotations.${index}.description" 2>/dev/null)
if [[ -z "$ann_text" ]]; then
die "Could not read annotation .$index"
fi
local desc
desc=$(task_get "$id.description" 2>/dev/null)
echo "Task $id: $desc"
echo "---"
local ann_entry
ann_entry=$(task_get "$id.annotations.${index}.entry" 2>/dev/null)
local date_str="$ann_entry"
if command -v date &>/dev/null && [[ -n "$ann_entry" ]]; then
date_str=$(date -d "$ann_entry" "+%Y-%m-%d %H:%M" 2>/dev/null) || date_str="$ann_entry"
fi
printf " .%-3d [%s]\n" "$index" "$date_str"
echo "$ann_text" | sed 's/^/ /'
echo ""
read -rp "Remove this annotation? [y/N]: " response
if [[ "${response,,}" != "y" ]]; then
msg "Cancelled."
return
fi
if task rc.verbose=nothing rc.confirmation=off "$id" denotate "$ann_text"; then
msg "Annotation .$index removed from task $id"
else
msg "Error: could not remove annotation .$index"
fi
}
# ============================================================================
# Task notes — note: label with file storage in ~/.task/notes/
# File: ~/.task/notes/<slug>-<uuid8>.note.md
# ============================================================================
NOTES_DIR="${HOME}/.task/notes"
get_note_file() {
local id="$1"
local desc uuid slug
desc=$(task_get "$id.description")
uuid=$(task_get "$id.uuid")
slug=$(sanitize_for_filename "${desc:-task}")
echo "${NOTES_DIR}/${slug}-${uuid:0:8}.note.md"
}
update_note_annotation() {
local id="$1"
local note_file="$2"
local first_line
first_line=$(grep -m1 '.' "$note_file" 2>/dev/null || echo "")
# Remove any existing note: annotation(s)
local idxs old_text
mapfile -t idxs < <(find_by_label "$id" "note")
for idx in "${idxs[@]}"; do
old_text=$(task_get "$id.annotations.${idx}.description")
task rc.verbose=nothing rc.confirmation=off "$id" denotate "$old_text" 2>/dev/null || true
done
if ! task rc.verbose=nothing rc.confirmation=off "$id" annotate "note: ${first_line}"; then
msg "ERROR: annotation update failed. Note file preserved at: $note_file"
fi
}
note_action() {
local id="$1"
local inline_text="${2:-}"
local note_file
note_file=$(get_note_file "$id")
mkdir -p "$NOTES_DIR"
local is_new=true
[[ -f "$note_file" ]] && is_new=false
if [[ ! -t 0 ]]; then
# Pipe: create or append
local content
content="$(cat)"
if [[ -z "$content" ]]; then
msg "Empty input, nothing saved."
return
fi
if $is_new; then
echo "$content" > "$note_file"
else
echo "" >> "$note_file"
echo "$content" >> "$note_file"
fi
update_note_annotation "$id" "$note_file"
msg "Note updated for task $id"
return
fi
if [[ -n "$inline_text" ]]; then
# Inline: create with template+text, or append to existing
if $is_new; then
local template
template=$(load_template "note")
if [[ -n "$template" ]]; then
cp "$template" "$note_file"
echo "" >> "$note_file"
fi
echo "$inline_text" >> "$note_file"
else
echo "" >> "$note_file"
echo "$inline_text" >> "$note_file"
fi
update_note_annotation "$id" "$note_file"
msg "Note updated for task $id"
return
fi
# Interactive: open note file directly (with template if new)
show_task_context "$id"
if $is_new; then
local template
template=$(load_template "note")
[[ -n "$template" ]] && cp "$template" "$note_file"
fi
$EDITOR "$note_file"
if [[ ! -f "$note_file" || ! -s "$note_file" ]]; then
rm -f "$note_file"
msg "Empty note, nothing saved."
return
fi
update_note_annotation "$id" "$note_file"
if $is_new; then
msg "Note created: $note_file"
else
msg "Note updated: $note_file"
fi
}
remove_note() {
local id="$1"
local note_file
note_file=$(get_note_file "$id")
local desc
desc=$(task_get "$id.description")
echo "Task $id: $desc"
echo "---"
if [[ -f "$note_file" ]]; then
echo "Note file: $note_file"
printf " %s\n" "$(head -1 "$note_file")"
else
echo "(No note file at $note_file)"
fi
echo ""
read -rp "Remove note annotation and file? [y/N]: " response
if [[ "${response,,}" != "y" ]]; then
msg "Cancelled."
return
fi
local idxs old_text removed=0
mapfile -t idxs < <(find_by_label "$id" "note")
for idx in "${idxs[@]}"; do
old_text=$(task_get "$id.annotations.${idx}.description")
if task rc.verbose=nothing rc.confirmation=off "$id" denotate "$old_text" 2>/dev/null; then
removed=$(( removed + 1 ))
fi
done
[[ $removed -gt 0 ]] && msg "Note annotation removed from task $id"
if [[ -f "$note_file" ]]; then
rm "$note_file"
msg "Note file deleted: $note_file"
fi
}
# ============================================================================
# Main
# ============================================================================
# Handle flags (only when stdin is a terminal — pipes shouldn't trigger usage)
if [[ -t 0 ]]; then
case "${1:-}" in
-h|--help) usage ;;
-v|--version) echo "annn v$VERSION"; exit 0 ;;
"") usage ;;
esac
else
if [[ $# -eq 0 ]]; then
die "Usage: echo \"text\" | $SCRIPT_NAME <id>"
fi
fi
# ============================================================================
# Argument parsing
#
# Strict: arg1 must be integer, integer+dot, integer+dot+N, -integer.N, or UUID.
# No filter expressions, project:, +tag, or bare words permitted as ID.
#
# Accepted forms:
# annn 42 → new annotation (editor)
# annn 42 text... → new annotation (inline, no editor)
# annn 42. → list annotations
# annn 42.3 → edit annotation 3 (editor)
# annn 42.3 text... → edit annotation 3 (editor, text appended)
# annn -42.3 → remove annotation 3
# ============================================================================
arg1="${1:-}"
shift
# Validate arg1 before anything else
validate_id_arg "$arg1"
remove_mode=false
filter=""
action="new"
ann_index=""
ann_label=""
if [[ "$arg1" =~ ^-([0-9]+)\.([a-zA-Z][a-zA-Z0-9_-]*)$ ]]; then
# Remove by label: -42.status
filter="${BASH_REMATCH[1]}"
ann_label="${BASH_REMATCH[2]}"
remove_mode=true
action="label"
elif [[ "$arg1" =~ ^-([0-9]+)\.([0-9]+)$ ]]; then
# Remove by index: -42.3
filter="${BASH_REMATCH[1]}"
ann_index="${BASH_REMATCH[2]}"
remove_mode=true
elif [[ "$arg1" =~ ^-([0-9]+)(\.?)$ ]]; then
# -42 or -42. without an index — ambiguous, require index
die "Remove requires an annotation index or label: use -${BASH_REMATCH[1]}.<N> or -${BASH_REMATCH[1]}.<label>"
elif [[ "$arg1" =~ ^([0-9]+)\.([a-zA-Z][a-zA-Z0-9_-]*)$ ]]; then
# Label action: 42.status
filter="${BASH_REMATCH[1]}"
ann_label="${BASH_REMATCH[2]}"
action="label"
elif [[ "$arg1" =~ ^([0-9]+)\.([0-9]+)$ ]]; then
# Edit by index: 42.3
filter="${BASH_REMATCH[1]}"
ann_index="${BASH_REMATCH[2]}"
action="edit"
elif [[ "$arg1" =~ ^([0-9]+)\.$ ]]; then
# List: 42.
filter="${BASH_REMATCH[1]}"
action="list"
elif is_integer "$arg1" || is_uuid "$arg1"; then
# Plain ID — new annotation (or inline if args follow)
filter="$arg1"
action="new"
else
die "Invalid ID: '$arg1' — must be a plain integer or UUID"
fi
# Resolve to confirmed task ID
id=$(resolve_id "$filter")
# Collect any remaining args as inline text
inline_text=""
if [[ $# -gt 0 ]]; then
inline_text="$*"
fi
# ============================================================================
# Dispatch
# ============================================================================
if [[ "$remove_mode" == true ]]; then
if [[ "$action" == "label" ]]; then
if [[ "${ann_label,,}" == "note" ]]; then
remove_note "$id"
else
remove_by_label "$id" "$ann_label"
fi
elif ! [[ "$ann_index" =~ ^[0-9]+$ ]]; then
die "Invalid annotation index: .$ann_index"
else
remove_annotation "$id" "$ann_index"
fi
else
case "$action" in
list)
list_annotations "$id"
;;
edit)
if ! [[ "$ann_index" =~ ^[0-9]+$ ]]; then
die "Invalid annotation index: .$ann_index"
fi
edit_annotation "$id" "$ann_index" "$inline_text"
;;
label)
if [[ "${ann_label,,}" == "note" ]]; then
note_action "$id" "$inline_text"
else
label_action "$id" "$ann_label" "$inline_text"
fi
;;
new)
new_annotation "$id" "$inline_text"
;;
*)
die "Unknown action: $action"
;;
esac
fi