Compare commits

...

22 Commits

Author SHA1 Message Date
Ingo Karkat
cd7d2f2fda ENH: Add file completion for move. 2011-11-20 17:07:59 +01:00
Ingo Karkat
395465b5f2 ENH: Add file completion for addto and listfile.
This enhancement to todo_completion requires a small enhancement to the listfile action: When no SRC is specified, the list of text files in the todo.txt directory is printed. This is probably also useful on its own, and better than the original behavior of printing "TODO: File  does not exist."

Note: I intentionally omitted bullet-proof error handling ($TODO_DIR non-existing or no text files contained), to avoid over-complicating this.
2011-11-20 16:30:31 +01:00
Ingo Karkat
d2e0f6e38d FIX: Support $HOME and $TODOTXT_ACTIONS_DIR containing spaces.
And document a minor discrepancy in action completion to todo.sh.
2011-11-20 15:17:50 +01:00
Ingo Karkat
b1d894f65d Add recently added "shorthelp" built-in command. 2011-11-20 15:17:50 +01:00
Ingo Karkat
f5270be463 ENH: Append task text as shell comment when completing task number.
This is useful for the paranoid before a destructive todo.txt operation. Appending the text as a shell comment doesn't affect the todo.txt command itself, but shows that the task number corresponds to the task you had in mind.
2011-11-20 15:17:45 +01:00
Ingo Karkat
8cba7533b9 Avoid leaking internal variables into the shell + robustness against add-ons.
Use "local" to avoid that the internal completion variables are accessible from the user's shell.
Use "todo.sh command" for the context/project lookups to avoid interference with custom add-ons of the same name, and reset TODOTXT_VERBOSE to avoid adding any message output (currently there is none).
2011-11-20 14:53:18 +01:00
Ingo Karkat
285d5039f2 Add shebang line and check for Bash.
The shebang is ignored when sourcing the script (but still helps many text editors auto-detect the file type), and will cause an error when the script is mistakenly executed.
The Bash check allows to have this called from a generic place (e.g. .profile), and do no harm when under a different shell.
2011-11-20 14:26:52 +01:00
Ingo Karkat
9b67a76833 Do not automatically complete for guessed aliases.
Don't infringe against the principle of least astonishment (they user may have completely unrelated aliases). Rather, if the user sets up his own alias, make him apply the same to todo_completion.
2011-11-20 14:22:48 +01:00
Ingo Karkat
2b0921f4ec Add todo_completion to todo.txt distribution. 2011-11-20 14:20:25 +01:00
Ingo Karkat
202f892e85 Include todo Bash completion from the todo.txt Wiki.
This is f32aba2 of https://github.com/ginatrapani/todo.txt-cli/wiki/Tips-and-Tricks, last edited 30-Oct-2011.
2011-11-20 14:17:22 +01:00
Ingo Karkat
417e86ae7a FIX: Handle special shell characters when filtering TERM.
The double quotes used in the filter_command erroneously expand $VARIABLE, and due to missing quoting in the eval() of filter_command, multiple spaces are condensed into a single space.
Introduce a new function shellquote() to correctly quote each filter TERM.
2011-11-10 14:21:47 -08:00
Ingo Karkat
cb982391de Implement listproj with factored out filtercommand() function.
_list() is way too large and monolithic for many (re-)use cases. As a first step, factor out the building of the filter_command and reuse that for the listproj filtering.
Enhance the listproj test with special cases that show the problems with the previous implementation directly using _list: Option -+, custom final filters, and non-ANSI colors cause it to break.
2011-11-10 14:21:47 -08:00
jmoore
b8244792cc Accept filters for lsprj 2011-11-10 14:21:47 -08:00
Ingo Karkat
a433c5828e shorthelp consistency
- Restore alphabetical order for "addto".
- Keep "depri" shorthand after the long form as all other.
Closes #50

Implement shorthelp listing add-on action one-line usage.
-h and an additional new "shorthelp" action list not just the usage of the built-in actions, but also from add-ons. This assumes that add-ons use the same usage indentation structure as todo.sh. (They should, anyway, for consistency of the full help message.)
Closes #12
2011-11-10 14:16:05 -08:00
Ingo Karkat
7742d3c115 Restructure help text to put built-in and add-on actions together.
I find it annoying that the built-in actions are printed first, and the add-on actions last, although for the user, the distinction is hardly important. Therefore, moving the "options" block first (as it is short and contains the stuff most difficult to memorize), then built-in, then add-on actions.
As environment variables are hardly used in day-to-day operations, only once for customizing the config, they are now omitted by default and only included when -vv is given.
2011-11-10 14:11:31 -08:00
Gina Trapani
7ca2b4c687 Merge pull request #49 from inkarkat/bug-disable-filter-not-propagating
BUG: disable filter not propagating to add-ons
2011-11-10 13:33:46 -08:00
Gina Trapani
239f7234ab Merge pull request #48 from inkarkat/bug-unrestrained-pri-matching
BUG: unrestrained pri "(X)" matching
2011-11-10 13:33:28 -08:00
Ingo Karkat
e3fce6f12b Revert to safer POSIX AWK regexp.
AWK from Ubuntu 8.04 (mawk) doesn't support [[:space:]]; so for backwards compatibility use a plain ASCII space instead.
2011-11-03 18:37:36 +01:00
Ingo Karkat
65d39319f1 TODOTXT_DISABLE_FILTER missing from help text.
Should be in there for consistency; corresponding option is -x.
2011-09-30 09:15:56 +02:00
Ingo Karkat
62d3ff2977 BUG: Option -x isn't propagated to custom actions.
Somehow, no default is set for TODOTXT_DISABLE_FILTER, so that it isn't exported, and therefore does not apply to the sourced actions, so you cannot disable filtering for "myaction" via todo.sh -x myaction.
2011-09-30 09:11:54 +02:00
Ingo Karkat
227b9d2c0a BUG: Pattern "(X) " anywhere hidden with -P.
The regular expression HIDE_PRIORITY_SUBSTITUTION is not anchored, so strings that look like a priority but are not at the beginning are filtered, too.
Anchoring the regexp in the step after the highlighting has been applied is problematic due to the prepended dynamic priority highlighting string, and it also cannot be done before that because highlighting needs the information. Therefore, the filtering is moved into the AWK highlighting itself.
2011-09-23 16:27:30 +02:00
Ingo Karkat
99fa15da36 BUG: Pattern "(X)" anywhere highlighted as prioritized.
According to the "Todo.txt Format" specs, "Rule 1: If a priority exists, it ALWAYS appears first."

Adapt AWK filtering to match priorities only directly after the task number added by the _list processing, and also matching the required trailing space.
2011-09-23 15:40:23 +02:00
7 changed files with 367 additions and 125 deletions

View File

@@ -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

View File

@@ -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
#

42
tests/t1260-listprj.sh Executable file
View File

@@ -0,0 +1,42 @@
#!/bin/sh
test_description='list project functionality
'
. ./test-lib.sh
cat > todo.txt <<EOF
(B) smell the uppercase Roses +roses @outside +shared
(C) notice the sunflowers +sunflowers @garden +shared +landscape
stop
EOF
test_todo_session 'basic listproj' <<EOF
>>> todo.sh listproj
+landscape
+roses
+shared
+sunflowers
EOF
test_todo_session 'listproj with context' <<EOF
>>> todo.sh listproj @garden
+landscape
+shared
+sunflowers
EOF
TEST_TODO_CUSTOM=todo-custom.cfg
cat todo.cfg > "$TEST_TODO_CUSTOM"
cat >> "$TEST_TODO_CUSTOM" <<'EOF'
export DEFAULT='</color>'
export PRI_B='<color type=green>'
export PRI_C='<color type=blue>'
export TODOTXT_FINAL_FILTER='grep -i roses'
EOF
test_todo_session 'listproj with context special cases' <<EOF
>>> todo.sh -+ -d "$TEST_TODO_CUSTOM" listproj @garden
+landscape
+shared
+sunflowers
EOF
test_done

View File

@@ -94,6 +94,40 @@ TODO: 1 of 3 tasks shown
TODO: 1 of 3 tasks shown
EOF
#
# check the filtering of TERM containing characters that are special to the
# shell, like variables, quotes, and multiple subsequent spaces.
#
cat > todo.txt <<'EOF'
earn some pennies
earn some $$
earn some "money"
get money from O'Brian
just get money!
EOF
test_todo_session 'checking filtering of special characters' <<'EOF'
>>> todo.sh ls '$$'
2 earn some $$
--
TODO: 1 of 5 tasks shown
>>> todo.sh ls '"money"'
3 earn some "money"
--
TODO: 1 of 5 tasks shown
>>> todo.sh ls "O'Brian"
4 get money from O'Brian
--
TODO: 1 of 5 tasks shown
>>> todo.sh ls "get money"
5 just get money!
--
TODO: 1 of 5 tasks shown
EOF
#
# check the x command line option
#
@@ -142,7 +176,7 @@ cat > todo.txt <<EOF
(D) @con02 +prj03 -- Some project 03 task, pri D
(D) @con02 +prj04 -- Some project 04 task, pri D
@con01 +prj01 -- Some project 01 task, no priority
@con01 +prj02 -- Some project 02 task, no priority
@con01 +prj02 -- Some project(S) 02 task, no priority
@con02 +prj03 -- Some project 03 task, no priorty
@con02 +prj04 -- Some project 04 task, no priority
EOF
@@ -165,7 +199,7 @@ test_todo_session 'plain mode option' <<EOF
15 (D) @con02 +prj03 -- Some project 03 task, pri D
16 (D) @con02 +prj04 -- Some project 04 task, pri D
17 @con01 +prj01 -- Some project 01 task, no priority
18 @con01 +prj02 -- Some project 02 task, no priority
18 @con01 +prj02 -- Some project(S) 02 task, no priority
19 @con02 +prj03 -- Some project 03 task, no priorty
20 @con02 +prj04 -- Some project 04 task, no priority
--
@@ -189,7 +223,7 @@ TODO: 20 of 20 tasks shown
15 (D) @con02 +prj03 -- Some project 03 task, pri D
16 (D) @con02 +prj04 -- Some project 04 task, pri D
17 @con01 +prj01 -- Some project 01 task, no priority
18 @con01 +prj02 -- Some project 02 task, no priority
18 @con01 +prj02 -- Some project(S) 02 task, no priority
19 @con02 +prj03 -- Some project 03 task, no priorty
20 @con02 +prj04 -- Some project 04 task, no priority
--
@@ -217,7 +251,7 @@ cat > todo.txt <<EOF
(D) @con02 +prj03 -- Some project 03 task, pri D
(D) @con02 +prj04 -- Some project 04 task, pri D
@con01 +prj01 -- Some project 01 task, no priority
@con01 +prj02 -- Some project 02 task, no priority
@con01 +prj02 -- Some project(S) 02 task, no priority
@con02 +prj03 -- Some project 03 task, no priorty
@con02 +prj04 -- Some project 04 task, no priority
EOF
@@ -240,7 +274,7 @@ test_todo_session 'context, project, and priority suppression' <<EOF
15 (D) @con02 +prj03 -- Some project 03 task, pri D
16 (D) @con02 +prj04 -- Some project 04 task, pri D
17 @con01 +prj01 -- Some project 01 task, no priority
18 @con01 +prj02 -- Some project 02 task, no priority
18 @con01 +prj02 -- Some project(S) 02 task, no priority
19 @con02 +prj03 -- Some project 03 task, no priorty
20 @con02 +prj04 -- Some project 04 task, no priority
--
@@ -256,7 +290,7 @@ TODO: 20 of 20 tasks shown
13 (D) @con01 +prj01 -- Some project 01 task, pri D
14 (D) @con01 +prj02 -- Some project 02 task, pri D
17 @con01 +prj01 -- Some project 01 task, no priority
18 @con01 +prj02 -- Some project 02 task, no priority
18 @con01 +prj02 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
@@ -270,7 +304,7 @@ TODO: 10 of 20 tasks shown
13 @con01 +prj01 -- Some project 01 task, pri D
14 @con01 +prj02 -- Some project 02 task, pri D
17 @con01 +prj01 -- Some project 01 task, no priority
18 @con01 +prj02 -- Some project 02 task, no priority
18 @con01 +prj02 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
@@ -284,7 +318,7 @@ TODO: 10 of 20 tasks shown
13 (D) @con01 -- Some project 01 task, pri D
14 (D) @con01 -- Some project 02 task, pri D
17 @con01 -- Some project 01 task, no priority
18 @con01 -- Some project 02 task, no priority
18 @con01 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
@@ -298,7 +332,7 @@ TODO: 10 of 20 tasks shown
13 (D) +prj01 -- Some project 01 task, pri D
14 (D) +prj02 -- Some project 02 task, pri D
17 +prj01 -- Some project 01 task, no priority
18 +prj02 -- Some project 02 task, no priority
18 +prj02 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
@@ -312,7 +346,7 @@ TODO: 10 of 20 tasks shown
13 +prj01 -- Some project 01 task, pri D
14 +prj02 -- Some project 02 task, pri D
17 +prj01 -- Some project 01 task, no priority
18 +prj02 -- Some project 02 task, no priority
18 +prj02 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
@@ -326,7 +360,7 @@ TODO: 10 of 20 tasks shown
13 (D) @con01 +prj01 -- Some project 01 task, pri D
14 (D) @con01 +prj02 -- Some project 02 task, pri D
17 @con01 +prj01 -- Some project 01 task, no priority
18 @con01 +prj02 -- Some project 02 task, no priority
18 @con01 +prj02 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
@@ -340,7 +374,7 @@ TODO: 10 of 20 tasks shown
13 -- Some project 01 task, pri D
14 -- Some project 02 task, pri D
17 -- Some project 01 task, no priority
18 -- Some project 02 task, no priority
18 -- Some project(S) 02 task, no priority
--
TODO: 10 of 20 tasks shown
EOF

View File

@@ -166,4 +166,20 @@ test_todo_session 'highlighting with hidden contexts/projects' <<EOF
TODO: 4 of 4 tasks shown
EOF
# check that priorities are only matched at the start of the task
#
cat > todo.txt <<EOF
(D) some prioritized task
not prioritized
should not be seen as PRIORITIZE(D) task
EOF
test_todo_session 'highlighting priority position' <<EOF
>>> todo.sh ls
1 (D) some prioritized task
2 not prioritized
3 should not be seen as PRIORITIZE(D) task
--
TODO: 3 of 3 tasks shown
EOF
test_done

285
todo.sh
View File

@@ -44,39 +44,113 @@ shorthelp()
Actions:
add|a "THING I NEED TO DO +project @context"
addto DEST "TEXT TO ADD"
addm "THINGS I NEED TO DO
MORE THINGS I NEED TO DO"
addto DEST "TEXT TO ADD"
append|app ITEM# "TEXT TO APPEND"
archive
command [ACTIONS]
del|rm ITEM# [TERM]
dp|depri ITEM#[, ITEM#, ITEM#, ...]
depri|dp ITEM#[, ITEM#, ITEM#, ...]
do ITEM#[, ITEM#, ITEM#, ...]
help
list|ls [TERM...]
listall|lsa [TERM...]
listcon|lsc
listfile|lf SRC [TERM...]
listfile|lf [SRC [TERM...]]
listpri|lsp [PRIORITY] [TERM...]
listproj|lsprj
listproj|lsprj [TERM...]
move|mv ITEM# DEST [SRC]
prepend|prep ITEM# "TEXT TO PREPEND"
pri|p ITEM# PRIORITY
replace ITEM# "UPDATED TODO"
report
shorthelp
EndHelp
# Only list the one-line usage from the add-on actions. This assumes that
# add-ons use the same usage indentation structure as todo.sh.
addonHelp | grep -e '^ Add-on Actions:' -e '^ [[:alpha:]]'
cat <<-EndHelpFooter
See "help" for more details.
EndHelp
EndHelpFooter
exit 0
}
help()
{
cat <<-EndHelp
cat <<-EndOptionsHelp
Usage: $oneline_usage
Actions:
Options:
-@
Hide context names in list output. Use twice to show context
names (default).
-+
Hide project names in list output. Use twice to show project
names (default).
-c
Color mode
-d CONFIG_FILE
Use a configuration file other than the default ~/.todo/config
-f
Forces actions without confirmation or interactive input
-h
Display a short help message; same as action "shorthelp"
-p
Plain mode turns off colors
-P
Hide priority labels in list output. Use twice to show
priority labels (default).
-a
Don't auto-archive tasks automatically on completion
-A
Auto-archive tasks automatically on completion
-n
Don't preserve line numbers; automatically remove blank lines
on task deletion
-N
Preserve line numbers
-t
Prepend the current date to a task automatically
when it's added.
-T
Do not prepend the current date to a task automatically
when it's added.
-v
Verbose mode turns on confirmation messages
-vv
Extra verbose mode prints some debugging information and
additional help text
-V
Displays version, license and credits
-x
Disables TODOTXT_FINAL_FILTER
EndOptionsHelp
[ $TODOTXT_VERBOSE -gt 1 ] && cat <<-EndVerboseHelp
Environment variables:
TODOTXT_AUTO_ARCHIVE is same as option -a (0)/-A (1)
TODOTXT_CFG_FILE=CONFIG_FILE is same as option -d CONFIG_FILE
TODOTXT_FORCE=1 is same as option -f
TODOTXT_PRESERVE_LINE_NUMBERS is same as option -n (0)/-N (1)
TODOTXT_PLAIN is same as option -p (1)/-c (0)
TODOTXT_DATE_ON_ADD is same as option -t (1)/-T (0)
TODOTXT_VERBOSE=1 is same as option -v
TODOTXT_DISABLE_FILTER=1 is same as option -x
TODOTXT_DEFAULT_ACTION="" run this when called with no arguments
TODOTXT_SORT_COMMAND="sort ..." customize list output
TODOTXT_FINAL_FILTER="sed ..." customize list after color, P@+ hiding
EndVerboseHelp
cat <<-EndActionsHelp
Built-in Actions:
add "THING I NEED TO DO +project @context"
a "THING I NEED TO DO +project @context"
Adds THING I NEED TO DO to your todo.txt file on its own line.
@@ -137,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...]
@@ -177,81 +253,33 @@ help()
report
Adds the number of open tasks and done tasks to report.txt.
shorthelp
List the one-line usage of all built-in and add-on actions.
Options:
-@
Hide context names in list output. Use twice to show context
names (default).
-+
Hide project names in list output. Use twice to show project
names (default).
-c
Color mode
-d CONFIG_FILE
Use a configuration file other than the default ~/.todo/config
-f
Forces actions without confirmation or interactive input
-h
Display a short help message
-p
Plain mode turns off colors
-P
Hide priority labels in list output. Use twice to show
priority labels (default).
-a
Don't auto-archive tasks automatically on completion
-A
Auto-archive tasks automatically on completion
-n
Don't preserve line numbers; automatically remove blank lines
on task deletion
-N
Preserve line numbers
-t
Prepend the current date to a task automatically
when it's added.
-T
Do not prepend the current date to a task automatically
when it's added.
-v
Verbose mode turns on confirmation messages
-vv
Extra verbose mode prints some debugging information
-V
Displays version, license and credits
-x
Disables TODOTXT_FINAL_FILTER
EndActionsHelp
addonHelp
exit 1
}
Environment variables:
TODOTXT_AUTO_ARCHIVE is same as option -a (0)/-A (1)
TODOTXT_CFG_FILE=CONFIG_FILE is same as option -d CONFIG_FILE
TODOTXT_FORCE=1 is same as option -f
TODOTXT_PRESERVE_LINE_NUMBERS is same as option -n (0)/-N (1)
TODOTXT_PLAIN is same as option -p (1)/-c (0)
TODOTXT_DATE_ON_ADD is same as option -t (1)/-T (0)
TODOTXT_VERBOSE=1 is same as option -v
TODOTXT_DEFAULT_ACTION="" run this when called with no arguments
TODOTXT_SORT_COMMAND="sort ..." customize list output
TODOTXT_FINAL_FILTER="sed ..." customize list after color, P@+ hiding
EndHelp
if [ -d "$TODO_ACTIONS_DIR" ]
then
echo ""
addonHelp()
{
if [ -d "$TODO_ACTIONS_DIR" ]; then
didPrintAddonActionsHeader=
for action in "$TODO_ACTIONS_DIR"/*
do
if [ -f "$action" -a -x "$action" ]
then
if [ -f "$action" -a -x "$action" ]; then
if [ ! "$didPrintAddonActionsHeader" ]; then
cat <<-EndAddonActionsHeader
Add-on Actions:
EndAddonActionsHeader
didPrintAddonActionsHeader=1
fi
"$action" usage
fi
done
echo ""
fi
exit 1
}
die()
@@ -418,7 +446,10 @@ do
OVR_TODOTXT_FORCE=1
;;
h )
shorthelp
# Short-circuit option parsing and forward to the action.
# Cannot just invoke shorthelp() because we need the configuration
# processed to locate the add-on actions directory.
set -- '-h' 'shorthelp'
;;
n )
OVR_TODOTXT_PRESERVE_LINE_NUMBERS=0
@@ -474,6 +505,7 @@ TODOTXT_AUTO_ARCHIVE=${TODOTXT_AUTO_ARCHIVE:-1}
TODOTXT_DATE_ON_ADD=${TODOTXT_DATE_ON_ADD:-0}
TODOTXT_DEFAULT_ACTION=${TODOTXT_DEFAULT_ACTION:-}
TODOTXT_SORT_COMMAND=${TODOTXT_SORT_COMMAND:-env LC_COLLATE=C sort -f -k2}
TODOTXT_DISABLE_FILTER=${TODOTXT_DISABLE_FILTER:-}
TODOTXT_FINAL_FILTER=${TODOTXT_FINAL_FILTER:-cat}
# Export all TODOTXT_* variables
@@ -633,6 +665,42 @@ _addto() {
fi
}
shellquote()
{
typeset -r qq=\'; printf %s\\n "'${1//\'/${qq}\\${qq}${qq}}'";
}
filtercommand()
{
filter=${1:-}
shift
post_filter=${1:-}
shift
for search_term
do
## See if the first character of $search_term is a dash
if [ "${search_term:0:1}" != '-' ]
then
## First character isn't a dash: hide lines that don't match
## this $search_term
filter="${filter:-}${filter:+ | }grep -i $(shellquote "$search_term")"
else
## First character is a dash: hide lines that match this
## $search_term
#
## Remove the first character (-) before adding to our filter command
filter="${filter:-}${filter:+ | }grep -v -i '$(shellquote "${search_term:1}")'"
fi
done
[ -n "$post_filter" ] && {
filter="${filter:-}${filter:+ | }${post_filter:-}"
}
printf %s "$filter"
}
_list() {
local FILE="$1"
## If the file starts with a "/" use absolute path. Otherwise,
@@ -656,32 +724,8 @@ _list() {
## Get our search arguments, if any
shift ## was file name, new $1 is first search term
## Prefix the filter_command with the pre_filter_command
filter_command="${pre_filter_command:-}"
for search_term
do
## See if the first character of $search_term is a dash
if [ "${search_term:0:1}" != '-' ]
then
## First character isn't a dash: hide lines that don't match
## this $search_term
filter_command="${filter_command:-} ${filter_command:+|} \
grep -i \"$search_term\" "
else
## First character is a dash: hide lines that match this
## $search_term
#
## Remove the first character (-) before adding to our filter command
filter_command="${filter_command:-} ${filter_command:+|} \
grep -v -i \"${search_term:1}\" "
fi
done
## If post_filter_command is set, append it to the filter_command
[ -n "$post_filter_command" ] && {
filter_command="${filter_command:-}${filter_command:+ | }${post_filter_command:-}"
}
## Build the filter.
filter_command=$(filtercommand "${pre_filter_command:-}" "${post_filter_command:-}" "$@")
## Figure out how much padding we need to use
## We need one level of padding for each power of 10 $LINES uses
@@ -699,7 +743,7 @@ _list() {
| grep -v "^[ 0-9]\+ *$"
)
if [ "${filter_command}" ]; then
filtered_items=$(echo -n "$items" | eval ${filter_command})
filtered_items=$(echo -n "$items" | eval "${filter_command}")
else
filtered_items=$items
fi
@@ -720,17 +764,18 @@ _list() {
return color
}
{
pos = match($0, /\([A-Z]\)/)
if (match($0, /^[0-9]+ x /)) {
print highlight("COLOR_DONE") $0 highlight("DEFAULT")
} else if (pos > 0) {
clr = highlight("PRI_" substr($0, pos+1, 1))
print ( clr ? clr : highlight("PRI_X") ) $0 highlight("DEFAULT")
} else if (match($0, /^[0-9]+ \([A-Z]\)[[:space:]]/)) {
clr = highlight("PRI_" substr($0, RSTART + RLENGTH - 3, 1))
print \
(clr ? clr : highlight("PRI_X")) \
(ENVIRON["HIDE_PRIORITY_SUBSTITUTION"] == "" ? $0 : substr($0, 1, RLENGTH - 4) substr($0, RSTART + RLENGTH)) \
highlight("DEFAULT")
} else { print }
}
''' \
| sed '''
s/'${HIDE_PRIORITY_SUBSTITUTION:-^}'//g
s/'${HIDE_PROJECTS_SUBSTITUTION:-^}'//g
s/'${HIDE_CONTEXTS_SUBSTITUTION:-^}'//g
''' \
@@ -752,7 +797,7 @@ _list() {
fi
}
export -f cleaninput _list die
export -f cleaninput shellquote filtercommand _list die
# == HANDLE ACTION ==
action=$( printf "%s\n" "$ACTION" | tr 'A-Z' 'a-z' )
@@ -982,6 +1027,16 @@ case $action in
help # just in case something failed above, we go ahead and just spew to STDOUT
;;
"shorthelp" )
if [ -t 1 ] ; then # STDOUT is a TTY
if which "${PAGER:-less}" >/dev/null 2>&1; then
# we have a working PAGER (or less as a default)
shorthelp | "${PAGER:-less}" && exit 0
fi
fi
shorthelp # just in case something failed above, we go ahead and just spew to STDOUT
;;
"list" | "ls" )
shift ## Was ls; new $1 is first search term
_list "$TODO_FILE" "$@"
@@ -996,10 +1051,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" )
@@ -1007,7 +1067,8 @@ case $action in
;;
"listproj" | "lsprj" )
grep -o '[^ ]*+[^ ]\+' "$TODO_FILE" | grep '^+' | sort -u
shift
eval "$(filtercommand 'cat "$TODO_FILE"' '' "$@")" | grep -o '[^ ]*+[^ ]\+' | grep '^+' | sort -u
;;
"listpri" | "lsp" )

71
todo_completion Normal file
View 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