Add message showing the number of search results (#1524)

* Added message showing the number of search results

Modified _search_journal() to return a bool based upon whether
any filter args were passed to the program.

Added _print_entries_found_count() which prints a message
based upon the number of entries found and prints warnings
if the user was trying to edit or delete.

* Add tests for search results msgs
This commit is contained in:
Kevin 2022-08-27 12:13:57 -07:00 committed by GitHub
parent 0fddb07c09
commit 0cc771f633
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 13 deletions

View file

@ -173,8 +173,9 @@ def search_mode(args, journal, **kwargs):
"old_entries": journal.entries,
}
# Filters the journal entries in place
_search_journal(**kwargs)
if _has_search_args(args):
_filter_journal_entries(**kwargs)
_print_entries_found_count(len(journal), args)
# Where do the search results go?
if args.edit:
@ -196,6 +197,10 @@ def search_mode(args, journal, **kwargs):
_edit_search_results(**kwargs)
elif not journal:
# Bail out if there are no entries and we're not editing
return
elif args.change_time:
_change_time_search_results(**kwargs)
@ -244,8 +249,28 @@ def _get_editor_template(config, **kwargs):
return template
def _search_journal(args, journal, **kwargs):
"""Search the journal with the given args"""
def _has_search_args(args):
return any(
(
args.on_date,
args.today_in_history,
args.text,
args.month,
args.day,
args.year,
args.start_date,
args.end_date,
args.strict,
args.starred,
args.excluded,
args.contains,
args.limit,
)
)
def _filter_journal_entries(args, journal, **kwargs):
"""Filter journal entries in-place based upon search args"""
if args.on_date:
args.start_date = args.end_date = args.on_date
@ -269,6 +294,27 @@ def _search_journal(args, journal, **kwargs):
journal.limit(args.limit)
def _print_entries_found_count(count, args):
if count == 0:
if args.edit or args.change_time:
print_msg(Message(MsgText.NothingToModify, MsgStyle.WARNING))
elif args.delete:
print_msg(Message(MsgText.NothingToDelete, MsgStyle.WARNING))
else:
print_msg(Message(MsgText.NoEntriesFound, MsgStyle.NORMAL))
elif args.limit:
# Don't show count if the user expects a limited number of results
return
elif args.edit or not (args.change_time or args.delete):
# Don't show count if we are ONLY changing the time or deleting entries
my_msg = (
MsgText.EntryFoundCountSingular
if count == 1
else MsgText.EntryFoundCountPlural
)
print_msg(Message(my_msg, MsgStyle.NORMAL, {"num": count}))
def _other_entries(journal, entries):
"""Find entries that are not in journal"""
return [e for e in entries if e not in journal.entries]
@ -352,9 +398,6 @@ def _get_predit_stats(journal):
def _delete_search_results(journal, old_entries, **kwargs):
if not journal.entries:
raise JrnlException(Message(MsgText.NothingToDelete, MsgStyle.ERROR))
entries_to_delete = journal.prompt_action_entries(MsgText.DeleteEntryQuestion)
if entries_to_delete:
@ -365,9 +408,6 @@ def _delete_search_results(journal, old_entries, **kwargs):
def _change_time_search_results(args, journal, old_entries, no_prompt=False, **kwargs):
if not journal.entries:
raise JrnlException(Message(MsgText.NothingToModify, MsgStyle.WARNING))
# separate entries we are not editing
other_entries = _other_entries(journal, old_entries)

View file

@ -224,6 +224,10 @@ class MsgText(Enum):
No entries to modify, because the search returned no results
"""
NoEntriesFound = "no entries found"
EntryFoundCountSingular = "{num} entry found"
EntryFoundCountPlural = "{num} entries found"
# --- Formats --- #
HeadingsPastH6 = """
Headings increased past H6 on export - {date} {title}

View file

@ -27,7 +27,8 @@ Feature: Searching in a journal
When we run "jrnl tomorrow: A future entry."
Then the output should contain "Entry added"
When we run "jrnl -from today"
Then the output should contain "Adding an entry right now."
Then the output should contain "2 entries found"
And the output should contain "Adding an entry right now."
And the output should contain "A future entry."
And the output should not contain "This thing happened yesterday"
@ -47,7 +48,8 @@ Feature: Searching in a journal
When we run "jrnl tomorrow: A future entry."
Then the output should contain "Entry added"
When we run "jrnl -from yesterday -to today"
Then the output should contain "This thing happened yesterday"
Then the output should contain "2 entries found"
And the output should contain "This thing happened yesterday"
And the output should contain "Adding an entry right now."
And the output should not contain "A future entry."
@ -61,6 +63,7 @@ Feature: Searching in a journal
Given we use the config "<config_file>"
When we run "jrnl -contains first --short"
Then we should get no error
And the output should contain "1 entry found"
And the output should be
2020-08-29 11:11 Entry the first.
@ -70,6 +73,19 @@ Feature: Searching in a journal
| basic_folder.yaml |
| basic_dayone.yaml |
Scenario Outline: Searching for an unknown string
Given we use the config "<config_file>"
When we run "jrnl -contains slkdfsdkfjsd"
Then we should get no error
And the output should contain "no entries found"
And the output should not contain "slkdfsdkfjsd"
Examples: configs
| config_file |
| basic_onefile.yaml |
| basic_folder.yaml |
| basic_dayone.yaml |
Scenario Outline: Searching for a string within tag results
Given we use the config "<config_file>"
When we run "jrnl @tagone -contains maybe"
@ -86,6 +102,7 @@ Feature: Searching in a journal
Given we use the config "<config_file>"
When we run "jrnl -and @tagone @tagtwo -contains maybe"
Then we should get no error
And the output should contain "1 entry found"
And the output should contain "maybe"
Examples: configs
@ -98,6 +115,7 @@ Feature: Searching in a journal
Given we use the config "<config_file>"
When we run "jrnl -not @tagone -contains lonesome"
Then we should get no error
And the output should contain "1 entry found"
And the output should contain "lonesome"
Examples: configs
@ -287,7 +305,8 @@ Feature: Searching in a journal
And now is "2020-08-31 02:32:00 PM"
When we run "jrnl 2019-08-31 01:01: Hi, from last year."
And we run "jrnl -today-in-history --short"
Then the output should be
Then the output should contain "2 entries found"
And the output should be
2019-08-31 01:01 Hi, from last year.
2020-08-31 14:32 A second entry in what I hope to be a long series.
@ -302,6 +321,7 @@ Feature: Searching in a journal
Given we use the config "dayone.yaml"
When we run "jrnl -from 'feb 2013'"
Then we should get no error
And the output should contain "3 entries found"
And the output should be
2013-05-17 11:39 This entry has tags!