Allow custom extensions when editing (for easier syntax highlighting) (#1139)

* Create tests and steps for temporary filename suffix
* Have temporary filename suffix be -{template_filename} or .jrnl
* Finalize extension_editor_file
* Make suffix local variable in get_text_from_editor
This commit is contained in:
Karim Rahal 2021-01-02 23:23:15 +02:00 committed by GitHub
parent c47c1e209e
commit e9fd5cdc0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 1 deletions

View file

@ -0,0 +1,18 @@
default_hour: 9
default_minute: 0
editor: ""
encrypt: false
highlight: true
editor: "vim"
journals:
default: features/journals/editor_markdown_extension.journal
linewrap: 80
tagsymbols: "@"
template: features/templates/extension.md
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none

View file

View file

@ -44,3 +44,13 @@ Feature: Journals iteracting with the file system in a way that users can see
And we change directory to "features" And we change directory to "features"
And we run "jrnl -n 1" And we run "jrnl -n 1"
Then the output should contain "hello world" Then the output should contain "hello world"
Scenario: the temporary filename suffix should default to ".jrnl"
Given we use the config "editor.yaml"
When we run "jrnl --edit"
Then the temporary filename suffix should be ".jrnl"
Scenario: the temporary filename suffix should be "-{template_filename}"
Given we use the config "editor_markdown_extension.yaml"
When we run "jrnl --edit"
Then the temporary filename suffix should be "-extension.md"

View file

@ -248,6 +248,14 @@ def contains_editor_file(context, method, text=""):
assert False, f"Method '{method}' not supported" assert False, f"Method '{method}' not supported"
@then('the temporary filename suffix should be "{suffix}"')
def extension_editor_file(context, suffix):
filename = Path(context.editor_file["name"]).name
delimiter = "-" if "-" in filename else "."
filename_suffix = delimiter + filename.split(delimiter)[-1]
assert filename_suffix == suffix
def _mock_getpass(inputs): def _mock_getpass(inputs):
def prompt_return(prompt=""): def prompt_return(prompt=""):
if type(inputs) == str: if type(inputs) == str:

View file

@ -5,6 +5,7 @@ import subprocess
import sys import sys
import tempfile import tempfile
import textwrap import textwrap
from pathlib import Path
from .color import ERROR_COLOR from .color import ERROR_COLOR
from .color import RESET_COLOR from .color import RESET_COLOR
@ -12,7 +13,11 @@ from .os_compat import on_windows
def get_text_from_editor(config, template=""): def get_text_from_editor(config, template=""):
filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") suffix = ".jrnl"
if config["template"]:
template_filename = Path(config["template"]).name
suffix = "-" + template_filename
filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=suffix)
os.close(filehandle) os.close(filehandle)
with open(tmpfile, "w", encoding="utf-8") as f: with open(tmpfile, "w", encoding="utf-8") as f: