mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Add ability to put --edit partly through a cli entry to move it to the editor (#1091)
also add some functionality to test suite to see what the editor file contents were and make assertions about it
This commit is contained in:
parent
336936651f
commit
cfdd05358a
3 changed files with 56 additions and 5 deletions
|
@ -230,6 +230,21 @@ def matches_editor_arg(context, regex):
|
|||
), f"\nRegex didn't match exactly 1 time:\n{regex}\n{str(args)}"
|
||||
|
||||
|
||||
@then("the editor file content should {method}")
|
||||
@then("the editor file content should {method} empty")
|
||||
@then('the editor file content should {method} "{text}"')
|
||||
def contains_editor_file(context, method, text=""):
|
||||
text = text or context.text or ""
|
||||
content = context.editor_file.get("content")
|
||||
format = f'\n"""\n{content}\n"""\n'
|
||||
if method == "be":
|
||||
assert content == text, format
|
||||
elif method == "contain":
|
||||
assert text in content, format
|
||||
else:
|
||||
assert False, f"Method '{method}' not supported"
|
||||
|
||||
|
||||
def _mock_getpass(inputs):
|
||||
def prompt_return(prompt=""):
|
||||
if type(inputs) == str:
|
||||
|
@ -270,7 +285,9 @@ def run_with_input(context, command, inputs=""):
|
|||
def _mock_editor(command):
|
||||
context.editor_command = command
|
||||
tmpfile = command[-1]
|
||||
context.editor_file = tmpfile
|
||||
with open(tmpfile, "r") as editor_file:
|
||||
file_content = editor_file.read()
|
||||
context.editor_file = {"name": tmpfile, "content": file_content}
|
||||
Path(tmpfile).touch()
|
||||
|
||||
if "password" in context:
|
||||
|
@ -348,6 +365,11 @@ def run(context, command, text=""):
|
|||
|
||||
def _mock_editor(command):
|
||||
context.editor_command = command
|
||||
tmpfile = command[-1]
|
||||
with open(tmpfile, "r") as editor_file:
|
||||
file_content = editor_file.read()
|
||||
context.editor_file = {"name": tmpfile, "content": file_content}
|
||||
Path(tmpfile).touch()
|
||||
|
||||
if "password" in context:
|
||||
password = context.password
|
||||
|
@ -359,10 +381,12 @@ def run(context, command, text=""):
|
|||
# see: https://github.com/psf/black/issues/664
|
||||
with \
|
||||
patch("sys.argv", args), \
|
||||
patch("getpass.getpass", side_effect=_mock_getpass(password)), \
|
||||
patch("subprocess.call", side_effect=_mock_editor), \
|
||||
patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \
|
||||
patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \
|
||||
patch("sys.stdin.read", side_effect=lambda: text) \
|
||||
:
|
||||
context.editor = mock_editor
|
||||
context.getpass = mock_getpass
|
||||
cli(args[1:])
|
||||
context.exit_status = 0
|
||||
# fmt: on
|
||||
|
|
|
@ -43,6 +43,26 @@ Feature: Writing new entries.
|
|||
| dayone |
|
||||
| encrypted |
|
||||
|
||||
Scenario Outline: Writing a partial entry from command line with edit flag should go to the editor
|
||||
Given we use the config "<config_file>.yaml"
|
||||
And we use the password "test" if prompted
|
||||
When we run "jrnl this is a partial --edit"
|
||||
Then we should see the message "Entry added"
|
||||
Then the editor should have been called
|
||||
And the editor file content should be
|
||||
"""
|
||||
this is a partial
|
||||
"""
|
||||
When we run "jrnl -n 1"
|
||||
Then the output should contain "this is a partial"
|
||||
|
||||
Examples: configs
|
||||
| config_file |
|
||||
| basic_onefile |
|
||||
| basic_encrypted |
|
||||
| basic_dayone |
|
||||
| basic_folder |
|
||||
|
||||
Scenario Outline: Writing an empty entry from the editor should yield "Nothing saved to file" message
|
||||
Given we use the config "<config_file>.yaml"
|
||||
And we use the password "test" if prompted
|
||||
|
|
11
jrnl/jrnl.py
11
jrnl/jrnl.py
|
@ -83,6 +83,10 @@ def _is_write_mode(args, config, **kwargs):
|
|||
)
|
||||
)
|
||||
|
||||
# Might be writing and want to move to editor part of the way through
|
||||
if args.edit and args.text:
|
||||
write_mode = True
|
||||
|
||||
# If the text is entirely tags, then we are also searching (not writing)
|
||||
if (
|
||||
write_mode
|
||||
|
@ -108,6 +112,8 @@ def write_mode(args, config, journal, **kwargs):
|
|||
if args.text:
|
||||
logging.debug("Write mode: cli text detected: %s", args.text)
|
||||
raw = " ".join(args.text).strip()
|
||||
if args.edit:
|
||||
raw = _write_in_editor(config, raw)
|
||||
|
||||
elif not sys.stdin.isatty():
|
||||
logging.debug("Write mode: receiving piped text")
|
||||
|
@ -157,10 +163,11 @@ def search_mode(args, journal, **kwargs):
|
|||
_display_search_results(**kwargs)
|
||||
|
||||
|
||||
def _write_in_editor(config):
|
||||
def _write_in_editor(config, template=None):
|
||||
if config["editor"]:
|
||||
logging.debug("Write mode: opening editor")
|
||||
template = _get_editor_template(config)
|
||||
if not template:
|
||||
template = _get_editor_template(config)
|
||||
raw = get_text_from_editor(config, template)
|
||||
|
||||
else:
|
||||
|
|
Loading…
Add table
Reference in a new issue