Support "preserve line numbers" in deduplicate.
This commit is contained in:
@@ -16,10 +16,38 @@ double task
|
|||||||
three
|
three
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
test_todo_session 'deduplicate with duplicates' <<EOF
|
test_todo_session 'deduplicate and preserve line numbers' <<EOF
|
||||||
>>> todo.sh deduplicate
|
>>> todo.sh deduplicate
|
||||||
TODO: 2 duplicate task(s) removed
|
TODO: 2 duplicate task(s) removed
|
||||||
|
|
||||||
|
>>> todo.sh -p ls
|
||||||
|
5 double task
|
||||||
|
1 duplicated
|
||||||
|
7 three
|
||||||
|
2 two
|
||||||
|
3 x done
|
||||||
|
--
|
||||||
|
TODO: 5 of 5 tasks shown
|
||||||
|
EOF
|
||||||
|
|
||||||
|
test_todo_session 'deduplicate without duplicates' <<EOF
|
||||||
|
>>> todo.sh deduplicate
|
||||||
|
TODO: No duplicate tasks found
|
||||||
|
EOF
|
||||||
|
|
||||||
|
cat > todo.txt <<EOF
|
||||||
|
duplicated
|
||||||
|
two
|
||||||
|
x done
|
||||||
|
duplicated
|
||||||
|
double task
|
||||||
|
double task
|
||||||
|
three
|
||||||
|
EOF
|
||||||
|
test_todo_session 'deduplicate and delete lines' <<EOF
|
||||||
|
>>> todo.sh -n deduplicate
|
||||||
|
TODO: 2 duplicate task(s) removed
|
||||||
|
|
||||||
>>> todo.sh -p ls
|
>>> todo.sh -p ls
|
||||||
4 double task
|
4 double task
|
||||||
1 duplicated
|
1 duplicated
|
||||||
@@ -30,9 +58,26 @@ TODO: 2 duplicate task(s) removed
|
|||||||
TODO: 5 of 5 tasks shown
|
TODO: 5 of 5 tasks shown
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
test_todo_session 'deduplicate without duplicates' <<EOF
|
cat > todo.txt <<EOF
|
||||||
|
one
|
||||||
|
duplicated
|
||||||
|
three
|
||||||
|
duplicated
|
||||||
|
duplicated
|
||||||
|
six
|
||||||
|
duplicated
|
||||||
|
EOF
|
||||||
|
test_todo_session 'deduplicate more than two occurrences' <<EOF
|
||||||
>>> todo.sh deduplicate
|
>>> todo.sh deduplicate
|
||||||
TODO: No duplicate tasks found
|
TODO: 3 duplicate task(s) removed
|
||||||
|
|
||||||
|
>>> todo.sh -p ls
|
||||||
|
2 duplicated
|
||||||
|
1 one
|
||||||
|
6 six
|
||||||
|
3 three
|
||||||
|
--
|
||||||
|
TODO: 4 of 4 tasks shown
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
cat > todo.txt <<EOF
|
cat > todo.txt <<EOF
|
||||||
@@ -50,7 +95,7 @@ TODO: 1 duplicate task(s) removed
|
|||||||
2 a [1mbold[0m action
|
2 a [1mbold[0m action
|
||||||
1 normal task
|
1 normal task
|
||||||
3 something else
|
3 something else
|
||||||
4 something more
|
5 something more
|
||||||
--
|
--
|
||||||
TODO: 4 of 4 tasks shown
|
TODO: 4 of 4 tasks shown
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
24
todo.sh
24
todo.sh
@@ -1208,21 +1208,33 @@ note: PRIORITY must be anywhere from A to Z."
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
"deduplicate" )
|
"deduplicate" )
|
||||||
originalTaskNum=$( sed -n '$ =' "$TODO_FILE" )
|
if [ $TODOTXT_PRESERVE_LINE_NUMBERS = 0 ]; then
|
||||||
|
deduplicateSedCommand='d'
|
||||||
|
else
|
||||||
|
deduplicateSedCommand='{ s/^.*//; p; b }'
|
||||||
|
fi
|
||||||
|
|
||||||
|
# To determine the difference when deduplicated lines are preserved, only
|
||||||
|
# non-empty lines must be counted.
|
||||||
|
originalTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' )
|
||||||
|
|
||||||
# Look for duplicate lines and discard the second occurrence.
|
# Look for duplicate lines and discard the second occurrence.
|
||||||
# We start with an empty hold space on the first line. For each line:
|
# We start with an empty hold space on the first line. For each line:
|
||||||
# G - appends newline + hold space to the pattern space
|
# G - appends newline + hold space to the pattern space
|
||||||
# s/\n/&&/; - double up the first new line so we catch adjacent dups
|
# s/\n/&&/; - double up the first new line so we catch adjacent dups
|
||||||
# /^\([^\n]*\n\).*\n\1/d;
|
# /^\([^\n]*\n\).*\n\1/
|
||||||
# If the first line of the hold space shows up again later as an
|
# If the first line of the hold space shows up again later as an
|
||||||
# entire line, it's a duplicate. Delete the current pattern space,
|
# entire line, it's a duplicate.
|
||||||
# quit this line and move on to the next
|
# d; - Delete the current pattern space, quit this line
|
||||||
|
# and move on to the next, or:
|
||||||
|
# { s/^.*//; p; b }; - Clear the task text, print this line and move on
|
||||||
|
# to the next.
|
||||||
# s/\n//; - else, drop the doubled newline
|
# s/\n//; - else, drop the doubled newline
|
||||||
# h; - replace the hold space with the expanded pattern space
|
# h; - replace the hold space with the expanded pattern space
|
||||||
# P; - print up to the first newline (that is, the input line)
|
# P; - print up to the first newline (that is, the input line)
|
||||||
sed -i.bak -n 'G; s/\n/&&/; /^\([^\n]*\n\).*\n\1/d; s/\n//; h; P' "$TODO_FILE"
|
sed -i.bak -n 'G; s/\n/&&/; /^\([^\n]*\n\).*\n\1/'"$deduplicateSedCommand"'; s/\n//; h; P' "$TODO_FILE"
|
||||||
newTaskNum=$( sed -n '$ =' "$TODO_FILE" )
|
|
||||||
|
newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' )
|
||||||
deduplicateNum=$(( originalTaskNum - newTaskNum ))
|
deduplicateNum=$(( originalTaskNum - newTaskNum ))
|
||||||
if [ $deduplicateNum -eq 0 ]; then
|
if [ $deduplicateNum -eq 0 ]; then
|
||||||
echo "TODO: No duplicate tasks found"
|
echo "TODO: No duplicate tasks found"
|
||||||
|
|||||||
Reference in New Issue
Block a user