From c65f2cd6983f444bb626d4e8625ca646d17fd0e8 Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 7 Feb 2024 10:40:38 -0800 Subject: [PATCH] Filter todos with `--tag ` option. refs gh-294 --- README.md | 8 ++--- docs/index.markdown | 8 ++--- nb | 83 ++++++++++++++++++++++++++++++++------------- test/search.bats | 24 +++++++++++++ test/todo.bats | 42 +++++++++++++++++++++++ 5 files changed, 133 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 3241b2ad1..299324fbe 100644 --- a/README.md +++ b/README.md @@ -6036,12 +6036,12 @@ Usage: nb todo add [:][/][] [--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>] @@ -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] diff --git a/docs/index.markdown b/docs/index.markdown index 6781d341b..eb185193f 100644 --- a/docs/index.markdown +++ b/docs/index.markdown @@ -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>] @@ -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] diff --git a/nb b/nb index b08d412dd..4aa3effa5 100755 --- a/nb +++ b/nb @@ -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>] @@ -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}" @@ -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 @@ -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] @@ -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=() @@ -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 ;; @@ -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) @@ -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) @@ -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 diff --git a/test/search.bats b/test/search.bats index cf2375dfb..0d923eac7 100644 --- a/test/search.bats +++ b/test/search.bats @@ -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." { diff --git a/test/todo.bats b/test/todo.bats index 69e0d3f80..2ae2445d1 100644 --- a/test/todo.bats +++ b/test/todo.bats @@ -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." {