简体   繁体   English

如何显示带有curses的预着色字符串?

[英]How to display pre-colored string with curses?

I'm writing a curses program in Python. 我正在用Python编写curses程序。 I'm a beginner of curses but I've used terminal control sequences for colored output. 我是curses的初学者,但是我使用了彩色输出的终端控制序列。

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之类的库完成,该库输出带有控制序列的突出显示的代码。

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 ). 最初,我将pygments输出直接输入window.addstr() ,但是事实证明,控制序列已转义,并且整个突出显示的字符串都在屏幕上打印出来(就像这样: https : //too-young.me/web /repos/curses-highlight.png )。 How can I display it directly with curses, just like cat ? 我如何cat直接用诅咒展示它?

This has been asked several times, with the same answer: you could write a parser to do this. 这个问题已经被问了好几次了,答案是相同的:您可以编写一个解析器来做到这一点。 For related discussion: 有关讨论:

It is not suitable as an extension to ncurses for example because: 它不适合作为ncurses的扩展,例如,因为:

  • curses produces escape sequences, but for a wide variety of devices (which may not be "ANSI color escapes"). curses 会产生转义序列,但对于各种各样的设备(可能不是“ ANSI颜色转义”)。
  • 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. ncurses(请参阅FAQ 为什么我的错误未得到修复? )没有将其作为扩展提供,因为这种类型的解析器将不依赖于任何ncurses的内部结构。

On GitHub there is a free to use, study, modify and re-distribute High Level GUI library, at " https://github.com/rigordo959/tsWxGTUI_PyVx_Repository ". 在GitHub上,可以免费使用,研究,修改和重新分发高级GUI库, 网址为“ https://github.com/rigordo959/tsWxGTUI_PyVx_Repository ”。

It is implemented in Python 2x & 3x using the "curses" Low Level GUI package. 它使用“ curses”低级GUI包在Python 2x和3x中实现。 The Linux nCurses implementation has typically replaced the original Unix Curses implementation. Linux nCurses实现通常已替换了原始的Unix Curses实现。

Your application programs can be programmed using a character-mode subset of the pixel-mode "wxPython" High Level GUI API. 可以使用像素模式“ wxPython”高级GUI API的字符模式子集对应用程序进行编程。 It supports displays with keyboard and mouse input and various terminal emulators including the color xterms (8-color with 64-color pairs and 16-color with 256-color pairs) and non-color vt100/vt220. 它支持带键盘和鼠标输入的显示器以及各种终端仿真器,包括彩色xterms(8色64色对和16色256色对)和非彩色vt100 / vt220。

Curses enables you to colorize text strings by inserting an attribute (for color, underline, bold, reverse etc.) token before the text and one to restore the previous attribute after the text. Curses使您可以通过在文本之前插入一个属性标记(用于颜色,下划线,粗体,反向等)来为文本字符串着色,并在文本之后插入一个用于恢复先前属性的标记。 For example: 例如:

sampleWindow.attron(curses.A_REVERSE | 
                    curses.color_pair(color_pair_number))
sampleWindow.addstr(begin_y + 3,
                    begin_x + 48,
                    '        ')
sampleWindow.attroff(curses.A_REVERSE |
                     curses.color_pair(color_pair_number))

There's the "culour" python module which does exactly that. 有一个“ culour” python模块可以做到这一点。

Install it using pip install culour , and then you can use it to print pre-colored strings: 使用pip install culour安装它,然后可以使用它打印预着色的字符串:

import culour
culour.addstr(window, colored_string)

This will print the string colored in your window. 这将在窗口中打印彩色的字符串。

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

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