Skip to content

Commit

Permalink
using default editor for config managing
Browse files Browse the repository at this point in the history
fixes #17
  • Loading branch information
lnxpy committed Jun 11, 2024
1 parent cccf54c commit a4de337
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions hey/configs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from hey import console
from hey.configs.utils import app_dir_exists, config_exists, init_config
from hey.consts import APP_CONFIG_DIR, CONFIG_FILE_PATH
from hey.consts import APP_CONFIG_DIR, CONFIG_FILE_PATH, DEFAULT_EDITOR

app = typer.Typer(help="Configuration management.")

Expand All @@ -30,4 +30,4 @@ def edit():
if not app_dir_exists() or not config_exists():
console.print("You don't have any config file. Try `hey config create` first.")
else:
subprocess.run([os.environ["EDITOR"], CONFIG_FILE_PATH])
subprocess.run([DEFAULT_EDITOR, CONFIG_FILE_PATH])
7 changes: 7 additions & 0 deletions hey/consts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import platform

from platformdirs import user_config_path

# App name
Expand All @@ -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 = {
Expand Down
15 changes: 7 additions & 8 deletions hey/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit a4de337

Please sign in to comment.