Skip to content

Commit

Permalink
linux: custom terminal settings replacing setraw
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Cube707 committed Jul 25, 2022
1 parent bc8e2f1 commit be02344
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 7 additions & 2 deletions readchar/_posix_read.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 0 additions & 5 deletions tests/posix/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

if sys.platform.startswith("linux"):
import termios
import tty


# ignore all tests in this folder if not on linux
Expand Down Expand Up @@ -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

0 comments on commit be02344

Please sign in to comment.