Skip to content

Commit

Permalink
Filter todos with --tag <tag> option.
Browse files Browse the repository at this point in the history
refs gh-294
  • Loading branch information
xwmx committed Feb 7, 2024
1 parent 3203845 commit c65f2cd
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6036,12 +6036,12 @@ Usage:
nb todo add [<notebook>:][<folder-path>/][<filename>] <title>
[--description <description>] [--due <date>]
[-r (<url> | <selector>) | --related (<url> | <selector>)]
[--tags <tag1>,<tag2>...] [--task <title>]...
[--tags <tag1>,<tag2>...] [--task <title>...]
nb todo do ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todo undo ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todos [<notebook>:][<folder-path>/] [open | closed]
nb todos [<notebook>:][<folder-path>/] [open | closed] [--tags <tag1>,<tag2>...]
nb todos tasks ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[open | closed]
nb unarchive [<notebook>]
Expand Down Expand Up @@ -8506,12 +8506,12 @@ Usage:
nb todo add [<notebook>:][<folder-path>/][<filename>] <title>
[--description <description>] [--due <date>]
[-r (<url> | <selector>) | --related (<url> | <selector>)]
[--tags <tag1>,<tag2>...] [--task <title>]...
[--tags <tag1>,<tag2>...] [--task <title>...]
nb todo do ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todo undo ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todos [<notebook>:][<folder-path>/] [open | closed]
nb todos [<notebook>:][<folder-path>/] [open | closed] [--tags <tag1>,<tag2>...]
nb todos tasks ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[open | closed]

Expand Down
8 changes: 4 additions & 4 deletions docs/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6042,12 +6042,12 @@ Usage:
nb todo add [<notebook>:][<folder-path>/][<filename>] <title>
[--description <description>] [--due <date>]
[-r (<url> | <selector>) | --related (<url> | <selector>)]
[--tags <tag1>,<tag2>...] [--task <title>]...
[--tags <tag1>,<tag2>...] [--task <title>...]
nb todo do ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todo undo ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todos [<notebook>:][<folder-path>/] [open | closed]
nb todos [<notebook>:][<folder-path>/] [open | closed] [--tags <tag1>,<tag2>...]
nb todos tasks ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[open | closed]
nb unarchive [<notebook>]
Expand Down Expand Up @@ -8512,12 +8512,12 @@ Usage:
nb todo add [<notebook>:][<folder-path>/][<filename>] <title>
[--description <description>] [--due <date>]
[-r (<url> | <selector>) | --related (<url> | <selector>)]
[--tags <tag1>,<tag2>...] [--task <title>]...
[--tags <tag1>,<tag2>...] [--task <title>...]
nb todo do ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todo undo ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
nb todos [<notebook>:][<folder-path>/] [open | closed]
nb todos [<notebook>:][<folder-path>/] [open | closed] [--tags <tag1>,<tag2>...]
nb todos tasks ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[open | closed]

Expand Down
83 changes: 59 additions & 24 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -4415,12 +4415,12 @@ $(_color_primary "Usage"):
${_ME} todo add [<notebook>:][<folder-path>/][<filename>] <title>
[--description <description>] [--due <date>]
[-r (<url> | <selector>) | --related (<url> | <selector>)]
[--tags <tag1>,<tag2>...] [--task <title>]...
[--tags <tag1>,<tag2>...] [--task <title>...]
${_ME} todo do ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
${_ME} todo undo ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
${_ME} todos [<notebook>:][<folder-path>/] [open | closed]
${_ME} todos [<notebook>:][<folder-path>/] [open | closed] [--tags <tag1>,<tag2>...]
${_ME} todos tasks ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[open | closed]
${_ME} unarchive [<notebook>]
Expand Down Expand Up @@ -17521,6 +17521,9 @@ _search() {
elif {
[[ "${_type}" =~ (^bookmark$|^bookmarks$) ]] &&
_file_is_bookmark "${_path}"
} || {
[[ "${_type}" =~ (^todo$|^todos$|^task$|^tasks$) ]] &&
_file_is_todo "${_path}"
} || {
[[ "${_type}" =~ (^note$|^notes$|^text$) ]] &&
_file_is_text "${_path}"
Expand Down Expand Up @@ -17863,9 +17866,9 @@ _search() {
then
return 0
else
printf "Not found in %s: %s\\n" \
"$(_color_primary "${_search_selector:-}")" \
"$(_color_primary "${_query_message:-}")"
_warn printf "Not found in %s: %s\\n" \
"$(_color_primary "${_search_selector:-}")" \
"$(_color_primary "${_query_message:-}")"
fi
fi

Expand Down Expand Up @@ -25198,12 +25201,12 @@ $(_color_primary "Usage"):
${_ME} todo add [<notebook>:][<folder-path>/][<filename>] <title>
[--description <description>] [--due <date>]
[-r (<url> | <selector>) | --related (<url> | <selector>)]
[--tags <tag1>,<tag2>...] [--task <title>]...
[--tags <tag1>,<tag2>...] [--task <title>...]
${_ME} todo do ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
${_ME} todo undo ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[<task-number>]
${_ME} todos [<notebook>:][<folder-path>/] [open | closed]
${_ME} todos [<notebook>:][<folder-path>/] [open | closed] [--tags <tag1>,<tag2>...]
${_ME} todos tasks ([<notebook>:][<folder-path>/][<id> | <filename> | <description>])
[open | closed]

Expand Down Expand Up @@ -25342,7 +25345,9 @@ _todo() {
local _only_todos=0
local _option_title=
local _related_identifiers=()
local _tag_list=
local _tag_list_with_commas=
local _tag_list_with_spaces=
local _tags=()
local _task_list_options=()
local _tasks=()

Expand All @@ -25368,8 +25373,14 @@ _todo() {
shift
;;
--tag*)
[[ -n "${_tag_list:-}" ]] && _tag_list+=" "
_tag_list+="$(_option_get_tags "${1}" "${2:-}")"
[[ -n "${_tag_list_with_commas:-}" ]] && _tag_list_with_commas+=","
_tag_list_with_commas+="$(_option_get_tags "${1}" "${2:-}")"

[[ -n "${_tag_list_with_spaces:-}" ]] && _tag_list_with_spaces+=" "

_tag_list_with_spaces+="$(_option_get_tags "${1}" "${2:-}")"

_tags+=("${2:-}")

shift
;;
Expand Down Expand Up @@ -25538,15 +25549,15 @@ _todo() {
_todo_content+="${_NEWLINE}"
fi

if [[ -n "${_tag_list:-}" ]]
if [[ -n "${_tag_list_with_spaces:-}" ]]
then
_todo_content+="${_NEWLINE}## Tags${_NEWLINE}"
_todo_content+="${_NEWLINE}${_tag_list}${_NEWLINE}"
_todo_content+="${_NEWLINE}${_tag_list_with_spaces}${_NEWLINE}"
fi

_add \
--content "${_todo_content:-}" \
--type "todo.md" \
_add \
--content "${_todo_content:-}" \
--type "todo.md" \
"${_arguments[@]:-}"
;;
do|undo)
Expand Down Expand Up @@ -25660,7 +25671,20 @@ _todo() {
;;
list)
{
_list "${_arguments[@]:-}" --error-on-empty --type todo 2>/dev/null
if [[ -n "${_tag_list_with_commas:-}" ]]
then
_search \
"${_arguments[@]:-}" \
--tags "${_tag_list_with_commas:-}" \
--type todo \
--list 2>/dev/null
else
_list \
"${_arguments[@]:-}" \
--error-on-empty \
--type todo \
2>/dev/null
fi
} | {
case "${_filter:-}" in
closed)
Expand Down Expand Up @@ -25757,14 +25781,25 @@ _todo() {
)

{
_list "${_selector_path:-}" \
--error-on-empty \
--no-id \
--no-indicator \
--path \
--type md \
"${_task_list_options[@]:-}" \
2>/dev/null || printf ""
if [[ -n "${_tag_list_with_commas:-}" ]]
then
_search \
"${_selector_path:-}" \
"${_task_list_options[@]:-}" \
--tags "${_tag_list_with_commas:-}" \
--type todo \
--path
else
_list "${_selector_path:-}" \
--error-on-empty \
--no-id \
--no-indicator \
--path \
--type md \
"${_task_list_options[@]:-}" \
2>/dev/null || printf ""
fi

} | {
local _first=1
local _in_task_list_item=0
Expand Down
24 changes: 24 additions & 0 deletions test/search.bats
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ _search_all_setup() {
# <(printf "Content #example Seven. #tag3 #tag1")
# }

# --type <type> ###############################################################

@test "'search <query> --type <type>' only searches items of type <type>." {
{
_setup_search

"${_NB}" rename "File Two.md" --to-todo --force
}

run "${_NB}" search "example" --type todo

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
printf "\${lines[0]}: '%s'\\n" "${lines[0]}"

[[ "${status}" -eq 0 ]]

[[ "${#lines[@]}" -eq 3 ]]

[[ "${lines[0]}" =~ Title\ Two ]]
[[ "${lines[1]}" =~ -*- ]]
[[ "${lines[2]}" =~ Example.*\ Content\ Two\ .*Example.*\ Phrase ]]
}

# filename handling ###########################################################

@test "'search <query>' with uncommon filename exits with status 0 and prints output." {
Expand Down
42 changes: 42 additions & 0 deletions test/todo.bats
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,48 @@ load test_helper
.*\[.*Example\ Notebook:Example\ Folder/1.*\].*\ ✔️\ \ .*\[\ \].*\ Example\ todo\ description\ one\. ]]
}

@test "'todos --tags <tag>' exits with 0 and lists todos containing <tag>." {
{
"${_NB}" init

"${_NB}" add \
--content "# [ ] Example todo description one.${_NEWLINE}${_NEWLINE}#sample-tag" \
--filename "One.todo.md"

sleep 1

"${_NB}" add \
--content "# [ ] Example todo description two.${_NEWLINE}${_NEWLINE}#example-tag" \
--filename "Two.todo.md"

sleep 1

"${_NB}" add \
--content "# [x] Example todo description three.${_NEWLINE}${_NEWLINE}example-tag" \
--filename "Three.todo.md"

sleep 1

"${_NB}" add \
--content "# Example description four.${_NEWLINE}${_NEWLINE}#example-tag" \
--filename "Four.md"
}

run "${_NB}" todos --tags example-tag

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]

[[ ! "${output}" =~ one ]]
[[ ! "${output}" =~ three ]]
[[ ! "${output}" =~ four ]]

[[ "${lines[0]}" =~ \
.*\[.*2.*\].*\ ✔️\ \ .*\[\ \].*\ Example\ todo\ description\ two\. ]]
}

# todos closed / done #########################################################

@test "'todos closed <notebook>:<folder>/' exits with 0 and lists todos." {
Expand Down

0 comments on commit c65f2cd

Please sign in to comment.