Skip to content

Commit

Permalink
feat: support VISUAL env var
Browse files Browse the repository at this point in the history
refer: #86
  • Loading branch information
lervag committed May 28, 2024
1 parent 70a10c0 commit 9f8040c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/apyanki/anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from apyanki.config import cfg
from apyanki.console import console
from apyanki.note import Note, NoteData, markdown_file_to_notes
from apyanki.utilities import cd, choose, editor, suppress_stdout
from apyanki.utilities import cd, choose, edit_file, suppress_stdout

if TYPE_CHECKING:
from anki.notes import NoteId
Expand Down Expand Up @@ -368,7 +368,7 @@ def edit_model_css(self, model_name: str) -> None:
tf.write(model["css"])
tf.flush()

retcode = editor(tf.name)
retcode = edit_file(tf.name)
if retcode != 0:
console.print(f"Editor return with exit code {retcode}!")
return
Expand Down Expand Up @@ -441,7 +441,7 @@ def add_notes_with_editor(
) as tf:
tf.write(input_string)
tf.flush()
retcode = editor(tf.name)
retcode = edit_file(tf.name)

if retcode != 0:
console.print(f"Editor return with exit code {retcode}!")
Expand Down
4 changes: 2 additions & 2 deletions src/apyanki/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
prepare_field_for_cli_raw,
toggle_field_to_markdown,
)
from apyanki.utilities import cd, choose, editor
from apyanki.utilities import cd, choose, edit_file

if TYPE_CHECKING:
from apyanki.anki import Anki
Expand Down Expand Up @@ -192,7 +192,7 @@ def edit(self) -> None:
tf.write(str(self))
tf.flush()

retcode = editor(tf.name)
retcode = edit_file(tf.name)
if retcode != 0:
console.print(f"[red]Editor return with exit code {retcode}![/red]")
return
Expand Down
9 changes: 5 additions & 4 deletions src/apyanki/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def __exit__(
os.chdir(self.savedPath)


def editor(filepath: str) -> int:
"""Use EDITOR to edit file at given path"""
return call([os.environ.get("EDITOR", "vim"), filepath])
def edit_file(filepath: str) -> int:
"""Use $VISUAL or $EDITOR to edit file at given path"""
editor = os.environ.get("VISUAL", os.environ.get("EDITOR", "vim"))
return call([editor, filepath])


def edit_text(input_text: str, prefix: str = "") -> str:
Expand All @@ -46,7 +47,7 @@ def edit_text(input_text: str, prefix: str = "") -> str:
with NamedTemporaryFile(mode="w+", prefix=prefix, suffix=".md") as tf:
tf.write(input_text)
tf.flush()
editor(tf.name)
edit_file(tf.name)
tf.seek(0)
edited_message = tf.read().strip()

Expand Down

0 comments on commit 9f8040c

Please sign in to comment.