如何显示带有curses的预着色字符串? [英] How to display pre-colored string with curses?

查看:87
本文介绍了如何显示带有curses的预着色字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写一个curses程序.我是curses的初学者,但是我使用了终端控制序列进行彩色输出.

I'm writing a curses program in Python. I'm a beginner of curses but I've used terminal control sequences for colored output.

现在在窗口中有一些代码片段要打印,我希望对它们进行语法突出显示,最好使用像pygments这样的库来完成,这些库会输出带有控制序列的突出显示的代码.

Now there's some code snippets to print inside the window, I'd like them be syntax highlighted, and it's better done with libraries like pygments, which outputs highlighted code with control sequences.

最初,我将pygments输出直接输入到window.addstr(),但是事实证明,控制序列已转义,并且整个突出显示的字符串都显示在屏幕上(就像这样:

Initially I feed pygments output directly to window.addstr(), but it is turned out that the control sequences is escaped and the whole highlighted string is printed on the screen (just like this: https://too-young.me/web/repos/curses-highlight.png). How can I display it directly with curses, just like cat?

推荐答案

已多次询问,答案相同:您可以编写解析器来执行此操作.对于相关讨论:

This has been asked several times, with the same answer: you could write a parser to do this. For related discussion:

  • How to use ANSI escape codes inside mvwprintw in ncurses?
  • Comment on Parsing ANSI color escape sequences
  • Handle escape sequences with ncurses? Does printf handle escape sequences?

它不适合作为ncurses的扩展,例如,因为:

It is not suitable as an extension to ncurses for example because:

  • 诅咒产生转义序列,但是对于各种各样的设备(可能不是"ANSI颜色转义").
  • ncurses(请参阅常见问题解答 为什么我的错误不存在是固定的? )没有提供它作为扩展,因为这种类型的解析器不会依赖任何ncurses的内部函数.
  • curses produces escape sequences, but for a wide variety of devices (which may not be "ANSI color escapes").
  • ncurses (see the FAQ Why aren't my bugs being fixed?) does not provide it as an extension because a parser of this type would not rely upon any of ncurses' internals.

这篇关于如何显示带有curses的预着色字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆