Skip to content

Commit 806313d

Browse files
authored
Merge pull request #1009 from aleeraser/feat/better-EDITOR-handling
feat(config): better EDITOR handling
2 parents c4521c1 + af56ba5 commit 806313d

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

howdy/src/cli/config.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,40 @@
33
# Import required modules
44
import os
55
import subprocess
6+
import shutil
67
import paths_factory
78

89
from i18n import _
910

10-
# Let the user know what we're doing
11-
print(_("Opening config.ini in the default editor"))
12-
13-
# Default to the nano editor
14-
editor = "/bin/nano"
11+
# Determine the editor to use
12+
editor = None
13+
preferred_editor = os.environ.get("EDITOR")
14+
nano_path = shutil.which("nano")
15+
vi_path = shutil.which("vi")
1516

1617
# Use the user preferred editor if available
17-
if "EDITOR" in os.environ:
18-
editor = os.environ["EDITOR"]
19-
elif os.path.isfile("/etc/alternatives/editor"):
20-
editor = "/etc/alternatives/editor"
18+
if preferred_editor:
19+
if shutil.which(preferred_editor):
20+
editor = preferred_editor
21+
22+
if not editor:
23+
if nano_path:
24+
editor = nano_path
25+
elif vi_path:
26+
editor = vi_path
27+
28+
if editor:
29+
editor_name = os.path.basename(editor)
30+
31+
# Let the user know what we're doing
32+
print(_("Opening config.ini in {editor}").format(editor=editor_name))
2133

22-
# Open the editor as a subprocess and fork it
23-
subprocess.call([editor, paths_factory.config_file_path()])
34+
# Open the editor as a subprocess and fork it
35+
try:
36+
subprocess.call([editor, paths_factory.config_file_path()])
37+
except Exception as e:
38+
print(_("Failed to open editor: {error}").format(error=e))
39+
else:
40+
print(_("Error: Could not find a suitable text editor."))
41+
print(_("Please install 'nano' or 'vi', or set the EDITOR environment variable."))
42+
print(_("If you are running this command with sudo, try 'sudo -E howdy config' to preserve your EDITOR variable."))

0 commit comments

Comments
 (0)