How to display pre-colored string with curses?

廉价感情. 提交于 2020-05-27 05:08:11

问题


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

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.

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?


回答1:


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?

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

  • 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.



回答2:


There's the "culour" python module which does exactly that.

Install it using pip install culour, and then you can use it to print pre-colored strings:

import culour
culour.addstr(window, colored_string)

This will print the string colored in your window.




回答3:


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".

It is implemented in Python 2x & 3x using the "curses" Low Level GUI package. The Linux nCurses implementation has typically replaced the original Unix Curses implementation.

Your application programs can be programmed using a character-mode subset of the pixel-mode "wxPython" High Level 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.

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. 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))


来源:https://stackoverflow.com/questions/30189434/how-to-display-pre-colored-string-with-curses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!