From 0c78f33b432e1a5fbc34d1948b75d3a6cd229799 Mon Sep 17 00:00:00 2001 From: Sam Colwell Date: Sat, 9 Feb 2019 13:04:30 -0500 Subject: [PATCH] Trying new method to get unbuffered stdin --- py65/monitor.py | 27 ++++++++++++++++++++++++++- py65/utils/console.py | 4 ++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/py65/monitor.py b/py65/monitor.py index 57319b7..b9ea6d2 100644 --- a/py65/monitor.py +++ b/py65/monitor.py @@ -53,7 +53,32 @@ class Monitor(cmd.Cmd): self._width = 78 self.prompt = "." self._add_shortcuts() - cmd.Cmd.__init__(self, stdin=stdin, stdout=stdout) + + # Attempt to get a copy of stdin that is unbuffered. + # This allows for immediate response to typed input as well as + # pasted input. If unable to get an unbuffered version of + # stdin, use the original version. + if stdin != None: + try: + # Reopen stdin with no buffer. + self.unbuffered_stdin = os.fdopen(stdin.fileno(), 'rb', 0) + except Exception as e: + print(e) + # Unable to reopen this file handle with no buffer. + # Just use the original file handle. + print("Error opening unbuffered stdin - using buffered version") + self.unbuffered_stdin = stdin + else: + # If stdin is None, try using sys.stdin for input. + try: + # Reopen the system's stdin with no buffer. + self.unbuffered_stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) + except Exception as e: + print(e) + print("Error opening default unbuffered stdin - using buffered version") + self.unbuffered_stdin = None + + cmd.Cmd.__init__(self, stdin=self.unbuffered_stdin, stdout=stdout) # Save the current input mode so it can be restored after # after processing commands and before exiting. diff --git a/py65/utils/console.py b/py65/utils/console.py index c44e0a8..de39da2 100644 --- a/py65/utils/console.py +++ b/py65/utils/console.py @@ -80,9 +80,9 @@ else: newattr = currentattr[:] newattr[3] &= ~termios.ICANON & ~termios.ECHO - # Switch to non-blocking reads with 0.1 second timeout. + # Switch to non-blocking reads with no timeout. newattr[6][termios.VMIN] = 0 - newattr[6][termios.VTIME] = 1 + newattr[6][termios.VTIME] = 0 termios.tcsetattr(fd, termios.TCSANOW, newattr) except: # Quietly ignore termios errors, such as stdin not being