Compare commits
6 Commits
archive/li
...
archive/ne
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7ffb96695 | ||
|
|
67e0d9dd98 | ||
|
|
1160ae1276 | ||
|
|
534184e4dd | ||
|
|
cd2f585fb6 | ||
|
|
492d98e50a |
35
tests/t1900-archive.sh
Executable file
35
tests/t1900-archive.sh
Executable file
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
|
||||
test_description='archive functionality
|
||||
|
||||
Ensure we can archive items successfully.
|
||||
'
|
||||
. ./test-lib.sh
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
one
|
||||
two
|
||||
three
|
||||
one
|
||||
x done
|
||||
four
|
||||
EOF
|
||||
|
||||
test_todo_session 'archive with duplicates' <<EOF
|
||||
>>> todo.sh archive
|
||||
x done
|
||||
TODO: $HOME/todo.txt archived.
|
||||
EOF
|
||||
|
||||
test_todo_session 'list after archive' <<EOF
|
||||
>>> todo.sh ls
|
||||
5 four
|
||||
1 one
|
||||
4 one
|
||||
3 three
|
||||
2 two
|
||||
--
|
||||
TODO: 5 of 5 tasks shown
|
||||
EOF
|
||||
|
||||
test_done
|
||||
103
tests/t1910-deduplicate.sh
Executable file
103
tests/t1910-deduplicate.sh
Executable file
@@ -0,0 +1,103 @@
|
||||
#!/bin/sh
|
||||
|
||||
test_description='deduplicate functionality
|
||||
|
||||
Ensure we can deduplicate items successfully.
|
||||
'
|
||||
. ./test-lib.sh
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
duplicated
|
||||
two
|
||||
x done
|
||||
duplicated
|
||||
double task
|
||||
double task
|
||||
three
|
||||
EOF
|
||||
|
||||
test_todo_session 'deduplicate and preserve line numbers' <<EOF
|
||||
>>> todo.sh deduplicate
|
||||
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
|
||||
4 double task
|
||||
1 duplicated
|
||||
5 three
|
||||
2 two
|
||||
3 x done
|
||||
--
|
||||
TODO: 5 of 5 tasks shown
|
||||
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: 3 duplicate task(s) removed
|
||||
|
||||
>>> todo.sh -p ls
|
||||
2 duplicated
|
||||
1 one
|
||||
6 six
|
||||
3 three
|
||||
--
|
||||
TODO: 4 of 4 tasks shown
|
||||
EOF
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
normal task
|
||||
a [1mbold[0m action
|
||||
something else
|
||||
a [1mbold[0m action
|
||||
something more
|
||||
EOF
|
||||
test_todo_session 'deduplicate with non-printable duplicates' <<EOF
|
||||
>>> todo.sh deduplicate
|
||||
TODO: 1 duplicate task(s) removed
|
||||
|
||||
>>> todo.sh -p ls
|
||||
2 a [1mbold[0m action
|
||||
1 normal task
|
||||
3 something else
|
||||
5 something more
|
||||
--
|
||||
TODO: 4 of 4 tasks shown
|
||||
EOF
|
||||
|
||||
test_done
|
||||
@@ -85,10 +85,12 @@ TODO: $HOME/todo.txt archived.
|
||||
TODO: 5 of 5 tasks shown
|
||||
|
||||
>>> todo.sh report
|
||||
TODO: $HOME/todo.txt archived.
|
||||
TODO: Report file updated.
|
||||
2009-02-13-04:40:00 5 1
|
||||
|
||||
>>> todo.sh report
|
||||
TODO: $HOME/todo.txt archived.
|
||||
TODO: Report file updated.
|
||||
2009-02-13-04:40:00 5 1
|
||||
2009-02-13-04:40:00 5 1
|
||||
|
||||
83
todo.sh
83
todo.sh
@@ -50,6 +50,7 @@ shorthelp()
|
||||
append|app ITEM# "TEXT TO APPEND"
|
||||
archive
|
||||
command [ACTIONS]
|
||||
deduplicate
|
||||
del|rm ITEM# [TERM]
|
||||
depri|dp ITEM#[, ITEM#, ITEM#, ...]
|
||||
do ITEM#[, ITEM#, ITEM#, ...]
|
||||
@@ -180,6 +181,9 @@ help()
|
||||
Runs the remaining arguments using only todo.sh builtins.
|
||||
Will not call any .todo.actions.d scripts.
|
||||
|
||||
deduplicate
|
||||
Removes duplicate lines from todo.txt.
|
||||
|
||||
del ITEM# [TERM]
|
||||
rm ITEM# [TERM]
|
||||
Deletes the task on line ITEM# in todo.txt.
|
||||
@@ -353,20 +357,6 @@ getNewtodo()
|
||||
[ -z "$newtodo" ] && die "$(getPrefix "$2"): No updated task $item."
|
||||
}
|
||||
|
||||
archive()
|
||||
{
|
||||
#defragment blank lines
|
||||
sed -i.bak -e '/./!d' "$TODO_FILE"
|
||||
[ $TODOTXT_VERBOSE -gt 0 ] && grep "^x " "$TODO_FILE"
|
||||
grep "^x " "$TODO_FILE" >> "$DONE_FILE"
|
||||
sed -i.bak '/^x /d' "$TODO_FILE"
|
||||
cp "$TODO_FILE" "$TMP_FILE"
|
||||
sed -n 'G; s/\n/&&/; /^\([ ~-]*\n\).*\n\1/d; s/\n//; h; P' "$TMP_FILE" > "$TODO_FILE"
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
echo "TODO: $TODO_FILE archived."
|
||||
fi
|
||||
}
|
||||
|
||||
replaceOrPrepend()
|
||||
{
|
||||
action=$1; shift
|
||||
@@ -775,12 +765,8 @@ _list() {
|
||||
fi
|
||||
items=$(
|
||||
sed = "$src" \
|
||||
| sed '''
|
||||
N
|
||||
s/^/ /
|
||||
s/ *\([ 0-9]\{'"$PADDING"',\}\)\n/\1 /
|
||||
/^[ 0-9]\+ *$/d
|
||||
'''
|
||||
| sed "N; s/^/ /; s/ *\(.\{$PADDING,\}\)\n/\1 /" \
|
||||
| grep -v "^[ 0-9]\+ *$"
|
||||
)
|
||||
if [ "${filter_command}" ]; then
|
||||
filtered_items=$(echo -n "$items" | eval "${filter_command}")
|
||||
@@ -937,7 +923,15 @@ case $action in
|
||||
;;
|
||||
|
||||
"archive" )
|
||||
archive;;
|
||||
# defragment blank lines
|
||||
sed -i.bak -e '/./!d' "$TODO_FILE"
|
||||
[ $TODOTXT_VERBOSE -gt 0 ] && grep "^x " "$TODO_FILE"
|
||||
grep "^x " "$TODO_FILE" >> "$DONE_FILE"
|
||||
sed -i.bak '/^x /d' "$TODO_FILE"
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
echo "TODO: $TODO_FILE archived."
|
||||
fi
|
||||
;;
|
||||
|
||||
"del" | "rm" )
|
||||
# replace deleted line with a blank line when TODOTXT_PRESERVE_LINE_NUMBERS is 1
|
||||
@@ -1039,7 +1033,9 @@ case $action in
|
||||
done
|
||||
|
||||
if [ $TODOTXT_AUTO_ARCHIVE = 1 ]; then
|
||||
archive
|
||||
# Recursively invoke the script to allow overriding of the archive
|
||||
# action.
|
||||
"$TODO_FULL_SH" archive
|
||||
fi
|
||||
;;
|
||||
|
||||
@@ -1191,9 +1187,10 @@ note: PRIORITY must be anywhere from A to Z."
|
||||
;;
|
||||
|
||||
"report" )
|
||||
#archive first
|
||||
sed '/^x /!d' "$TODO_FILE" >> "$DONE_FILE"
|
||||
sed -i.bak '/^x /d' "$TODO_FILE"
|
||||
# archive first
|
||||
# Recursively invoke the script to allow overriding of the archive
|
||||
# action.
|
||||
"$TODO_FULL_SH" archive
|
||||
|
||||
NUMLINES=$( sed -n '$ =' "$TODO_FILE" )
|
||||
if [ ${NUMLINES:-0} = "0" ]; then
|
||||
@@ -1209,6 +1206,42 @@ note: PRIORITY must be anywhere from A to Z."
|
||||
cat "$REPORT_FILE"
|
||||
;;
|
||||
|
||||
"deduplicate" )
|
||||
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.
|
||||
# We start with an empty hold space on the first line. For each line:
|
||||
# G - appends newline + hold space to the pattern space
|
||||
# s/\n/&&/; - double up the first new line so we catch adjacent dups
|
||||
# /^\([^\n]*\n\).*\n\1/
|
||||
# If the first line of the hold space shows up again later as an
|
||||
# entire line, it's a duplicate.
|
||||
# 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
|
||||
# h; - replace the hold space with the expanded pattern space
|
||||
# P; - print up to the first newline (that is, the input line)
|
||||
sed -i.bak -n 'G; s/\n/&&/; /^\([^\n]*\n\).*\n\1/'"$deduplicateSedCommand"'; s/\n//; h; P' "$TODO_FILE"
|
||||
|
||||
newTaskNum=$( sed -e '/./!d' "$TODO_FILE" | sed -n '$ =' )
|
||||
deduplicateNum=$(( originalTaskNum - newTaskNum ))
|
||||
if [ $deduplicateNum -eq 0 ]; then
|
||||
echo "TODO: No duplicate tasks found"
|
||||
else
|
||||
echo "TODO: $deduplicateNum duplicate task(s) removed"
|
||||
fi
|
||||
;;
|
||||
|
||||
* )
|
||||
usage;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user