summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2021-01-22 14:53:33 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2021-01-22 14:53:33 -0500
commit45580056485328338899dd65607c6c57d3797f35 (patch)
tree4cb6a10485b440e0b23eb638764fc2ff14faa974 /cmd2/utils.py
parent49fac04513727a67b0360a9e7560087c64dd557d (diff)
parenta3b1b6ddf81cdc0b253f15feeb167ff348afd14f (diff)
downloadcmd2-git-45580056485328338899dd65607c6c57d3797f35.tar.gz
Merge branch 'master' into 2.0
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py70
1 files changed, 41 insertions, 29 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index ea1a08c5..1f5de366 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -4,7 +4,6 @@
import argparse
import collections
import collections.abc as collections_abc
-import enum
import functools
import glob
import inspect
@@ -14,11 +13,26 @@ import re
import subprocess
import sys
import threading
-
import unicodedata
-from typing import Any, Callable, Dict, IO, Iterable, List, Optional, TextIO, Type, Union
-
-from . import constants
+from enum import (
+ Enum,
+)
+from typing import (
+ IO,
+ Any,
+ Callable,
+ Dict,
+ Iterable,
+ List,
+ Optional,
+ TextIO,
+ Type,
+ Union,
+)
+
+from . import (
+ constants,
+)
def is_quoted(arg: str) -> bool:
@@ -320,32 +334,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:
+ 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 = probe_editors()
+ editor = None
+
return editor
@@ -669,7 +681,7 @@ class RedirectionSavedState:
self.saved_redirecting = saved_redirecting
-class TextAlignment(enum.Enum):
+class TextAlignment(Enum):
"""Horizontal text alignment"""
LEFT = 1
CENTER = 2
@@ -1041,7 +1053,7 @@ def get_defining_class(meth) -> Type:
return getattr(meth, '__objclass__', None) # handle special descriptor objects
-class CompletionMode(enum.Enum):
+class CompletionMode(Enum):
"""Enum for what type of tab completion to perform in cmd2.Cmd.read_input()"""
# Tab completion will be disabled during read_input() call
# Use of custom up-arrow history supported