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

Add --delete for interactive removal of entries #698

Merged
merged 7 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions features/core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ Feature: Basic reading and writing to a journal
2013-06-10 15:40 Life is good.
"""
And we should get no error

# The input for this test is <SPACE><ENTER>y
Scenario: --delete flag allows deletion of single entry
Given we use the config "deletion.yaml"
When we run "jrnl --delete"
And we type " "
And we type
"""

y
"""
When we run "jrnl -on 2019-10-29 -s"
Then the output should not contain "2019-10-29 11:11 First entry."

12 changes: 12 additions & 0 deletions features/data/configs/deletion.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
default_hour: 9
default_minute: 0
editor: ""
encrypt: false
highlight: true
journals:
default: features/journals/deletion.journal
linewrap: 80
tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
5 changes: 5 additions & 0 deletions features/data/journals/deletion.journal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[2019-10-29 11:11] First entry.

[2019-10-29 11:11] Second entry.

[2019-10-29 11:13] Third entry.
11 changes: 11 additions & 0 deletions jrnl/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ def filter(

self.entries = result

def prompt_delete_entries(self):
"""Prompts for deletion of entries in a journal."""
print("Confirm each entry you want to delete [N/y]:")
to_delete: List[Entry] = []
for entry in self.entries:
response = input("jrnl: Delete entry '{}'? ".format(entry.pprint(short=True)))
if response == "y":
to_delete.append(entry)

self.entries = [entry for entry in self.entries if entry not in to_delete]

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."""
Expand Down
13 changes: 12 additions & 1 deletion jrnl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ def parse_args(args=None):
action="store_true",
)

exporting.add_argument(
"--delete",
dest="delete",
action="store_true",
help="Opens an interactive interface for deleting entries.",
)

# Handle '-123' as a shortcut for '-n 123'
num = re.compile(r"^-(\d+)$")
if args is None:
Expand All @@ -194,7 +201,7 @@ def guess_mode(args, config):
args.decrypt is not False
or args.encrypt is not False
or args.export is not False
or any((args.short, args.tags, args.edit))
or any((args.short, args.tags, args.edit, args.delete))
):
compose = False
export = True
Expand Down Expand Up @@ -456,3 +463,7 @@ def run(manual_args=None):
journal.entries += other_entries
journal.sort()
journal.write()

elif args.delete:
journal.prompt_delete_entries()
journal.write()