diff --git a/jrnl/editor.py b/jrnl/editor.py index 06f32b9f..3397cdac 100644 --- a/jrnl/editor.py +++ b/jrnl/editor.py @@ -1,3 +1,4 @@ +import logging import os import shlex import subprocess @@ -38,3 +39,19 @@ def get_text_from_editor(config, template=""): print("[Nothing saved to file]", file=sys.stderr) return raw + + +def get_text_from_stdin(): + _how_to_quit = "Ctrl+z and then Enter" if on_windows else "Ctrl+d" + print( + f"[Writing Entry; on a blank line, press {_how_to_quit} to finish writing]\n", + file=sys.stderr, + ) + try: + raw = sys.stdin.read() + except KeyboardInterrupt: + logging.error("Write mode: keyboard interrupt") + print("[Entry NOT saved to journal]", file=sys.stderr) + sys.exit(0) + + return raw diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index b691bb5a..33eabdd0 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -9,8 +9,8 @@ from .color import RESET_COLOR from .config import get_journal_name from .config import scope_config from .editor import get_text_from_editor +from .editor import get_text_from_stdin from .exception import UserAbort -from .os_compat import on_windows def run(args): @@ -164,17 +164,7 @@ def _write_in_editor(config): raw = get_text_from_editor(config, template) else: - _how_to_quit = "Ctrl+z and then Enter" if on_windows else "Ctrl+d" - print( - f"[Writing Entry; on a blank line, press {_how_to_quit} to finish writing]\n", - file=sys.stderr, - ) - try: - raw = sys.stdin.read() - except KeyboardInterrupt: - logging.error("Write mode: keyboard interrupt") - print("[Entry NOT saved to journal]", file=sys.stderr) - sys.exit(0) + raw = get_text_from_stdin() return raw