diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-23 21:11:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-23 21:11:01 -0400 |
commit | bef07746e33da9def33d814913891384a545a95c (patch) | |
tree | 86b162f79663f70cbe88e64deb4cecb93106ba68 /cmd2/utils.py | |
parent | c12ba0ff11b3a8fd083c641cb9149aff6494bbf9 (diff) | |
parent | eb1936e568a2ca4817ab0cd640220a5bc355e226 (diff) | |
download | cmd2-git-bef07746e33da9def33d814913891384a545a95c.tar.gz |
Merge pull request #703 from python-cmd2/public_api
Minimize public API of cmd2.Cmd class
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 3500ba7a..3e28641d 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -5,6 +5,7 @@ import collections import glob import os import re +import shutil import subprocess import sys import threading @@ -348,6 +349,50 @@ def files_from_glob_patterns(patterns: List[str], access=os.F_OK) -> List[str]: return files +def get_exes_in_path(starts_with: str) -> List[str]: + """Returns names of executables in a user's path + + :param starts_with: what the exes should start with. leave blank for all exes in path. + :return: a list of matching exe names + """ + # Purposely don't match any executable containing wildcards + wildcards = ['*', '?'] + for wildcard in wildcards: + if wildcard in starts_with: + return [] + + # Get a list of every directory in the PATH environment variable and ignore symbolic links + paths = [p for p in os.getenv('PATH').split(os.path.pathsep) if not os.path.islink(p)] + + # Use a set to store exe names since there can be duplicates + exes_set = set() + + # Find every executable file in the user's path that matches the pattern + for path in paths: + full_path = os.path.join(path, starts_with) + matches = files_from_glob_pattern(full_path + '*', access=os.X_OK) + + for match in matches: + exes_set.add(os.path.basename(match)) + + return list(exes_set) + + +def center_text(msg: str, *, pad: str = ' ') -> str: + """Centers text horizontally for display within the current terminal, optionally padding both sides. + + :param msg: message to display in the center + :param pad: (optional) if provided, the first character will be used to pad both sides of the message + :return: centered message, optionally padded on both sides with pad_char + """ + term_width = shutil.get_terminal_size().columns + surrounded_msg = ' {} '.format(msg) + if not pad: + pad = ' ' + fill_char = pad[:1] + return surrounded_msg.center(term_width, fill_char) + + class StdSim(object): """ Class to simulate behavior of sys.stdout or sys.stderr. |