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

Interactive delete #850

Merged
merged 10 commits into from
Mar 21, 2020
1 change: 0 additions & 1 deletion features/core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ Feature: Basic reading and writing to a journal
Given we use the config "basic.yaml"
When we run "jrnl -on 2013-06-10 -s"
Then the output should be "2013-06-10 15:40 Life is good."

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.
20 changes: 20 additions & 0 deletions features/delete.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Feature: Delete entries from journal

Scenario: --delete flag allows deletion of single entry
Given we use the config "deletion.yaml"
When we run "jrnl -n 1"
Then the output should contain
"""
2019-10-29 11:13 Third entry.
"""
When we run "jrnl --delete" and enter
"""
N
N
Y
"""
When we run "jrnl -n 1"
Then the output should contain
"""
2019-10-29 11:11 Second entry.
"""
31 changes: 25 additions & 6 deletions jrnl/Journal.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/usr/bin/env python

from . import Entry
from . import util
from . import time
import logging
import os
import sys
import re

from datetime import datetime
import logging
from itertools import filterfalse
from jrnl import Entry
from jrnl import util
from jrnl import time
dbxnr marked this conversation as resolved.
Show resolved Hide resolved

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -111,7 +113,8 @@ def _to_text(self):
def _load(self, filename):
raise NotImplementedError

def _store(self, filename, text):
@classmethod
def _store(filename, text):
raise NotImplementedError

def _parse(self, journal_txt):
Expand Down Expand Up @@ -247,12 +250,28 @@ def filter(

self.entries = result

def prompt_delete_entries(self):
"""Prompts for deletion of entries in a journal."""

to_delete = []

def ask_delete(entry):
return util.yesno(
f"Delete entry '{entry.pprint(short=True)}'?", default=False,
)

for entry in self.entries:
if ask_delete(entry):
to_delete.append(entry)

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

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."""

raw = raw.replace("\\n ", "\n").replace("\\n", "\n")
starred = False
# Split raw text into title and body
sep = re.search(r"\n|[?!.]+ +\n?", raw)
first_line = raw[: sep.end()].strip() if sep else raw
Expand Down
12 changes: 11 additions & 1 deletion jrnl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ 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.",
)
return parser.parse_args(args)


Expand All @@ -189,7 +195,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 @@ -459,3 +465,7 @@ def run(manual_args=None):
journal.entries += other_entries
journal.sort()
journal.write()

elif args.delete:
journal.prompt_delete_entries()
journal.write()
2 changes: 1 addition & 1 deletion jrnl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def set_keychain(journal_name, password):
def yesno(prompt, default=True):
prompt = f"{prompt.strip()} {'[Y/n]' if default else '[y/N]'} "
response = input(prompt)
return {"y": True, "n": False}.get(response.lower(), default)
return {"y": True, "n": False}.get(response.lower().strip(), default)


def load_config(config_path):
Expand Down