Skip to content
Advertisement

Positioning the cursor in Windows 10 terminal window using colorama

Every bit of documentation I’ve found suggests that this should work, but the cursor position doesn’t return to 0,0.

colorama.init()

def move (y, x):
    print("33[%d;%dH" % (y, x))

for file in original_file:
        for element in root.iter():
                all_lines = all_lines+1
                if element.text != None:
                    if element.tag in field_list:
                        if len(element.text) > field_list[element.tag]:
                                corrected_lines = corrected_lines+1           
                                move(0, 0)
                                working_message(username, window_width)
                                print("{3}: {0} n {1} to {2}n---".format(element.tag, len(element.text), field_list[element.tag], corrected_lines))
                                print(corrected_lines)
                                print(all_lines)

                                element.text = element.text[field_list[element.tag]]

The important bits being that colorama is initialized, then every loop the cursor should be moved to 0,0 using the move(0,0) function (passing as string of “33[0;0H” to print).

Advertisement

Answer

The top left of the screen is at coordinate 1,1 not 0,0. So your call should be

move(1,1)

And your print() call should look like this:

print("33[%d;%dH" % (y, x), end="")

Without that, move(1,1) will call print() which will issue the escape code to send the cursor to the desired coordinate and then immediately issue a carriage return/line feed. That will turn your move(1,1) effectively into move(1,2).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement