Skip to content

Commit

Permalink
List matching items when only --not query is present in search.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwmx committed Apr 28, 2023
1 parent eccb10b commit dab8013
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -16784,7 +16784,7 @@ _search() {
return 0
fi

if [[ -z "${_and_queries[*]:-}" ]]
if [[ -z "${_and_queries[*]:-}" ]] && [[ -z "${_not_queries[*]:-}" ]]
then
_exit_1 _help "search"
fi
Expand Down Expand Up @@ -16817,6 +16817,12 @@ _search() {
local _query=
_query="$(_join "|" "${_queries[@]:-}")"

if [[ -z "${_query:-}" ]] &&
[[ -n "${_not_queries[*]:-}" ]]
then
_only_list=1
fi

{
local __target_path=
for __target_path in "${_target_paths[@]:-}"
Expand Down Expand Up @@ -17043,7 +17049,7 @@ _search() {
_search_paths+=("${__target_path%\/*}/.index")
fi

if [[ "${#_queries[@]}" -le 1 ]]
if [[ "${#_queries[@]}" == 1 ]]
then
GIT_PAGER='' git -C "${_notebook_path}" \
grep \
Expand Down
35 changes: 35 additions & 0 deletions test/search-logic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ _setup_search() {

# --not option ################################################################

@test "'search --not <query>' lists items NOT matchin query, omitting content." {
{
"${_NB}" init

"${_NB}" add "File One.md" \
--title "Title One" \
--content "Sample Content One Sample Phrase Alpha."

"${_NB}" add "File Two.md" \
--title "Title Two" \
--content "Example Content Two Example Phrase Beta."

"${_NB}" add "File Three.md" \
--title "Title Three" \
--content "Example Content Three Example Phrase Alpha."

"${_NB}" add "File Four.md" \
--title "Title Four" \
--content "Example Content Four Example Phrase Beta."
}

run "${_NB}" search --not "example"

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

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

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

[[ "${lines[0]}" =~ [.*1.*].*\ Title\ One ]]
[[ -z "${lines[1]}" ]]
}

@test "'search --and <query1> --and <query2> --not <query3>' lists items matching query1 AND query2 and NOT query3." {
{
"${_NB}" init
Expand Down

0 comments on commit dab8013

Please sign in to comment.