miniterm: make getkey cancelable on posix too, see #107
diff --git a/serial/tools/miniterm.py b/serial/tools/miniterm.py
index c0e4dc0..2e6865f 100644
--- a/serial/tools/miniterm.py
+++ b/serial/tools/miniterm.py
@@ -134,11 +134,15 @@
elif os.name == 'posix':
import atexit
import termios
+ import select
class Console(ConsoleBase):
def __init__(self):
super(Console, self).__init__()
self.fd = sys.stdin.fileno()
+ # an additional pipe is used in getkey, so that the cancel method
+ # can abort the waiting getkey method
+ self.pipe_r, self.pipe_w = os.pipe()
self.old = termios.tcgetattr(self.fd)
atexit.register(self.cleanup)
if sys.version_info < (3, 0):
@@ -154,11 +158,18 @@
termios.tcsetattr(self.fd, termios.TCSANOW, new)
def getkey(self):
+ ready, _, _ = select.select([self.enc_stdin, self.pipe_r], [], [], None)
+ if self.pipe_r in ready:
+ os.read(self.pipe_r, 1)
+ return
c = self.enc_stdin.read(1)
if c == unichr(0x7f):
c = unichr(8) # map the BS key (which yields DEL) to backspace
return c
+ def cancel(self):
+ os.write(self.pipe_w, "x")
+
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)