summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-04-02 19:00:01 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-04-02 19:00:01 -0400
commit7a24546f234855368e7ec11005a38e4deb1f77eb (patch)
tree41a4d60a4591aa3332c77ede42bd8358a5436c70 /cmd2/utils.py
parentc17858d3ad13b830d8e579f13b80d736877c0ba8 (diff)
downloadcmd2-git-7a24546f234855368e7ec11005a38e4deb1f77eb.tar.gz
Started addressing my PR comments
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py43
1 files changed, 32 insertions, 11 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py
index 4172e362..74f83c64 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -342,12 +342,14 @@ class StdSim(object):
def clear(self) -> None:
"""Clear the internal contents"""
- self.buffer.byte_buf = b''
+ self.buffer.byte_buf = bytearray()
- @staticmethod
- def isatty() -> bool:
- """StdSim will never be considered an interactive stream"""
- return False
+ def isatty(self) -> bool:
+ """StdSim only be considered an interactive stream if `echo` is True and `inner_stream` is a tty."""
+ if self.echo:
+ return self.inner_stream.isatty()
+ else:
+ return False
def __getattr__(self, item: str):
if item in self.__dict__:
@@ -469,24 +471,43 @@ class ProcReader(object):
class ContextFlag(object):
- """
- A flag value that is used in a with statement.
+ """A context manager which is also used as a boolean flag value within the default sigint handler.
+
Its main use is as a flag to prevent the SIGINT handler in cmd2 from raising a KeyboardInterrupt
while a critical code section has set the flag to True. Because signal handling is always done on the
main thread, this class is not thread-safe since there is no need.
"""
- def __init__(self):
+ def __init__(self) -> None:
# When this flag has a positive value, it is considered set.
# When it is 0, it is not set. It should never go below 0.
self.__count = 0
- def __bool__(self):
+ def __bool__(self) -> bool:
return self.__count > 0
- def __enter__(self):
+ def __enter__(self) -> None:
self.__count += 1
- def __exit__(self, *args):
+ def __exit__(self, *args) -> None:
self.__count -= 1
if self.__count < 0:
raise ValueError("count has gone below 0")
+
+
+class RedirectionSavedState(object):
+ """Created by each command to store information about their redirection."""
+
+ def __init__(self, self_stdout: Union[StdSim, BinaryIO, TextIO], sys_stdout: Union[StdSim, BinaryIO, TextIO],
+ pipe_proc_reader: Optional[ProcReader]) -> None:
+ # Used to restore values after the command ends
+ self.saved_self_stdout = self_stdout
+ self.saved_sys_stdout = sys_stdout
+ self.saved_pipe_proc_reader = pipe_proc_reader
+
+ # Tells if the command is redirecting
+ self.redirecting = False
+
+ # If the command created a process to pipe to, then then is its reader
+ self.pipe_proc_reader = None
+
+