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
?
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With