From 2c2651efd51b6722c12281752b196ce873897a17 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 | 6 ++++++ hey/editor.py | 15 +++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/hey/consts.py b/hey/consts.py index 7ad0f1c..87046e6 100644 --- a/hey/consts.py +++ b/hey/consts.py @@ -1,4 +1,6 @@ from platformdirs import user_config_path +from platform import platform +import os # App name APP_NAME = "Hey" @@ -6,6 +8,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