From 70bf9e1a12b89bb913c11fb07893ab4b9cab2576 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sat, 15 Jun 2019 15:00:59 -0400 Subject: Began work to minimize public API --- cmd2/utils.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index 3500ba7a..43eb3d9d 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -348,6 +348,35 @@ 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) + + class StdSim(object): """ Class to simulate behavior of sys.stdout or sys.stderr. @@ -586,7 +615,7 @@ class RedirectionSavedState(object): self.saved_sys_stdout = sys_stdout self.saved_pipe_proc_reader = pipe_proc_reader - # Tells if the command is redirecting + # Tells if the command is _redirecting self.redirecting = False # If the command created a process to pipe to, then then is its reader -- cgit v1.2.1 From f77c44dd79b484f96a3077fd7ca64fd5b3c35fa1 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sat, 15 Jun 2019 16:27:29 -0400 Subject: Fixed a few comments where "redirecting" accidentally got changed to "_redirecting" --- cmd2/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index 43eb3d9d..c8fb4aff 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -615,7 +615,7 @@ class RedirectionSavedState(object): self.saved_sys_stdout = sys_stdout self.saved_pipe_proc_reader = pipe_proc_reader - # Tells if the command is _redirecting + # Tells if the command is redirecting self.redirecting = False # If the command created a process to pipe to, then then is its reader -- cgit v1.2.1 From a9ce49bfc46610e03d62a1e4afbfdde9f5b0f7ee Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sun, 16 Jun 2019 14:41:49 -0400 Subject: Added center_text() utility function for centering text in a terminal along with unit tests for it This function isn't used yet, but I have plans to use it when improving the output of transcript testing --- cmd2/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index c8fb4aff..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 @@ -377,6 +378,21 @@ def get_exes_in_path(starts_with: str) -> List[str]: 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. -- cgit v1.2.1