Python – Use curses with colorama

Use curses with colorama… here is a solution to the problem.

Use curses with colorama

Can I use curses with Colorama? Here is my code and it prints the escape sequence :

from curses import wrapper
import colorama

STYLE = colorama. Fore.GREEN + colorama. Back.BLUE
TITLE = STYLE + 'Current terminal size:'
HEIGHT_STRING = STYLE + 'Screen height: {}\n'
WIDTH_STRING = STYLE + 'Screen width:  {}\n'
STR_LEN = 18

def main(stdscr):

colorama.init()
    stdscr.clear()

height, width = stdscr.getmaxyx()
    y = height//2 - 2
    x = width//2 - STR_LEN//2

stdscr.addstr(y - 2, x, TITLE)
    stdscr.addstr(y, x, HEIGHT_STRING.format(height))
    stdscr.addstr(y + 1, x, WIDTH_STRING.format(width))

stdscr.refresh()
    stdscr.getkey(y + 2, x)

if __name__ == '__main__':
    wrapper(main)

I know curses can’t be used on Windows, just wondering if that’s possible

Solution

Given the description of colorama, there is no (it uses hardcoded escape sequences), there is no way to output in other forms.

According to <a href=”https://docs.python.org/3.3/howto/curses.html” rel=”noreferrer noopener nofollow”>Python documentation, UniCurses should work (on Windows). That is to use PDCurses. ncurses itself works for Windows, and its packages and Cygwin are available in MSYS2.

Related Problems and Solutions