From 0f791cf69aea1ec742ac12e8614da27bacc1f1a7 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 30 Apr 2022 21:10:55 -0500 Subject: [PATCH] Add prompt for --change-time --- jrnl/Journal.py | 36 ++++++++-------- jrnl/jrnl.py | 23 +++++++---- tests/bdd/features/change_time.feature | 57 +++++++++++++++++++++----- 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 5da79c4b..13c5363a 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -261,32 +261,30 @@ class Journal: for entry in entries_to_delete: self.entries.remove(entry) - def prompt_delete_entries(self): - """Prompts for deletion of each of the entries in a journal. - Returns the entries the user wishes to delete.""" - - to_delete = [] - - def ask_delete(entry): - return yesno( - f"Delete entry '{entry.pprint(short=True)}'?", - default=False, - ) - - for entry in self.entries: - if ask_delete(entry): - to_delete.append(entry) - - return to_delete - def change_date_entries(self, date): """Changes entry dates to given date.""" - date = time.parse(date) for entry in self.entries: entry.date = date + def prompt_action_entries(self, message): + """Prompts for action for each entry in a journal, using given message. + Returns the entries the user wishes to apply the action on.""" + to_act = [] + + def ask_action(entry): + return yesno( + f"{message} '{entry.pprint(short=True)}'?", + default=False, + ) + + for entry in self.entries: + if ask_action(entry): + to_act.append(entry) + + return to_act + def new_entry(self, raw, date=None, sort=True): """Constructs a new entry from some raw text input. If a date is given, it will parse and use this, otherwise scan for a date in the input first.""" diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 43800ddf..9176cd10 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -329,7 +329,7 @@ def _delete_search_results(journal, old_entries, **kwargs): if not journal.entries: raise JrnlException(Message(MsgText.NothingToDelete, MsgType.ERROR)) - entries_to_delete = journal.prompt_delete_entries() + entries_to_delete = journal.prompt_action_entries("Delete entry") if entries_to_delete: journal.entries = old_entries @@ -338,19 +338,28 @@ def _delete_search_results(journal, old_entries, **kwargs): journal.write() -def _change_time_search_results(args, 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, MsgType.WARNING)) # separate entries we are not editing other_entries = _other_entries(journal, old_entries) - date = time.parse(args.change_time) - journal.change_date_entries(date) + if no_prompt: + entries_to_change = journal.entries + else: + entries_to_change = journal.prompt_action_entries("Change time") - journal.entries += other_entries - journal.sort() - journal.write() + if entries_to_change: + other_entries += [e for e in journal.entries if e not in entries_to_change] + journal.entries = entries_to_change + + date = time.parse(args.change_time) + journal.change_date_entries(date) + + journal.entries += other_entries + journal.sort() + journal.write() def _display_search_results(args, journal, **kwargs): diff --git a/tests/bdd/features/change_time.feature b/tests/bdd/features/change_time.feature index a4a3a7eb..f2b6a70a 100644 --- a/tests/bdd/features/change_time.feature +++ b/tests/bdd/features/change_time.feature @@ -4,7 +4,8 @@ Feature: Change entry times in journal And we use the password "test" if prompted When we run "jrnl -1" Then the output should contain "2020-09-24 09:14 The third entry finally" - When we run "jrnl -1 --change-time '2022-04-23 10:30'" + When we run "jrnl -1 --change-time '2022-04-23 10:30'" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-29 11:11 Entry the first. @@ -18,6 +19,28 @@ Feature: Change entry times in journal | basic_folder.yaml | # | basic_dayone.yaml | @todo + Scenario Outline: Change flag changes prompted entries + Given we use the config "" + And we use the password "test" if prompted + When we run "jrnl -1" + Then the output should contain "2020-09-24 09:14 The third entry finally" + When we run "jrnl --change-time '2022-04-23 10:30'" and enter + Y + N + Y + When we run "jrnl -99 --short" + Then the output should be + 2020-08-31 14:32 A second entry in what I hope to be a long series. + 2022-04-23 10:30 Entry the first. + 2022-04-23 10:30 The third entry finally after weeks without writing. + + Examples: Configs + | config_file | + | basic_onefile.yaml | + | basic_encrypted.yaml | + | basic_folder.yaml | + # | basic_dayone.yaml | @todo + Scenario Outline: Change time flag with nonsense input changes nothing Given we use the config "" @@ -38,7 +61,8 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with tag only changes tagged entries Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' @ipsum" + When we run "jrnl --change-time '2022-04-23 10:30' @ipsum" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-31 14:32 A second entry in what I hope to be a long series. @@ -54,7 +78,9 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with multiple tags changes all entries matching any of the tags Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' @ipsum @tagthree" + When we run "jrnl --change-time '2022-04-23 10:30' @ipsum @tagthree" and enter + Y + Y When we run "jrnl -99 --short" Then the output should be 2020-08-31 14:32 A second entry in what I hope to be a long series. @@ -70,7 +96,8 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with -and changes boolean AND of tagged entries Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' -and @tagone @tagtwo" + When we run "jrnl --change-time '2022-04-23 10:30' -and @tagone @tagtwo" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-31 14:32 A second entry in what I hope to be a long series. @@ -86,7 +113,8 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with -not does not change entries from given tag Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' @tagone -not @ipsum" + When we run "jrnl --change-time '2022-04-23 10:30' @tagone -not @ipsum" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-29 11:11 Entry the first. @@ -102,7 +130,8 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with -from search operator only changes entries since that date Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' -from 2020-09-01" + When we run "jrnl --change-time '2022-04-23 10:30' -from 2020-09-01" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-29 11:11 Entry the first. @@ -118,7 +147,9 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with -to only changes entries up to specified date Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' -to 2020-08-31" + When we run "jrnl --change-time '2022-04-23 10:30' -to 2020-08-31" and enter + Y + Y When we run "jrnl -99 --short" Then the output should be 2020-09-24 09:14 The third entry finally after weeks without writing. @@ -134,7 +165,8 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with -starred only changes starred entries Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' -starred" + When we run "jrnl --change-time '2022-04-23 10:30' -starred" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-29 11:11 Entry the first. @@ -150,7 +182,8 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with -contains only changes entries containing expression Given we use the config "" - When we run "jrnl --change-time '2022-04-23 10:30' -contains dignissim" + When we run "jrnl --change-time '2022-04-23 10:30' -contains dignissim" and enter + Y When we run "jrnl -99 --short" Then the output should be 2020-08-31 14:32 A second entry in what I hope to be a long series. @@ -167,8 +200,10 @@ Feature: Change entry times in journal Scenario Outline: Change time flag with no enties specified changes nothing Given we use the config "" And we use the password "test" if prompted - When we run "jrnl --change-time" - Then the output should contain "No entries to modify" + When we run "jrnl --change-time" and enter + N + N + N When we run "jrnl -99 --short" Then the output should be 2020-08-29 11:11 Entry the first.