|
3 | 3 | # Import required modules |
4 | 4 | import os |
5 | 5 | import subprocess |
| 6 | +import shutil |
6 | 7 | import paths_factory |
7 | 8 |
|
8 | 9 | from i18n import _ |
9 | 10 |
|
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") |
15 | 16 |
|
16 | 17 | # 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)) |
21 | 33 |
|
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