Skip to content

Commit be02344

Browse files
committed
linux: custom terminal settings replacing setraw
fixes #64, as described there, the raw terminal can produce a lot of unneeded whitespace. Using the new configuration it seem to behave as expected.
1 parent bc8e2f1 commit be02344

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

readchar/_posix_read.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import sys
22
import termios
3-
import tty
43

54

65
# Initially taken from:
76
# http://code.activestate.com/recipes/134892/
87
# Thanks to Danny Yoo
8+
# more infos from:
9+
# https://gist.github.com/michelbl/efda48b19d3e587685e3441a74457024
10+
# Thanks to Michel Blancard
911
def readchar() -> str:
1012
"""Reads a single character from the input stream.
1113
Blocks until a character is available."""
1214

1315
fd = sys.stdin.fileno()
1416
old_settings = termios.tcgetattr(fd)
17+
term = termios.tcgetattr(fd)
1518
try:
16-
tty.setraw(sys.stdin.fileno())
19+
term[3] &= ~(termios.ICANON | termios.ECHO | termios.IGNBRK | termios.BRKINT)
20+
termios.tcsetattr(fd, termios.TCSAFLUSH, term)
21+
1722
ch = sys.stdin.read(1)
1823
finally:
1924
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

tests/posix/conftest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
if sys.platform.startswith("linux"):
55
import termios
6-
import tty
76

87

98
# ignore all tests in this folder if not on linux
@@ -33,13 +32,9 @@ def mock_tcgetattr(fd):
3332
def mock_tcsetattr(fd, TCSADRAIN, old_settings):
3433
return None
3534

36-
def mock_setraw(fd):
37-
return None
38-
3935
mock = mocked_stdin()
4036
with pytest.MonkeyPatch.context() as mp:
4137
mp.setattr(sys.stdin, "read", mock.read)
4238
mp.setattr(termios, "tcgetattr", mock_tcgetattr)
4339
mp.setattr(termios, "tcsetattr", mock_tcsetattr)
44-
mp.setattr(tty, "setraw", mock_setraw)
4540
yield mock

0 commit comments

Comments
 (0)