Move cursor up using Colorama has problems at bottom of screen

2.4k views Asked by At

I'm using Colorama in Python (32-bit 2.7.2) on Windows 7 (64-bit) and it works great for colouring text in the console, but I'm having problems when getting it to move the cursor.

Specifically, if I use the ANSI code to go up a line, it works when the cursor is far from the bottom of the screen, but when the cursor is near the bottom the cursor doesn't move up correctly and then text starts to be printed further down the page causing it to scroll.

The code I use to move up a line is:

sys.stdout.write('\x1b[4A')

where 4 is moving it four lines up (and something like '\x1b[8A' would move it eight lines up)

I'm not sure if this is a lack of understanding on my part regarding how ANSI codes work or whether it is an issue with Colorama.

To recreate it, run something like this in either the normal Windows Command Prompt (cmd.exe) or in Console2 (it seems to make no difference)

from __future__ import print_function
import colorama
from colorama import Fore, Back, Style
import sys

def main():

    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    print('Blah')
    sys.stdout.write('\x1b[6A')
    sys.stdout.write('some text')

if __name__ == '__main__':
    main()

If you run the code above near the top of the screen, it'll end up with "some text" written part way through the "Blah" output, but if you start it when already near the bottom of the screen the "some text" will be at the end, with the cursor seemingly not having scrolled back at all.

I specifically need to move the cursor up, so it's placed on a relative basis to other output, rather than give it an absolute screen position (ie move it to position x,y)

Any suggestions on where to start?

2

There are 2 answers

0
Paul Boț On

You should import "init" from colorama and call "init()" before trying to move up the cursor.

0
Udhay Prakash On

colorama.init() need to be added. It is essential in windows, but optional in *nix systems.

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.On other platforms, calling init() has no effect.

To stop using colorama before your program exits, simply call deinit(). This will restore stdout and stderr to their original values, so that Colorama is disabled. To resume using Colorama again, call reinit(); it is cheaper to calling init() again (but does the same thing).

colorama.init() accepts some **kwargs to override its default behaviour.

Default will be

colorama.init(convert = True, wrap = True, autoreset=False, strip = None)

init(autoreset=True) will reset the color/wrap/strip changes after every statement.

colorama.init(autoreset=True) # To reset the color to defaults, for every statement

colorama.init(convert = True) # works in the same way as colorama.init()

colorama.init(convert = False) # Used when we want to stop the color change

colorama.init(wrap = True)# works in the same way as colorama.init()

colorama.init(convert = False) # Used when we want to stop the color change