Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement --change-time flag #1452

Merged
merged 10 commits into from
May 21, 2022
Merged

Implement --change-time flag #1452

merged 10 commits into from
May 21, 2022

Conversation

richardjs
Copy link
Contributor

This implements #1429, for the reasons and in the manner described there.

It wasn't discussed in the issue, but I also handled the case where both --edit and --change-time are used. I thought it was a useful enough feature that it was worth implementing (it's equivalent to the originally-described --amend flag in the issue). Unfortunately, the resulting code feels a little ad-hoc; I'm happy to improve it if you have suggestions. I'm also happy to drop that sub-feature if you don't think it's worth the special case.

Checklist

  • I have read the contributing doc.
  • I have included a link to the relevant issue number.
  • I have checked to ensure there aren't other open pull requests
    for the same issue.
  • I have written new tests for these changes, as needed.

@wren
Copy link
Member

wren commented Apr 23, 2022

@richardjs Still reviewing this, but I wanted to say that I really appreciate how well you matched our style, both in the code and in the tests. This is a very high quality PR. Thank you!

jrnl/jrnl.py Outdated
@@ -169,6 +172,16 @@ def search_mode(args, journal, **kwargs):
# Where do the search results go?
if args.edit:
_edit_search_results(**kwargs)
# If we want to both edit and change time in one action
if args.change_time:
# Re-filter the journal enties (_edit_search_results puts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good observation. We should (in some other PR, not this one) separate the filtering and recombining of entries so that each of these commands doesn't have to worry about it. Thanks for pointing this out.

@@ -169,6 +172,16 @@ def search_mode(args, journal, **kwargs):
# Where do the search results go?
if args.edit:
_edit_search_results(**kwargs)
# If we want to both edit and change time in one action
if args.change_time:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good addition but I have one slight change to request. If a user runs --edit and --change-time together, then --change-time should happen first, and the updated timestamps should be loaded into the editor.

This avoids the awkward use case of jrnl overwriting a user's manual changes from the editor after they save the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wren I actually had it that way first, but changed it because after we change the entries' dates, they may not be selected again by _search_journal. For example, if the user ran jrnl -on today --change-time tomorrow --edit, today's entries would be changed to tomorrow, and then rerunning _search_journal before the call to _edit_search_results would select nothing.

However, while writing this I realize that we can do something to save which entries were selected and pass them on to edit, maybe apart from _search_journal. I'll see what I can do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wren , the change has been implemented!

@@ -320,6 +338,18 @@ def _delete_search_results(journal, old_entries, **kwargs):
journal.write()


def _change_time_search_results(args, journal, old_entries, **kwargs):
# separate entries we are not editing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add something similar here to the start of _delete_search_results? That way, if there are no search results, the user will get a message about how nothing is happening. Right now, this doesn't do anything, and is quiet about it, which can be confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that; I wasn't sure if --delete was only warned because it was destructive (for example, --edit doesn't warn if nothing is selected), and I reasoned it wasn't exactly an error situation (we're technically performing successfully what was requested).

But I agree it has the potential to be confusing. I'll add it in!

Copy link
Contributor Author

@richardjs richardjs Apr 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the message. I used the language "No entries to modify" because it sounded cleaner than "No entries to change time for" or similar, and it has the potential to be used in other situations. But I'm happy to use whatever you suggest. (Edit: Oh, and I also used MsgType.WARNING, but I'm happy to use ERROR like --delete.)

Unfortunately, I realized that if you run jrnl --change-time without anything else, it changes the time of every entry. So I'll fix that. In the meantime, I added a test for it (that currently fails).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After considering this bug, I posted on #1429 with some considerations about how --change-time might benefit from some safeguards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per @micahellison in #1429, I added a confirmation dialog to --change-time. There is one exception: if --edit and --change-time are used together with only one entry, no confirmation is requested. To my mind, this makes the amend-style action of --change-time smooth, while retaining the safety for larger operations.

But I can also see a clear case being made for confirmation even in that case (consistency, confirming what is still a destructive action, albeit on one entry, etc.), so I'm happy to remove that exception. I'm also wondering about adding some sort of output, e.g. "Changing time from X to Y", so the user can at least manually undo an error.

Copy link
Member

@wren wren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, again for such a high quality PR! There are just a few small changes needed, and this will be ready to go.

Copy link
Member

@wren wren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just one small bug with the prompt while using --edit to fix.

Also, if you can add a test or two for using --change-time and --edit it would be great (a test like this would have caught the bug mentioned above).

That should be it; everything else looks great! Thank you!

# modified by _change_time_search_results
selected_entries = [e for e in journal.entries]

no_change_time_prompt = len(journal.entries) == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work as intended right now. I think this line is supposed to reference selected_entries instead of journal.entries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wren, can you clarify the bug you're seeing? selected_entries here is built from journal.entries, so they have the same length. selected_entries only exists so we can reset journal.entries to the original set of entries on line 188.

(I agree that a test should cover the functionality--just added one.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @richardjs. Back from vacation now; thanks for your patience!

Nevermind about this bug! I misinterpreted the requirement about when to prompt the user. I thought we were always skipping when there was only one entry selected, but I see now that it's only when the edit flag is also used. So, this works as expected. Thank you!

Copy link
Member

@wren wren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, again!

@micahellison micahellison added the enhancement New feature or request label May 21, 2022
@micahellison micahellison linked an issue May 21, 2022 that may be closed by this pull request
@wren wren merged commit 33c9dce into jrnl-org:develop May 21, 2022
@richardjs richardjs mentioned this pull request Nov 13, 2022
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add --change-time command to change the timestamp of an entry
3 participants