简体   繁体   English

使用 Python 诅咒在终端中显示彩色 ascii 艺术

[英]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.我想使用 python-curses 在终端(例如 windows cmd)中显示彩色 ascii 艺术。 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 .可以在此处查看/下载 .txt 文件。

This isn't “colored ASCII” — back in the day, we called it ANSI art.这不是“彩色 ASCII”——在过去,我们称之为 ANSI 艺术。 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. Curses 不会解释生成颜色和运动的 ANSI 转义序列,也不会将它们干净利落地传递到终端,即使它可能(通常会,但取决于终端)使用它们本身来呈现其输出。

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.您可以采用两种基本方法: 如果您在更广泛的curses 框架内工作并且确实需要这些功能,您可以自己解释ANSI 序列,并在curses 结构中呈现它们。

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.更简单地说,如果您只想将该图像放在屏幕上,并且如果您知道您的终端已经支持 ANSI,那么请跳过 Curses,并通过标准输出打印每一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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