2016-07-19 1 views
2

"libansi"를 쓰고 있습니다. ansi 시퀀스의 반환 코드를 캡처하고 싶습니다. \ x1b [6n 몇 가지 해결 방법을 시도했지만 아무 것도하지 않았습니다.파이썬 3 :` x1b [6n` (` 033 [6n`,` e [6n`) ansi 시퀀스를 다시 얻습니다.

exemple :

#!/usr/bin/python3.4 
rep = os.popen("""a=$(echo "\033[6n") && echo $a""").read() 

은 담당자 반환 "\ 033 [6N"...

누구나 아이디어가?

도움 주셔서 감사합니다.

편집

:

a=input(print("\033[6n", end='') 

그러나 내가 커서 위치를 얻을 입력에 '입력'키를 누릅니다이 필요 : 나는 부분적인 해결책을 가지고 있습니다.

+0

sh/bash cmd의 ANSI 시퀀스가 ​​하위 셸에 에코되기 때문에이 모든 솔루션은 작동하지 않습니다. –

답변

0

문제는 표준 출력 시퀀스를 작성 후 기본 표준 입력에 의해 버퍼링되는 것을

  1. 인, 단말 뜻은 표준 출력보다는, 표준 입력에 대한 응답의 전송. 그래서 단말기는 돌아 가지 않고 실제 키를 누르는 것처럼 행동합니다.

트릭은 을 통해 터미널 속성을 저장하기 전에 기본 동작을 복원하기 위해 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]))