I'm trying to disable stdin buffering, in order to read the response of ANSI code \033[6n
(which should report the cursor position).
I tried stdin_ub = os.fdopen(stdin.fileno(), 'rb', buffering=0)
as suggested in answer Setting smaller buffer size for sys.stdin?, but still the program is blocked at line ch = stdin_ub.read(1)
of the first attempt to read. It unblocks when return is typed into the terminal, which suggests the stdin is still line buffered.
For reference, here's the complete code:
def getpos():
stdin_ub = os.fdopen(sys.stdin.fileno(), 'rb', buffering=0)
sys.stdout.write('\033[6n')
sys.stdout.flush()
ch, k, field = None, -1, [b'', b'']
while True:
#print('reading wait...')
ch = stdin_ub.read(1)
#print('reading OK')
if ch == b'[': k = 0
elif ch == b';': k = 1
elif ch == b'R': break
elif k >= 0: field[k] += ch
try:
return tuple(map(int, field))
except:
pass
I'm using python 3.5.1