Allow for -not -starred to search for unstarred entries

This commit is contained in:
Ciaran Concannon 2023-01-12 12:47:57 +00:00 committed by Ciaran Concannon
parent 17c987c605
commit c573ca9418
5 changed files with 50 additions and 16 deletions

View file

@ -27,6 +27,10 @@ class WrappingFormatter(argparse.RawTextHelpFormatter):
text = [item for sublist in text for item in sublist] text = [item for sublist in text for item in sublist]
return text return text
class IgnoreNoneAppendAction(argparse._AppendAction):
def __call__(self, parser, namespace, values, option_string=None):
if values is not None:
super().__call__(parser, namespace, values, option_string)
def parse_args(args: list[str] = []) -> argparse.Namespace: def parse_args(args: list[str] = []) -> argparse.Namespace:
""" """
@ -249,11 +253,13 @@ def parse_args(args: list[str] = []) -> argparse.Namespace:
reading.add_argument( reading.add_argument(
"-not", "-not",
dest="excluded", dest="excluded",
nargs=1, nargs="?",
default=[], default=[],
metavar="TAG", metavar="TAG/FLAG",
action="extend", action=IgnoreNoneAppendAction,
help="Exclude entries with this tag", help=("If passed a string, will exclude entries with that tag. "
"If it preceeds -starred flag, will exclude starred entries"
),
) )
search_options_msg = """ These help you do various tasks with the selected entries from your search. search_options_msg = """ These help you do various tasks with the selected entries from your search.
@ -388,5 +394,14 @@ def parse_args(args: list[str] = []) -> argparse.Namespace:
# Handle '-123' as a shortcut for '-n 123' # Handle '-123' as a shortcut for '-n 123'
num = re.compile(r"^-(\d+)$") num = re.compile(r"^-(\d+)$")
args = [num.sub(r"-n \1", arg) for arg in args] args = [num.sub(r"-n \1", arg) for arg in args]
parsed_args = parser.parse_intermixed_args(args)
parsed_args.exclude_starred = False
return parser.parse_intermixed_args(args) # Handle -not where it is reversing the behaviour of a flag
if "-not-starred" in "".join(args):
parsed_args.starred = False
parsed_args.exclude_starred = True
if "-not" in args and not any([parsed_args.exclude_starred, parsed_args.excluded]):
return parser.error("argument -not: expected 1 argument")
return parsed_args

View file

@ -89,6 +89,7 @@ def _is_write_mode(args: "Namespace", config: dict, **kwargs) -> bool:
args.edit, args.edit,
args.change_time, args.change_time,
args.excluded, args.excluded,
args.exclude_starred,
args.export, args.export,
args.end_date, args.end_date,
args.today_in_history, args.today_in_history,
@ -271,6 +272,7 @@ def _has_search_args(args: "Namespace") -> bool:
args.strict, args.strict,
args.starred, args.starred,
args.excluded, args.excluded,
args.exclude_starred,
args.contains, args.contains,
args.limit, args.limit,
) )
@ -297,6 +299,7 @@ def _filter_journal_entries(args: "Namespace", journal: Journal, **kwargs) -> No
strict=args.strict, strict=args.strict,
starred=args.starred, starred=args.starred,
exclude=args.excluded, exclude=args.excluded,
exclude_starred=args.exclude_starred,
contains=args.contains, contains=args.contains,
) )
journal.limit(args.limit) journal.limit(args.limit)

View file

@ -229,16 +229,17 @@ class Journal:
def filter( def filter(
self, self,
tags: list = [], tags=[],
month: str | int | None = None, month=None,
day: str | int | None = None, day=None,
year: str | None = None, year=None,
start_date: str | None = None, start_date=None,
end_date: str | None = None, end_date=None,
starred: bool = False, starred=False,
strict: bool = False, exclude_starred=False,
contains: bool = None, strict=False,
exclude: list = [], contains=None,
exclude=[],
): ):
"""Removes all entries from the journal that don't match the filter. """Removes all entries from the journal that don't match the filter.
@ -276,7 +277,7 @@ class Journal:
entry entry
for entry in self.entries for entry in self.entries
if (not tags or tagged(entry.tags)) if (not tags or tagged(entry.tags))
and (not starred or entry.starred) and (not (starred or exclude_starred) or entry.starred == starred)
and (not month or entry.date.month == compare_d.month) and (not month or entry.date.month == compare_d.month)
and (not day or entry.date.day == compare_d.day) and (not day or entry.date.day == compare_d.day)
and (not year or entry.date.year == compare_d.year) and (not year or entry.date.year == compare_d.year)

View file

@ -124,6 +124,20 @@ Feature: Searching in a journal
| basic_folder.yaml | | basic_folder.yaml |
| basic_dayone.yaml | | basic_dayone.yaml |
Scenario: Searching for unstarred entries
Given we use the config "<config_file>"
And we use the password "test" if prompted
When we run "jrnl -not -starred"
Then we should get no error
And the output should contain "2 entries found"
Examples: configs
| config_file |
| basic_onefile.yaml |
| basic_folder.yaml |
| basic_dayone.yaml |
Scenario Outline: Searching for dates Scenario Outline: Searching for dates
Given we use the config "<config_file>" Given we use the config "<config_file>"
When we run "jrnl -on 2020-08-31 --short" When we run "jrnl -on 2020-08-31 --short"

View file

@ -23,6 +23,7 @@ def expected_args(**kwargs):
"change_time": None, "change_time": None,
"edit": False, "edit": False,
"end_date": None, "end_date": None,
"exclude_starred": False,
"today_in_history": False, "today_in_history": False,
"month": None, "month": None,
"day": None, "day": None,