jquast / blessed

Blessed is an easy, practical library for making python terminal apps

Home Page:http://pypi.python.org/pypi/blessed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

move_xy does not move the cursor (strange behavior)

MRog40 opened this issue · comments

I'm trying to move the input cursor for user interaction/editing of a prompt, and on Windows 10 (don't have linux to test) I cannot get move_xy to put the cursor where I want on screen.

Minimum example:

from blessed import Terminal
import time

term = Terminal()
print(term.clear)

print(term.move_xy(0, 0) + "Moving the cursor:")

for c in ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+']:
    for i in range(20):
        # print('') # Without this line, nothing happens ?
        print(term.move_xy(i, 1) + c, end='')
        time.sleep(0.2)

I want to see the cursor move with the text, but instead I get nothing from this code.

Uncommenting the print('') line will make the output characters get printed:

image
Cursor is on next line while printing, in spite of end=''

I think you're seeing line buffering. Try adding sys.stdout.flush() before time.sleep().

sys.stdout.flush()

fixed my issue! Thank you @avylove

You can use flush argument anytime to print: print(‘’, flush=True, end=‘’)

we should document the need to flush when printing without a newline somewhere

Thanks for the note; flush=True is simpler. I agree adding that to documentation in the "Location" section would be good. I read "Note our use of end='' ..." and never thought to flush stdout.

Also - thanks for the great library. I was looking for something with easier colors/formatting than curses but still with low level and extensible terminal control (I tried prompt_toolkit before this and it's terrible).