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.
This commit is contained in:
@@ -5,18 +5,37 @@ test_description='list project functionality
|
|||||||
. ./test-lib.sh
|
. ./test-lib.sh
|
||||||
|
|
||||||
cat > todo.txt <<EOF
|
cat > todo.txt <<EOF
|
||||||
(B) smell the uppercase Roses +roses @outside
|
(B) smell the uppercase Roses +roses @outside +shared
|
||||||
(C) notice the sunflowers +sunflowers @garden
|
(C) notice the sunflowers +sunflowers @garden +shared +landscape
|
||||||
stop
|
stop
|
||||||
EOF
|
EOF
|
||||||
test_todo_session 'basic listproj' <<EOF
|
test_todo_session 'basic listproj' <<EOF
|
||||||
>>> todo.sh listproj
|
>>> todo.sh listproj
|
||||||
|
+landscape
|
||||||
+roses
|
+roses
|
||||||
|
+shared
|
||||||
+sunflowers
|
+sunflowers
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
test_todo_session 'listproj with context' <<EOF
|
test_todo_session 'listproj with context' <<EOF
|
||||||
>>> todo.sh listproj @garden
|
>>> 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
|
+sunflowers
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
|||||||
66
todo.sh
66
todo.sh
@@ -633,6 +633,37 @@ _addto() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 \"$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 \"${search_term:1}\""
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
[ -n "$post_filter" ] && {
|
||||||
|
filter="${filter:-}${filter:+ | }${post_filter:-}"
|
||||||
|
}
|
||||||
|
|
||||||
|
printf %s "$filter"
|
||||||
|
}
|
||||||
|
|
||||||
_list() {
|
_list() {
|
||||||
local FILE="$1"
|
local FILE="$1"
|
||||||
## If the file starts with a "/" use absolute path. Otherwise,
|
## If the file starts with a "/" use absolute path. Otherwise,
|
||||||
@@ -656,32 +687,8 @@ _list() {
|
|||||||
## Get our search arguments, if any
|
## Get our search arguments, if any
|
||||||
shift ## was file name, new $1 is first search term
|
shift ## was file name, new $1 is first search term
|
||||||
|
|
||||||
## Prefix the filter_command with the pre_filter_command
|
## Build the filter.
|
||||||
filter_command="${pre_filter_command:-}"
|
filter_command=$(filtercommand "${pre_filter_command:-}" "${post_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:-}"
|
|
||||||
}
|
|
||||||
|
|
||||||
## Figure out how much padding we need to use
|
## Figure out how much padding we need to use
|
||||||
## We need one level of padding for each power of 10 $LINES uses
|
## We need one level of padding for each power of 10 $LINES uses
|
||||||
@@ -752,7 +759,7 @@ _list() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
export -f cleaninput _list die
|
export -f cleaninput filtercommand _list die
|
||||||
|
|
||||||
# == HANDLE ACTION ==
|
# == HANDLE ACTION ==
|
||||||
action=$( printf "%s\n" "$ACTION" | tr 'A-Z' 'a-z' )
|
action=$( printf "%s\n" "$ACTION" | tr 'A-Z' 'a-z' )
|
||||||
@@ -1008,10 +1015,7 @@ case $action in
|
|||||||
|
|
||||||
"listproj" | "lsprj" )
|
"listproj" | "lsprj" )
|
||||||
shift
|
shift
|
||||||
|
eval $(filtercommand 'cat "$TODO_FILE"' '' "$@") | grep -o '[^ ]*+[^ ]\+' | grep '^+' | sort -u
|
||||||
export TODOTXT_PLAIN=1
|
|
||||||
_list "$TODO_FILE" "$@" | grep -o '[^ ]*+[^ ]\+' | grep '^+' | \
|
|
||||||
sed "s:\[[0-9;]*[mK]::g" | sort -u
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
"listpri" | "lsp" )
|
"listpri" | "lsp" )
|
||||||
|
|||||||
Reference in New Issue
Block a user