문제는 표준 출력 시퀀스를 작성 후 기본 표준 입력에 의해 버퍼링되는 것을
- 및
- 인, 단말 뜻은 표준 출력보다는, 표준 입력에 대한 응답의 전송. 그래서 단말기는 돌아 가지 않고 실제 키를 누르는 것처럼 행동합니다.
트릭은 을 통해 터미널 속성을 저장하기 전에 기본 동작을 복원하기 위해 tty.setcbreak(sys.stdin.fileno(), termios.TCSANOW)
을 사용하는 것입니다. cbreak
세트를 사용하면 os.read(sys.stdin.fileno(), 1)
을 즉시 읽을 수 있습니다. 이것은 또한 터미널에서 제어 코드 응답을 억제합니다.
def getpos():
buf = ""
stdin = sys.stdin.fileno()
tattr = termios.tcgetattr(stdin)
try:
tty.setcbreak(stdin, termios.TCSANOW)
sys.stdout.write("\x1b[6n")
sys.stdout.flush()
while True:
buf += sys.stdin.read(1)
if buf[-1] == "R":
break
finally:
termios.tcsetattr(stdin, termios.TCSANOW, tattr)
# reading the actual values, but what if a keystroke appears while reading
# from stdin? As dirty work around, getpos() returns if this fails: None
try:
matches = re.match(r"^\x1b\[(\d*);(\d*)R", buf)
groups = matches.groups()
except AttributeError:
return None
return (int(groups[0]), int(groups[1]))
sh/bash cmd의 ANSI 시퀀스가 하위 셸에 에코되기 때문에이 모든 솔루션은 작동하지 않습니다. –