Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions features/data/configs/editor_markdown_extension.yaml
Original file line number Diff line number Diff line change
@@ -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
Empty file.
10 changes: 10 additions & 0 deletions features/file_storage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 run "jrnl -n 1"
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"
8 changes: 8 additions & 0 deletions features/steps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ def contains_editor_file(context, method, text=""):
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 prompt_return(prompt=""):
if type(inputs) == str:
Expand Down
7 changes: 6 additions & 1 deletion jrnl/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import sys
import tempfile
import textwrap
from pathlib import Path

from .color import ERROR_COLOR
from .color import RESET_COLOR
from .os_compat import on_windows


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)

with open(tmpfile, "w", encoding="utf-8") as f:
Expand Down