| Conditions | 6 |
| Total Lines | 28 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import logging |
||
| 14 | def get_text_from_editor(config, template=""): |
||
| 15 | filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") |
||
| 16 | os.close(filehandle) |
||
| 17 | |||
| 18 | with open(tmpfile, "w", encoding="utf-8") as f: |
||
| 19 | if template: |
||
| 20 | f.write(template) |
||
| 21 | |||
| 22 | try: |
||
| 23 | subprocess.call(shlex.split(config["editor"], posix=on_windows) + [tmpfile]) |
||
| 24 | except Exception as e: |
||
| 25 | error_msg = f""" |
||
| 26 | {ERROR_COLOR}{str(e)}{RESET_COLOR} |
||
| 27 | |||
| 28 | Please check the 'editor' key in your config file for errors: |
||
| 29 | {repr(config['editor'])} |
||
| 30 | """ |
||
| 31 | print(textwrap.dedent(error_msg).strip(), file=sys.stderr) |
||
| 32 | exit(1) |
||
| 33 | |||
| 34 | with open(tmpfile, "r", encoding="utf-8") as f: |
||
| 35 | raw = f.read() |
||
| 36 | os.remove(tmpfile) |
||
| 37 | |||
| 38 | if not raw: |
||
| 39 | print("[Nothing saved to file]", file=sys.stderr) |
||
| 40 | |||
| 41 | return raw |
||
| 42 | |||
| 58 |