From 265a92b15c851451a37cc3b8b912a970a7acff40 Mon Sep 17 00:00:00 2001 From: Sadra Yahyapour Date: Wed, 12 Jun 2024 02:39:07 +0330 Subject: [PATCH] fixes #17 --- hey/consts.py | 7 +++++++ hey/editor.py | 15 +++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/hey/consts.py b/hey/consts.py index 7ad0f1c..2dadf35 100644 --- a/hey/consts.py +++ b/hey/consts.py @@ -1,3 +1,6 @@ +import os +import platform + from platformdirs import user_config_path # App name @@ -6,6 +9,10 @@ # Service address SERVICE_URL = "https://llm.mdb.ai" +if platform.system().lower() == "windows": + DEFAULT_EDITOR = os.environ.get("EDITOR", "notepad") +else: + DEFAULT_EDITOR = os.environ.get("EDITOR", "vim") # basic configuration setup BASE_CONFIG = { diff --git a/hey/editor.py b/hey/editor.py index 2ce9ec8..c7ebfd0 100644 --- a/hey/editor.py +++ b/hey/editor.py @@ -2,9 +2,7 @@ import subprocess import tempfile -from rich.console import Console - -logger = Console() +from hey.consts import DEFAULT_EDITOR def open_tmp_editor() -> str: @@ -14,12 +12,13 @@ def open_tmp_editor() -> str: str: input string """ - with tempfile.NamedTemporaryFile(mode="w+") as temp_file: + with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp_file: temp_file_path = temp_file.name - subprocess.run([os.environ["EDITOR"], temp_file_path]) + subprocess.run([DEFAULT_EDITOR, temp_file_path]) - with open(temp_file_path, "r") as file: - data = file.read() + with open(temp_file_path, "r") as file: + data = file.read() - return data + os.unlink(temp_file_path) + return data