简体   繁体   中英

Display colored ascii art in terminal using Python curses

I want to display a colored ascii art in a terminal (for eg windows cmd) using python-curses. I wrote something really basic like this :

import time
import curses
stdscr = curses.initscr()

curses.noecho()
curses.cbreak()
stdscr.keypad(True)

f = open('betterdays.ansi.txt',"r",encoding="utf8")
asciiart = f.read()
for y, line in enumerate(asciiart.splitlines(), 2):
    stdscr.addstr(y, 2, line)


stdscr.refresh()
time.sleep(5)

curses.echo()
curses.nocbreak()
stdscr.keypad(False)

curses.endwin()

whihc gives me the error:

 stdscr.addstr(y, 2, line)
_curses.error: addwstr() returned ERR

I already looked at this question , but it doesn't solve my problem. Is there a better way to accomplish this? Any suggestions are greatly appreciated.

The .txt file can be seen/downloaded here .

This isn't “colored ASCII” — back in the day, we called it ANSI art. Curses doesn't interpret the ANSI escape sequences that generate color and motion, nor does it pass them through cleanly to the terminal, even though it may (usually does, but it depends on the terminal) use them itself to render its output.

There are two basic approaches you can take: If you're working within a broader curses framework and really want those features, you can interpret the ANSI sequences yourself, and render them in curses structures.

More simply, if you just want to put that image on the screen, and if you know that your terminal already supports ANSI, then skip curses, and just print each line via standard output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM