Skip to content

Commit 861ad5e

Browse files
inkarkatkarbassi
andauthored
Fix/noeol (todotxt#295)
* Handle missing EOL in todo.txt This can happen easily with certain editors (such as Mousepad) that do not automatically add a newline character at the end of a file. In _addto(), ensure a trailing newline via sed (taken from https://unix.stackexchange.com/a/31955/18876). Fixes todotxt#294 * Tests: Add basic coverage of move * Handle missing EOL in todo.txt for move, too This can happen easily with certain editors (such as Mousepad) that do not automatically add a newline character at the end of a file. * Refactoring: Extract fixMissingEndOfLine() * FIX: Compatibility: sed \+ multi not supported on MacOS Use the POSIX \{1,\} instead. Co-authored-by: Ali Karbassi <[email protected]>
1 parent 13b451b commit 861ad5e

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

tests/t1000-addlist.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,17 @@ TODO: 1 added.
110110
TODO: 1 of 1 tasks shown
111111
EOF
112112

113+
echo -n 'this is a first task without newline' > todo.txt
114+
test_todo_session 'add to file without EOL' <<EOF
115+
>>> todo.sh add "a second task"
116+
2 a second task
117+
TODO: 2 added.
118+
119+
>>> todo.sh list
120+
2 a second task
121+
1 this is a first task without newline
122+
--
123+
TODO: 2 of 2 tasks shown
124+
EOF
125+
113126
test_done

tests/t1850-move.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
#
3+
4+
test_description='basic move functionality
5+
'
6+
. ./test-lib.sh
7+
8+
cat > todo.txt <<EOF
9+
(B) smell the uppercase Roses +flowers @outside
10+
(A) notice the sunflowers
11+
EOF
12+
cat > done.txt <<EOF
13+
x 2009-02-13 make the coffee +wakeup
14+
x 2009-02-13 smell the coffee +wakeup
15+
EOF
16+
test_todo_session 'basic move with implicit source' <<EOF
17+
>>> todo.sh -f move 1 done.txt | sed "s#'[^']\{1,\}/\([^/']\{1,\}\)'#'\1'#g"
18+
1 (B) smell the uppercase Roses +flowers @outside
19+
TODO: 1 moved from 'todo.txt' to 'done.txt'.
20+
21+
>>> todo.sh -p ls
22+
2 (A) notice the sunflowers
23+
--
24+
TODO: 1 of 1 tasks shown
25+
26+
>>> todo.sh -p listfile done.txt
27+
3 (B) smell the uppercase Roses +flowers @outside
28+
1 x 2009-02-13 make the coffee +wakeup
29+
2 x 2009-02-13 smell the coffee +wakeup
30+
--
31+
DONE: 3 of 3 tasks shown
32+
EOF
33+
34+
test_todo_session 'basic move with passed source' <<EOF
35+
>>> todo.sh -f move 2 todo.txt done.txt | sed "s#'[^']\{1,\}/\([^/']\{1,\}\)'#'\1'#g"
36+
2 x 2009-02-13 smell the coffee +wakeup
37+
TODO: 2 moved from 'done.txt' to 'todo.txt'.
38+
39+
>>> todo.sh -p ls
40+
2 (A) notice the sunflowers
41+
3 x 2009-02-13 smell the coffee +wakeup
42+
--
43+
TODO: 2 of 2 tasks shown
44+
45+
>>> todo.sh -p listfile done.txt
46+
3 (B) smell the uppercase Roses +flowers @outside
47+
1 x 2009-02-13 make the coffee +wakeup
48+
--
49+
DONE: 2 of 2 tasks shown
50+
EOF
51+
52+
echo -n 'this is a first task without newline' > todo.txt
53+
cat > done.txt <<EOF
54+
x 2009-02-13 make the coffee +wakeup
55+
x 2009-02-13 smell the coffee +wakeup
56+
EOF
57+
test_todo_session 'move to destination without EOL' <<EOF
58+
>>> todo.sh -f move 2 todo.txt done.txt | sed "s#'[^']\{1,\}/\([^/']\{1,\}\)'#'\1'#g"
59+
2 x 2009-02-13 smell the coffee +wakeup
60+
TODO: 2 moved from 'done.txt' to 'todo.txt'.
61+
62+
>>> todo.sh -p ls
63+
1 this is a first task without newline
64+
2 x 2009-02-13 smell the coffee +wakeup
65+
--
66+
TODO: 2 of 2 tasks shown
67+
68+
>>> todo.sh -p listfile done.txt
69+
1 x 2009-02-13 make the coffee +wakeup
70+
--
71+
DONE: 1 of 1 tasks shown
72+
EOF
73+
74+
test_done

todo.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ replaceOrPrepend()
466466
fi
467467
}
468468

469+
fixMissingEndOfLine()
470+
{
471+
# Parameters: $1: todo file; empty means $TODO_FILE.
472+
sed -i.bak -e '$a\' "${1:-$TODO_FILE}"
473+
}
474+
469475
uppercasePriority()
470476
{
471477
# Precondition: $input contains task text for which to uppercase priority.
@@ -809,6 +815,7 @@ _addto() {
809815
input=$(echo -n "($TODOTXT_PRIORITY_ON_ADD) " ; echo "$input")
810816
fi
811817
fi
818+
fixMissingEndOfLine "$file"
812819
echo "$input" >> "$file"
813820
if [ "$TODOTXT_VERBOSE" -gt 0 ]; then
814821
TASKNUM=$(sed -n '$ =' "$file")
@@ -1339,6 +1346,7 @@ case $action in
13391346
# leave blank line behind (preserves line numbers)
13401347
sed -i.bak -e "${item}s/^.*//" "$src"
13411348
fi
1349+
fixMissingEndOfLine "$dest"
13421350
echo "$todo" >> "$dest"
13431351

13441352
if [ "$TODOTXT_VERBOSE" -gt 0 ]; then

0 commit comments

Comments
 (0)