Compare commits
18 Commits
archive/bu
...
archive/tr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4359290b3c | ||
|
|
f8a6e5f8d6 | ||
|
|
388ae745af | ||
|
|
cb908bd454 | ||
|
|
55679d136f | ||
|
|
c0847b0b25 | ||
|
|
76fb1cb3ee | ||
|
|
ac090fa30b | ||
|
|
cd7d2f2fda | ||
|
|
395465b5f2 | ||
|
|
d2e0f6e38d | ||
|
|
b1d894f65d | ||
|
|
f5270be463 | ||
|
|
8cba7533b9 | ||
|
|
285d5039f2 | ||
|
|
9b67a76833 | ||
|
|
2b0921f4ec | ||
|
|
202f892e85 |
2
Makefile
2
Makefile
@@ -13,7 +13,7 @@ VERSION-FILE: .FORCE-VERSION-FILE
|
||||
todo.sh: VERSION-FILE
|
||||
|
||||
# For packaging
|
||||
DISTFILES := todo.cfg
|
||||
DISTFILES := todo.cfg todo_completion
|
||||
|
||||
DISTNAME=todo.txt_cli-$(VERSION)
|
||||
dist: $(DISTFILES) todo.sh
|
||||
|
||||
@@ -39,6 +39,24 @@ GARDEN: 2 added.
|
||||
GARDEN: 2 of 2 tasks shown
|
||||
EOF
|
||||
|
||||
#
|
||||
# List available files
|
||||
#
|
||||
test_todo_session 'list available files' <<EOF
|
||||
>>> todo.sh listfile
|
||||
Files in the todo.txt directory:
|
||||
done.txt
|
||||
garden.txt
|
||||
report.txt
|
||||
todo.txt
|
||||
|
||||
>>> TODOTXT_VERBOSE=0 todo.sh listfile
|
||||
done.txt
|
||||
garden.txt
|
||||
report.txt
|
||||
todo.txt
|
||||
EOF
|
||||
|
||||
#
|
||||
# Filter
|
||||
#
|
||||
|
||||
@@ -66,6 +66,12 @@ TODO: 4 added.
|
||||
TODO: 4 of 4 tasks shown
|
||||
EOF
|
||||
|
||||
test_todo_session 'priority error' <<EOF
|
||||
>>> todo.sh pri 10 B
|
||||
=== 1
|
||||
TODO: No task 10.
|
||||
EOF
|
||||
|
||||
cat > todo.txt <<EOF
|
||||
(B) smell the uppercase Roses +flowers @outside
|
||||
(C) notice the sunflowers
|
||||
|
||||
150
todo.sh
150
todo.sh
@@ -57,7 +57,7 @@ shorthelp()
|
||||
list|ls [TERM...]
|
||||
listall|lsa [TERM...]
|
||||
listcon|lsc
|
||||
listfile|lf SRC [TERM...]
|
||||
listfile|lf [SRC [TERM...]]
|
||||
listpri|lsp [PRIORITY] [TERM...]
|
||||
listproj|lsprj [TERM...]
|
||||
move|mv ITEM# DEST [SRC]
|
||||
@@ -211,11 +211,13 @@ help()
|
||||
lsc
|
||||
Lists all the task contexts that start with the @ sign in todo.txt.
|
||||
|
||||
listfile SRC [TERM...]
|
||||
lf SRC [TERM...]
|
||||
listfile [SRC [TERM...]]
|
||||
lf [SRC [TERM...]]
|
||||
Displays all the lines in SRC file located in the todo.txt directory,
|
||||
sorted by priority with line numbers. If TERM specified, lists
|
||||
all lines that contain TERM in SRC file.
|
||||
Without any arguments, the names of all text files in the todo.txt
|
||||
directory are listed.
|
||||
|
||||
listpri [PRIORITY] [TERM...]
|
||||
lsp [PRIORITY] [TERM...]
|
||||
@@ -294,6 +296,11 @@ cleanup()
|
||||
|
||||
cleaninput()
|
||||
{
|
||||
# Parameters: When $1 = "for sed", performs additional escaping for use
|
||||
# in sed substitution with "|" separators.
|
||||
# Precondition: $input contains text to be cleaned.
|
||||
# Postcondition: Modifies $input.
|
||||
|
||||
# Replace CR and LF with space; tasks always comprise a single line.
|
||||
input=${input//$'\r'/ }
|
||||
input=${input//$'\n'/ }
|
||||
@@ -308,6 +315,44 @@ cleaninput()
|
||||
fi
|
||||
}
|
||||
|
||||
getPrefix()
|
||||
{
|
||||
# Parameters: $1: todo file; empty means $TODO_FILE.
|
||||
# Returns: Uppercase FILE prefix to be used in place of "TODO:" where
|
||||
# a different todo file can be specified.
|
||||
local base=$(basename "${1:-$TODO_FILE}")
|
||||
echo "${base%%.[^.]*}" | tr 'a-z' 'A-Z'
|
||||
}
|
||||
|
||||
getTodo()
|
||||
{
|
||||
# Parameters: $1: task number
|
||||
# $2: Optional todo file
|
||||
# Precondition: $errmsg contains usage message.
|
||||
# Postcondition: $todo contains task text.
|
||||
|
||||
local item=$1
|
||||
[ -z "$item" ] && die "$errmsg"
|
||||
[ "${item//[0-9]/}" ] && die "$errmsg"
|
||||
|
||||
todo=$(sed "$item!d" "${2:-$TODO_FILE}")
|
||||
[ -z "$todo" ] && die "$(getPrefix "$2"): No task $item."
|
||||
}
|
||||
getNewtodo()
|
||||
{
|
||||
# Parameters: $1: task number
|
||||
# $2: Optional todo file
|
||||
# Precondition: None.
|
||||
# Postcondition: $newtodo contains task text.
|
||||
|
||||
local item=$1
|
||||
[ -z "$item" ] && die 'Programming error: $item should exist.'
|
||||
[ "${item//[0-9]/}" ] && die 'Programming error: $item should be numeric.'
|
||||
|
||||
newtodo=$(sed "$item!d" "${2:-$TODO_FILE}")
|
||||
[ -z "$newtodo" ] && die "$(getPrefix "$2"): No updated task $item."
|
||||
}
|
||||
|
||||
archive()
|
||||
{
|
||||
#defragment blank lines
|
||||
@@ -336,12 +381,7 @@ replaceOrPrepend()
|
||||
;;
|
||||
esac
|
||||
shift; item=$1; shift
|
||||
|
||||
[ -z "$item" ] && die "$errmsg"
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
|
||||
todo=$(sed "$item!d" "$TODO_FILE")
|
||||
[ -z "$todo" ] && die "TODO: No task $item."
|
||||
getTodo "$item"
|
||||
|
||||
if [[ -z "$1" && $TODOTXT_FORCE = 0 ]]; then
|
||||
echo -n "$querytext"
|
||||
@@ -366,7 +406,7 @@ replaceOrPrepend()
|
||||
# date again.
|
||||
sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE"
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
newtodo=$(sed "$item!d" "$TODO_FILE")
|
||||
getNewtodo "$item"
|
||||
case "$action" in
|
||||
replace)
|
||||
echo "$item $todo"
|
||||
@@ -656,10 +696,8 @@ _addto() {
|
||||
echo "$input" >> "$file"
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
TASKNUM=$(sed -n '$ =' "$file")
|
||||
BASE=$(basename "$file")
|
||||
PREFIX=$(echo ${BASE%%.[^.]*} | tr 'a-z' 'A-Z')
|
||||
echo "$TASKNUM $input"
|
||||
echo "${PREFIX}: $TASKNUM added."
|
||||
echo "$(getPrefix "$file"): $TASKNUM added."
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -782,20 +820,18 @@ _list() {
|
||||
[ "$filtered_items" ] && echo "$filtered_items"
|
||||
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
BASE=$(basename "$FILE")
|
||||
PREFIX=$(echo ${BASE%%.[^.]*} | tr 'a-z' 'A-Z')
|
||||
NUMTASKS=$( echo -n "$filtered_items" | sed -n '$ =' )
|
||||
TOTALTASKS=$( echo -n "$items" | sed -n '$ =' )
|
||||
|
||||
echo "--"
|
||||
echo "${PREFIX}: ${NUMTASKS:-0} of ${TOTALTASKS:-0} tasks shown"
|
||||
echo "$(getPrefix "$FILE"): ${NUMTASKS:-0} of ${TOTALTASKS:-0} tasks shown"
|
||||
fi
|
||||
if [ $TODOTXT_VERBOSE -gt 1 ]; then
|
||||
echo "TODO DEBUG: Filter Command was: ${filter_command:-cat}"
|
||||
fi
|
||||
}
|
||||
|
||||
export -f cleaninput shellquote filtercommand _list die
|
||||
export -f cleaninput getPrefix getTodo getNewtodo shellquote filtercommand _list die
|
||||
|
||||
# == HANDLE ACTION ==
|
||||
action=$( printf "%s\n" "$ACTION" | tr 'A-Z' 'a-z' )
|
||||
@@ -872,11 +908,8 @@ case $action in
|
||||
"append" | "app" )
|
||||
errmsg="usage: $TODO_SH append ITEM# \"TEXT TO APPEND\""
|
||||
shift; item=$1; shift
|
||||
getTodo "$item"
|
||||
|
||||
[ -z "$item" ] && die "$errmsg"
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
todo=$(sed "$item!d" "$TODO_FILE")
|
||||
[ -z "$todo" ] && die "TODO: No task $item."
|
||||
if [[ -z "$1" && $TODOTXT_FORCE = 0 ]]; then
|
||||
echo -n "Append: "
|
||||
read input
|
||||
@@ -891,7 +924,7 @@ case $action in
|
||||
|
||||
if sed -i.bak $item" s|^.*|&${appendspace}${input}|" "$TODO_FILE"; then
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
newtodo=$(sed "$item!d" "$TODO_FILE")
|
||||
getNewtodo "$item"
|
||||
echo "$item $newtodo"
|
||||
fi
|
||||
else
|
||||
@@ -906,14 +939,11 @@ case $action in
|
||||
# replace deleted line with a blank line when TODOTXT_PRESERVE_LINE_NUMBERS is 1
|
||||
errmsg="usage: $TODO_SH del ITEM# [TERM]"
|
||||
item=$2
|
||||
[ -z "$item" ] && die "$errmsg"
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
DELETEME=$(sed "$item!d" "$TODO_FILE")
|
||||
[ -z "$DELETEME" ] && die "TODO: No task $item."
|
||||
getTodo "$item"
|
||||
|
||||
if [ -z "$3" ]; then
|
||||
if [ $TODOTXT_FORCE = 0 ]; then
|
||||
echo "Delete '$DELETEME'? (y/n)"
|
||||
echo "Delete '$todo'? (y/n)"
|
||||
read ANSWER
|
||||
else
|
||||
ANSWER="y"
|
||||
@@ -927,7 +957,7 @@ case $action in
|
||||
sed -i.bak -e $item"s/^.*//" "$TODO_FILE"
|
||||
fi
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
echo "$item $DELETEME"
|
||||
echo "$item $todo"
|
||||
echo "TODO: $item deleted."
|
||||
fi
|
||||
else
|
||||
@@ -941,13 +971,13 @@ case $action in
|
||||
-e $item"s/ *$3 */ /g" \
|
||||
-e $item"s/$3//g" \
|
||||
"$TODO_FILE"
|
||||
newtodo=$(sed "$item!d" "$TODO_FILE")
|
||||
if [ "$DELETEME" = "$newtodo" ]; then
|
||||
[ $TODOTXT_VERBOSE -gt 0 ] && echo "$item $DELETEME"
|
||||
getNewtodo "$item"
|
||||
if [ "$todo" = "$newtodo" ]; then
|
||||
[ $TODOTXT_VERBOSE -gt 0 ] && echo "$item $todo"
|
||||
die "TODO: '$3' not found; no removal done."
|
||||
fi
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
echo "$item $DELETEME"
|
||||
echo "$item $todo"
|
||||
echo "TODO: Removed '$3' from task."
|
||||
echo "$item $newtodo"
|
||||
fi
|
||||
@@ -961,16 +991,14 @@ case $action in
|
||||
|
||||
# Split multiple depri's, if comma separated change to whitespace separated
|
||||
# Loop the 'depri' function for each item
|
||||
for item in $(echo $* | tr ',' ' '); do
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
todo=$(sed "$item!d" "$TODO_FILE")
|
||||
[ -z "$todo" ] && die "TODO: No task $item."
|
||||
for item in ${*//,/ }; do
|
||||
getTodo "$item"
|
||||
|
||||
if [[ "$todo" = \(?\)\ * ]]; then
|
||||
sed -i.bak -e $item"s/^(.) //" "$TODO_FILE"
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
NEWTODO=$(sed "$item!d" "$TODO_FILE")
|
||||
echo "$item $NEWTODO"
|
||||
getNewtodo "$item"
|
||||
echo "$item $newtodo"
|
||||
echo "TODO: $item deprioritized."
|
||||
fi
|
||||
else
|
||||
@@ -987,12 +1015,8 @@ case $action in
|
||||
|
||||
# Split multiple do's, if comma separated change to whitespace separated
|
||||
# Loop the 'do' function for each item
|
||||
for item in $(echo $* | tr ',' ' '); do
|
||||
[ -z "$item" ] && die "$errmsg"
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
|
||||
todo=$(sed "$item!d" "$TODO_FILE")
|
||||
[ -z "$todo" ] && die "TODO: No task $item."
|
||||
for item in ${*//,/ }; do
|
||||
getTodo "$item"
|
||||
|
||||
# Check if this item has already been done
|
||||
if [ "${todo:0:2}" != "x " ]; then
|
||||
@@ -1001,7 +1025,7 @@ case $action in
|
||||
sed -i.bak $item"s/^(.) //" "$TODO_FILE"
|
||||
sed -i.bak $item"s|^|x $now |" "$TODO_FILE"
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
newtodo=$(sed "$item!d" "$TODO_FILE")
|
||||
getNewtodo "$item"
|
||||
echo "$item $newtodo"
|
||||
echo "TODO: $item marked as done."
|
||||
fi
|
||||
@@ -1049,10 +1073,15 @@ case $action in
|
||||
|
||||
"listfile" | "lf" )
|
||||
shift ## Was listfile, next $1 is file name
|
||||
FILE="$1"
|
||||
shift ## Was filename; next $1 is first search term
|
||||
if [ $# -eq 0 ]; then
|
||||
[ $TODOTXT_VERBOSE -gt 0 ] && echo "Files in the todo.txt directory:"
|
||||
cd "$TODO_DIR" && ls -1 *.txt
|
||||
else
|
||||
FILE="$1"
|
||||
shift ## Was filename; next $1 is first search term
|
||||
|
||||
_list "$FILE" "$@"
|
||||
_list "$FILE" "$@"
|
||||
fi
|
||||
;;
|
||||
|
||||
"listcon" | "lsc" )
|
||||
@@ -1079,19 +1108,16 @@ case $action in
|
||||
dest="$TODO_DIR/$3"
|
||||
src="$TODO_DIR/$4"
|
||||
|
||||
[ -z "$item" ] && die "$errmsg"
|
||||
[ -z "$4" ] && src="$TODO_FILE"
|
||||
[ -z "$dest" ] && die "$errmsg"
|
||||
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
|
||||
[ -f "$src" ] || die "TODO: Source file $src does not exist."
|
||||
[ -f "$dest" ] || die "TODO: Destination file $dest does not exist."
|
||||
|
||||
MOVEME=$(sed "$item!d" "$src")
|
||||
[ -z "$MOVEME" ] && die "$item: No such item in $src."
|
||||
getTodo "$item" "$src"
|
||||
[ -z "$todo" ] && die "$item: No such item in $src."
|
||||
if [ $TODOTXT_FORCE = 0 ]; then
|
||||
echo "Move '$MOVEME' from $src to $dest? (y/n)"
|
||||
echo "Move '$todo' from $src to $dest? (y/n)"
|
||||
read ANSWER
|
||||
else
|
||||
ANSWER="y"
|
||||
@@ -1104,10 +1130,10 @@ case $action in
|
||||
# leave blank line behind (preserves line numbers)
|
||||
sed -i.bak -e $item"s/^.*//" "$src"
|
||||
fi
|
||||
echo "$MOVEME" >> "$dest"
|
||||
echo "$todo" >> "$dest"
|
||||
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
echo "$item $MOVEME"
|
||||
echo "$item $todo"
|
||||
echo "TODO: $item moved from '$src' to '$dest'."
|
||||
fi
|
||||
else
|
||||
@@ -1128,16 +1154,20 @@ case $action in
|
||||
note: PRIORITY must be anywhere from A to Z."
|
||||
|
||||
[ "$#" -ne 3 ] && die "$errmsg"
|
||||
[[ "$item" = +([0-9]) ]] || die "$errmsg"
|
||||
[[ "$newpri" = @([A-Z]) ]] || die "$errmsg"
|
||||
getTodo "$item"
|
||||
|
||||
oldpri=
|
||||
if [[ "$todo" = \(?\)\ * ]]; then
|
||||
oldpri=${todo:1:1}
|
||||
fi
|
||||
|
||||
oldpri=$(sed -ne $item's/^(\(.\)) .*/\1/p' "$TODO_FILE")
|
||||
if [ "$oldpri" != "$newpri" ]; then
|
||||
sed -i.bak -e $item"s/^(.) //" -e $item"s/^/($newpri) /" "$TODO_FILE"
|
||||
fi
|
||||
if [ $TODOTXT_VERBOSE -gt 0 ]; then
|
||||
NEWTODO=$(sed "$item!d" "$TODO_FILE")
|
||||
echo "$item $NEWTODO"
|
||||
getNewtodo "$item"
|
||||
echo "$item $newtodo"
|
||||
if [ "$oldpri" != "$newpri" ]; then
|
||||
if [ "$oldpri" ]; then
|
||||
echo "TODO: $item re-prioritized from ($oldpri) to ($newpri)."
|
||||
|
||||
71
todo_completion
Normal file
71
todo_completion
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/bin/bash source-this-script
|
||||
[ "$BASH_VERSION" ] || return
|
||||
|
||||
_todo()
|
||||
{
|
||||
local cur prev opts
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
|
||||
local -r OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x"
|
||||
local -r COMMANDS="\
|
||||
add a addto addm append app archive command del \
|
||||
rm depri dp do help list ls listall lsa listcon \
|
||||
lsc listfile lf listpri lsp listproj lsprj move \
|
||||
mv prepend prep pri p replace report shorthelp"
|
||||
|
||||
# Add custom commands from add-ons, if installed.
|
||||
# TODO: Filter for executable flag of files found in $TODO_ACTIONS_DIR.
|
||||
local allCommands="$COMMANDS $('ls' "${TODO_ACTIONS_DIR:-$HOME/.todo.actions.d}/" 2>/dev/null)"
|
||||
|
||||
local completions
|
||||
if [ $COMP_CWORD -eq 1 ]; then
|
||||
completions="$allCommands $OPTS"
|
||||
elif [[ $COMP_CWORD -gt 2 && ( \
|
||||
"${COMP_WORDS[COMP_CWORD-2]}" =~ ^(move|mv)$ || \
|
||||
"${COMP_WORDS[COMP_CWORD-3]}" =~ ^(move|mv)$ ) ]]; then
|
||||
# "move ITEM# DEST [SRC]" has file arguments on positions 2 and 3.
|
||||
completions=$(TODOTXT_VERBOSE=0 todo.sh command listfile)
|
||||
else
|
||||
case "$prev" in
|
||||
command)
|
||||
completions=$COMMANDS;;
|
||||
addto|listfile|lf)
|
||||
completions=$(TODOTXT_VERBOSE=0 todo.sh command listfile);;
|
||||
-*) completions="$allCommands $OPTS";;
|
||||
*) case "$cur" in
|
||||
+*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listproj);;
|
||||
@*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listcon);;
|
||||
*) if [[ "$cur" =~ ^[0-9]+$ ]]; then
|
||||
local item=$(TODOTXT_VERBOSE=0 todo.sh -@ -+ -p -x command ls "^ *${cur} " | head -n 1)
|
||||
|
||||
# Remove the (padded) task number; we prepend the
|
||||
# user-provided $cur.
|
||||
item=${item#* }
|
||||
|
||||
# Remove the timestamp prepended by the -t option;
|
||||
# there's no todo.txt option for that yet.
|
||||
item=${item#[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] }
|
||||
|
||||
# Append task text as a shell comment. This
|
||||
# completion can be a safety check before a
|
||||
# destructive todo.txt operation.
|
||||
[ "$item" ] && COMPREPLY[0]="$cur # $item"
|
||||
return 0
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
COMPREPLY=( $( compgen -W "$completions" -- $cur ))
|
||||
return 0
|
||||
}
|
||||
complete -F _todo todo.sh
|
||||
# If you define an alias (e.g. "t") to todo.sh, you need to explicitly enable
|
||||
# completion for it, too:
|
||||
#complete -F _todo t
|
||||
Reference in New Issue
Block a user