mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-15 02:38:30 +02:00
WIP
This commit is contained in:
parent
17c987c605
commit
6eb7d857f0
2 changed files with 117 additions and 132 deletions
|
@ -39,7 +39,8 @@ def run(args: "Namespace"):
|
||||||
3. Run standalone command if it does require config (encrypt, decrypt, etc), then exit
|
3. Run standalone command if it does require config (encrypt, decrypt, etc), then exit
|
||||||
4. Load specified journal
|
4. Load specified journal
|
||||||
5. Start write mode, or search mode
|
5. Start write mode, or search mode
|
||||||
6. Profit
|
6. Perform actions with results from search mode (if needed)
|
||||||
|
7. Profit
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Run command if possible before config is available
|
# Run command if possible before config is available
|
||||||
|
@ -71,56 +72,56 @@ def run(args: "Namespace"):
|
||||||
"args": args,
|
"args": args,
|
||||||
"config": config,
|
"config": config,
|
||||||
"journal": journal,
|
"journal": journal,
|
||||||
|
"old_entries": journal.entries,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _is_write_mode(**kwargs):
|
if _is_append_mode(**kwargs):
|
||||||
write_mode(**kwargs)
|
append_mode(**kwargs)
|
||||||
else:
|
return
|
||||||
search_mode(**kwargs)
|
|
||||||
|
# If not append mode, then we're in search mode (only 2 modes exist)
|
||||||
|
search_mode(**kwargs)
|
||||||
|
|
||||||
|
# perform actions (if needed)
|
||||||
|
if args.change_time:
|
||||||
|
_change_time_search_results(**kwargs)
|
||||||
|
|
||||||
|
if args.delete:
|
||||||
|
_delete_search_results(**kwargs)
|
||||||
|
|
||||||
|
# open results in editor (if `--edit` was used)
|
||||||
|
if args.edit:
|
||||||
|
_edit_search_results(**kwargs)
|
||||||
|
|
||||||
|
_print_entries_found_count(len(journal), args)
|
||||||
|
|
||||||
|
if not args.edit and not _has_action_args(args):
|
||||||
|
# display only occurs if no other action occurs
|
||||||
|
_display_search_results(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
def _is_write_mode(args: "Namespace", config: dict, **kwargs) -> bool:
|
def _is_append_mode(args: "Namespace", config: dict, **kwargs) -> bool:
|
||||||
"""Determines if we are in write mode (as opposed to search mode)"""
|
"""Determines if we are in write mode (as opposed to search mode)"""
|
||||||
# Are any search filters present? If so, then search mode.
|
# Are any search filters present? If so, then search mode.
|
||||||
write_mode = not any(
|
append_mode = (
|
||||||
(
|
not _has_search_args(args)
|
||||||
args.contains,
|
and not _has_action_args(args)
|
||||||
args.delete,
|
and not _has_display_args(args)
|
||||||
args.edit,
|
and not args.edit
|
||||||
args.change_time,
|
|
||||||
args.excluded,
|
|
||||||
args.export,
|
|
||||||
args.end_date,
|
|
||||||
args.today_in_history,
|
|
||||||
args.month,
|
|
||||||
args.day,
|
|
||||||
args.year,
|
|
||||||
args.limit,
|
|
||||||
args.on_date,
|
|
||||||
args.short,
|
|
||||||
args.starred,
|
|
||||||
args.start_date,
|
|
||||||
args.strict,
|
|
||||||
args.tags,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Might be writing and want to move to editor part of the way through
|
# Might be writing and want to move to editor part of the way through
|
||||||
if args.edit and args.text:
|
if args.edit and args.text:
|
||||||
write_mode = True
|
append_mode = True
|
||||||
|
|
||||||
# If the text is entirely tags, then we are also searching (not writing)
|
# If the text is entirely tags, then we are also searching (not writing)
|
||||||
if (
|
if append_mode and args.text and _has_only_tags(config["tagsymbols"], args.text):
|
||||||
write_mode
|
append_mode = False
|
||||||
and args.text
|
|
||||||
and all(word[0] in config["tagsymbols"] for word in " ".join(args.text).split())
|
|
||||||
):
|
|
||||||
write_mode = False
|
|
||||||
|
|
||||||
return write_mode
|
return append_mode
|
||||||
|
|
||||||
|
|
||||||
def write_mode(args: "Namespace", config: dict, journal: Journal, **kwargs) -> None:
|
def append_mode(args: "Namespace", config: dict, journal: Journal, **kwargs) -> None:
|
||||||
"""
|
"""
|
||||||
Gets input from the user to write to the journal
|
Gets input from the user to write to the journal
|
||||||
1. Check for input from cli
|
1. Check for input from cli
|
||||||
|
@ -129,27 +130,27 @@ def write_mode(args: "Namespace", config: dict, journal: Journal, **kwargs) -> N
|
||||||
4. Use stdin.read as last resort
|
4. Use stdin.read as last resort
|
||||||
6. Write any found text to journal, or exit
|
6. Write any found text to journal, or exit
|
||||||
"""
|
"""
|
||||||
logging.debug("Write mode: starting")
|
logging.debug("Append mode: starting")
|
||||||
|
|
||||||
if args.text:
|
if args.text:
|
||||||
logging.debug("Write mode: cli text detected: %s", args.text)
|
logging.debug("Append mode: cli text detected: %s", args.text)
|
||||||
raw = " ".join(args.text).strip()
|
raw = " ".join(args.text).strip()
|
||||||
if args.edit:
|
if args.edit:
|
||||||
raw = _write_in_editor(config, raw)
|
raw = _write_in_editor(config, raw)
|
||||||
|
|
||||||
elif not sys.stdin.isatty():
|
elif not sys.stdin.isatty():
|
||||||
logging.debug("Write mode: receiving piped text")
|
logging.debug("Append mode: receiving piped text")
|
||||||
raw = sys.stdin.read()
|
raw = sys.stdin.read()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raw = _write_in_editor(config)
|
raw = _write_in_editor(config)
|
||||||
|
|
||||||
if not raw or raw.isspace():
|
if not raw or raw.isspace():
|
||||||
logging.error("Write mode: couldn't get raw text or entry was empty")
|
logging.error("Append mode: couldn't get raw text or entry was empty")
|
||||||
raise JrnlException(Message(MsgText.NoTextReceived, MsgStyle.NORMAL))
|
raise JrnlException(Message(MsgText.NoTextReceived, MsgStyle.NORMAL))
|
||||||
|
|
||||||
logging.debug(
|
logging.debug(
|
||||||
'Write mode: appending raw text to journal "%s": %s', args.journal_name, raw
|
f"Append mode: appending raw text to journal '{args.journal_name}': {raw}"
|
||||||
)
|
)
|
||||||
journal.new_entry(raw)
|
journal.new_entry(raw)
|
||||||
if args.journal_name != DEFAULT_JOURNAL_KEY:
|
if args.journal_name != DEFAULT_JOURNAL_KEY:
|
||||||
|
@ -161,66 +162,32 @@ def write_mode(args: "Namespace", config: dict, journal: Journal, **kwargs) -> N
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
journal.write()
|
journal.write()
|
||||||
logging.debug("Write mode: completed journal.write()")
|
logging.debug("Append mode: completed journal.write()")
|
||||||
|
|
||||||
|
|
||||||
def search_mode(args: "Namespace", journal: Journal, **kwargs) -> None:
|
def search_mode(args: "Namespace", journal: Journal, **kwargs) -> None:
|
||||||
"""
|
"""
|
||||||
Search for entries in a journal, then either:
|
Search for entries in a journal, and return the
|
||||||
1. Send them to configured editor for user manipulation (and also
|
results. If no search args, then return all results
|
||||||
change their timestamps if requested)
|
|
||||||
2. Change their timestamps
|
|
||||||
2. Delete them (with confirmation for each entry)
|
|
||||||
3. Display them (with formatting options)
|
|
||||||
"""
|
"""
|
||||||
kwargs = {
|
logging.debug("Search mode: starting")
|
||||||
**kwargs,
|
|
||||||
"args": args,
|
|
||||||
"journal": journal,
|
|
||||||
"old_entries": journal.entries,
|
|
||||||
}
|
|
||||||
|
|
||||||
if _has_search_args(args):
|
# If no search args, then return all results (don't filter anything)
|
||||||
_filter_journal_entries(**kwargs)
|
if (
|
||||||
_print_entries_found_count(len(journal), args)
|
not _has_search_args(args)
|
||||||
|
and not _has_display_args(args)
|
||||||
# Where do the search results go?
|
and not _has_only_tags(kwargs["config"]["tagsymbols"], args.text)
|
||||||
if args.edit:
|
):
|
||||||
# If we want to both edit and change time in one action
|
logging.debug("Search mode: has not search args")
|
||||||
if args.change_time:
|
|
||||||
# Generate a new list instead of assigning so it won't be
|
|
||||||
# modified by _change_time_search_results
|
|
||||||
selected_entries = [e for e in journal.entries]
|
|
||||||
|
|
||||||
no_change_time_prompt = len(journal.entries) == 1
|
|
||||||
_change_time_search_results(no_prompt=no_change_time_prompt, **kwargs)
|
|
||||||
|
|
||||||
# Re-filter the journal enties (_change_time_search_results
|
|
||||||
# puts the filtered entries back); use selected_entries
|
|
||||||
# instead of running _search_journal again, because times
|
|
||||||
# have changed since the original search
|
|
||||||
kwargs["old_entries"] = journal.entries
|
|
||||||
journal.entries = selected_entries
|
|
||||||
|
|
||||||
_edit_search_results(**kwargs)
|
|
||||||
|
|
||||||
elif not journal:
|
|
||||||
# Bail out if there are no entries and we're not editing
|
|
||||||
return
|
return
|
||||||
|
|
||||||
elif args.change_time:
|
logging.debug("Search mode: has search args")
|
||||||
_change_time_search_results(**kwargs)
|
_filter_journal_entries(args, journal)
|
||||||
|
|
||||||
elif args.delete:
|
|
||||||
_delete_search_results(**kwargs)
|
|
||||||
|
|
||||||
else:
|
|
||||||
_display_search_results(**kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def _write_in_editor(config: dict, template: str | None = None) -> str:
|
def _write_in_editor(config: dict, template: str | None = None) -> str:
|
||||||
if config["editor"]:
|
if config["editor"]:
|
||||||
logging.debug("Write mode: opening editor")
|
logging.debug("Append mode: opening editor")
|
||||||
if not template:
|
if not template:
|
||||||
template = _get_editor_template(config)
|
template = _get_editor_template(config)
|
||||||
raw = get_text_from_editor(config, template)
|
raw = get_text_from_editor(config, template)
|
||||||
|
@ -232,10 +199,10 @@ def _write_in_editor(config: dict, template: str | None = None) -> str:
|
||||||
|
|
||||||
|
|
||||||
def _get_editor_template(config: dict, **kwargs) -> str:
|
def _get_editor_template(config: dict, **kwargs) -> str:
|
||||||
logging.debug("Write mode: loading template for entry")
|
logging.debug("Append mode: loading template for entry")
|
||||||
|
|
||||||
if not config["template"]:
|
if not config["template"]:
|
||||||
logging.debug("Write mode: no template configured")
|
logging.debug("Append mode: no template configured")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
template_path = expand_path(config["template"])
|
template_path = expand_path(config["template"])
|
||||||
|
@ -243,9 +210,9 @@ def _get_editor_template(config: dict, **kwargs) -> str:
|
||||||
try:
|
try:
|
||||||
with open(template_path) as f:
|
with open(template_path) as f:
|
||||||
template = f.read()
|
template = f.read()
|
||||||
logging.debug("Write mode: template loaded: %s", template)
|
logging.debug("Append mode: template loaded: %s", template)
|
||||||
except OSError:
|
except OSError:
|
||||||
logging.error("Write mode: template not loaded")
|
logging.error("Append mode: template not loaded")
|
||||||
raise JrnlException(
|
raise JrnlException(
|
||||||
Message(
|
Message(
|
||||||
MsgText.CantReadTemplate,
|
MsgText.CantReadTemplate,
|
||||||
|
@ -257,26 +224,6 @@ def _get_editor_template(config: dict, **kwargs) -> str:
|
||||||
return template
|
return template
|
||||||
|
|
||||||
|
|
||||||
def _has_search_args(args: "Namespace") -> bool:
|
|
||||||
return any(
|
|
||||||
(
|
|
||||||
args.on_date,
|
|
||||||
args.today_in_history,
|
|
||||||
args.text,
|
|
||||||
args.month,
|
|
||||||
args.day,
|
|
||||||
args.year,
|
|
||||||
args.start_date,
|
|
||||||
args.end_date,
|
|
||||||
args.strict,
|
|
||||||
args.starred,
|
|
||||||
args.excluded,
|
|
||||||
args.contains,
|
|
||||||
args.limit,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _filter_journal_entries(args: "Namespace", journal: Journal, **kwargs) -> None:
|
def _filter_journal_entries(args: "Namespace", journal: Journal, **kwargs) -> None:
|
||||||
"""Filter journal entries in-place based upon search args"""
|
"""Filter journal entries in-place based upon search args"""
|
||||||
if args.on_date:
|
if args.on_date:
|
||||||
|
@ -425,28 +372,21 @@ def _change_time_search_results(
|
||||||
args: "Namespace",
|
args: "Namespace",
|
||||||
journal: Journal,
|
journal: Journal,
|
||||||
old_entries: list["Entry"],
|
old_entries: list["Entry"],
|
||||||
no_prompt: bool = False,
|
**kwargs,
|
||||||
**kwargs
|
|
||||||
) -> None:
|
) -> None:
|
||||||
# separate entries we are not editing
|
|
||||||
other_entries = _other_entries(journal, old_entries)
|
|
||||||
|
|
||||||
if no_prompt:
|
import ipdb
|
||||||
entries_to_change = journal.entries
|
|
||||||
else:
|
ipdb.sset_trace()
|
||||||
entries_to_change = journal.prompt_action_entries(
|
# separate entries we are not editing
|
||||||
MsgText.ChangeTimeEntryQuestion
|
# @todo if there's only 1, don't prompt
|
||||||
)
|
entries_to_change = journal.prompt_action_entries(MsgText.ChangeTimeEntryQuestion)
|
||||||
|
|
||||||
if entries_to_change:
|
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)
|
date = time.parse(args.change_time)
|
||||||
journal.change_date_entries(date)
|
journal.entries = old_entries
|
||||||
|
journal.change_date_entries(date, entries_to_change)
|
||||||
|
|
||||||
journal.entries += other_entries
|
|
||||||
journal.sort()
|
|
||||||
journal.write()
|
journal.write()
|
||||||
|
|
||||||
|
|
||||||
|
@ -468,3 +408,46 @@ def _display_search_results(args: "Namespace", journal: Journal, **kwargs) -> No
|
||||||
print(exporter.export(journal, args.filename))
|
print(exporter.export(journal, args.filename))
|
||||||
else:
|
else:
|
||||||
print(journal.pprint())
|
print(journal.pprint())
|
||||||
|
|
||||||
|
|
||||||
|
def _has_search_args(args: "Namespace") -> bool:
|
||||||
|
"""Looking for arguments that filter a journal"""
|
||||||
|
return any(
|
||||||
|
(
|
||||||
|
args.contains,
|
||||||
|
args.excluded, # -not
|
||||||
|
args.end_date,
|
||||||
|
args.today_in_history,
|
||||||
|
args.month,
|
||||||
|
args.day,
|
||||||
|
args.year,
|
||||||
|
args.limit,
|
||||||
|
args.on_date,
|
||||||
|
args.starred,
|
||||||
|
args.start_date,
|
||||||
|
args.strict, # -and
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _has_action_args(args: "Namespace") -> bool:
|
||||||
|
return any(
|
||||||
|
(
|
||||||
|
args.change_time,
|
||||||
|
args.delete,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _has_display_args(args: "Namespace") -> bool:
|
||||||
|
return any(
|
||||||
|
(
|
||||||
|
args.tags,
|
||||||
|
args.short,
|
||||||
|
args.export, # --format
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _has_only_tags(tag_symbols: str, args_text: str) -> bool:
|
||||||
|
return all(word[0] in tag_symbols for word in " ".join(args_text).split())
|
||||||
|
|
|
@ -299,11 +299,13 @@ class Journal:
|
||||||
for entry in entries_to_delete:
|
for entry in entries_to_delete:
|
||||||
self.entries.remove(entry)
|
self.entries.remove(entry)
|
||||||
|
|
||||||
def change_date_entries(self, date: datetime.datetime | None) -> None:
|
def change_date_entries(
|
||||||
|
self, date: datetime.datetime, entries_to_change: list[Entry]
|
||||||
|
) -> None:
|
||||||
"""Changes entry dates to given date."""
|
"""Changes entry dates to given date."""
|
||||||
date = time.parse(date)
|
date = time.parse(date)
|
||||||
|
|
||||||
for entry in self.entries:
|
for entry in entries_to_change:
|
||||||
entry.date = date
|
entry.date = date
|
||||||
|
|
||||||
def prompt_action_entries(self, msg: MsgText) -> list[Entry]:
|
def prompt_action_entries(self, msg: MsgText) -> list[Entry]:
|
||||||
|
@ -436,7 +438,7 @@ def open_journal(journal_name: str, config: dict, legacy: bool = False) -> Journ
|
||||||
If legacy is True, it will open Journals with legacy classes build for
|
If legacy is True, it will open Journals with legacy classes build for
|
||||||
backwards compatibility with jrnl 1.x
|
backwards compatibility with jrnl 1.x
|
||||||
"""
|
"""
|
||||||
logging.debug("open_journal start")
|
logging.debug(f"open_journal '{journal_name}'")
|
||||||
validate_journal_name(journal_name, config)
|
validate_journal_name(journal_name, config)
|
||||||
config = config.copy()
|
config = config.copy()
|
||||||
config["journal"] = expand_path(config["journal"])
|
config["journal"] = expand_path(config["journal"])
|
||||||
|
|
Loading…
Add table
Reference in a new issue