diff --git a/readchar/_posix_read.py b/readchar/_posix_read.py index ad83a49..c34a797 100644 --- a/readchar/_posix_read.py +++ b/readchar/_posix_read.py @@ -1,19 +1,24 @@ import sys import termios -import tty # Initially taken from: # http://code.activestate.com/recipes/134892/ # Thanks to Danny Yoo +# more infos from: +# https://gist.github.com/michelbl/efda48b19d3e587685e3441a74457024 +# Thanks to Michel Blancard def readchar() -> str: """Reads a single character from the input stream. Blocks until a character is available.""" fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) + term = termios.tcgetattr(fd) try: - tty.setraw(sys.stdin.fileno()) + term[3] &= ~(termios.ICANON | termios.ECHO | termios.IGNBRK | termios.BRKINT) + termios.tcsetattr(fd, termios.TCSAFLUSH, term) + ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) diff --git a/tests/posix/conftest.py b/tests/posix/conftest.py index 793ef70..f0b55a1 100644 --- a/tests/posix/conftest.py +++ b/tests/posix/conftest.py @@ -3,7 +3,6 @@ if sys.platform.startswith("linux"): import termios - import tty # ignore all tests in this folder if not on linux @@ -33,13 +32,9 @@ def mock_tcgetattr(fd): def mock_tcsetattr(fd, TCSADRAIN, old_settings): return None - def mock_setraw(fd): - return None - mock = mocked_stdin() with pytest.MonkeyPatch.context() as mp: mp.setattr(sys.stdin, "read", mock.read) mp.setattr(termios, "tcgetattr", mock_tcgetattr) mp.setattr(termios, "tcsetattr", mock_tcsetattr) - mp.setattr(tty, "setraw", mock_setraw) yield mock