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.
()
| 301 | |
| 302 | |
| 303 | def find_editor() -> str | None: |
| 304 | """Set cmd2.Cmd.DEFAULT_EDITOR. If EDITOR env variable is set, that will be used. |
| 305 | |
| 306 | Otherwise the function will look for a known editor in directories specified by PATH env variable. |
| 307 | :return: Default editor or None. |
| 308 | """ |
| 309 | editor = os.environ.get("EDITOR") |
| 310 | if not editor: |
| 311 | if sys.platform[:3] == "win": |
| 312 | editors = ["edit", "code.cmd", "notepad++.exe", "notepad.exe"] |
| 313 | else: |
| 314 | editors = ["vim", "vi", "emacs", "nano", "pico", "joe", "code", "subl", "gedit", "kate"] |
| 315 | |
| 316 | # Get a list of every directory in the PATH environment variable and ignore symbolic links |
| 317 | env_path = os.getenv("PATH") |
| 318 | paths = [] if env_path is None else [p for p in env_path.split(os.path.pathsep) if not os.path.islink(p)] |
| 319 | |
| 320 | for possible_editor, path in itertools.product(editors, paths): |
| 321 | editor_path = os.path.join(path, possible_editor) |
| 322 | if os.path.isfile(editor_path) and os.access(editor_path, os.X_OK): |
| 323 | if sys.platform[:3] == "win": |
| 324 | # Remove extension from Windows file names |
| 325 | editor = os.path.splitext(possible_editor)[0] |
| 326 | else: |
| 327 | editor = possible_editor |
| 328 | break |
| 329 | else: |
| 330 | editor = None |
| 331 | |
| 332 | return editor |
| 333 | |
| 334 | |
| 335 | def files_from_glob_pattern(pattern: str, access: int = os.F_OK) -> list[str]: |
nothing calls this directly
no test coverage detected
searching dependent graphs…