diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-11-25 18:52:56 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-25 18:52:56 -0500 |
commit | 03c65c60b39e369958b056c5c844d36d515c8a63 (patch) | |
tree | 02cf6743ce8b8d411fd4e2148b8e90d5806c483a /cmd2 | |
parent | 9f1c6b1f000593b777c3ee2c62b68edd053f2a3a (diff) | |
parent | 20951e94a213eaec3a2f46f8089099256329d0e7 (diff) | |
download | cmd2-git-03c65c60b39e369958b056c5c844d36d515c8a63.tar.gz |
Merge pull request #1021 from python-cmd2/editors
Updated utils.find_editor() to include more Windows editors
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/utils.py | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index ca07d23b..b58cdb96 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -357,32 +357,30 @@ def expand_user_in_tokens(tokens: List[str]) -> None: tokens[index] = expand_user(tokens[index]) -def is_executable(path) -> bool: - """Return True if specified path is executable file, otherwise False.""" - return os.path.isfile(path) and os.access(path, os.X_OK) - - -def probe_editors() -> str: - """Find a favor editor in system path.""" - editors = ['vim', 'vi', 'emacs', 'nano', 'pico', 'gedit', 'kate', 'subl', 'geany', 'atom'] - paths = [p for p in os.getenv('PATH').split(os.path.pathsep) if not os.path.islink(p)] - for editor, path in itertools.product(editors, paths): - editor_path = os.path.join(path, editor) - if is_executable(editor_path): - break - else: - editor_path = None - return editor_path - - -def find_editor() -> str: - """Find a reasonable editor to use by default for the system that the cmd2 application is running on.""" +def find_editor() -> Optional[str]: + """ + Used to set cmd2.Cmd.DEFAULT_EDITOR. If EDITOR env variable is set, that will be used. + Otherwise the function will look for a known editor in directories specified by PATH env variable. + :return: Default editor or None + """ editor = os.environ.get('EDITOR') if not editor: if sys.platform[:3] == 'win': - editor = 'notepad' + editors = ['code.cmd', 'notepad++.exe', 'notepad.exe'] else: - editor = probe_editors() + editors = ['vim', 'vi', 'emacs', 'nano', 'pico', 'joe', 'code', 'subl', 'atom', 'gedit', 'geany', 'kate'] + + paths = [p for p in os.getenv('PATH').split(os.path.pathsep) if not os.path.islink(p)] + for editor, path in itertools.product(editors, paths): + editor_path = os.path.join(path, editor) + if os.path.isfile(editor_path) and os.access(editor_path, os.X_OK): + if sys.platform[:3] == 'win': + # Remove extension from Windows file names + editor = os.path.splitext(editor)[0] + break + else: + editor = None + return editor |