Python has a curses module. Is there a simple way to use this module to display inverse video text? I don't want to do the full-blown curses application, just want the text to bring inverse (or in color).
1 Answers
If you use the filter function (before initscr), the curses application will only update the current line of the screen (and will not clear the whole screen). That would be a minimal use of the curses library.
If you want a lower-level (no optimization, do it yourself), you would have to use the terminfo level of the library, i.e., these functions:
initscrwithfilter(since Python curses apparently has no interface to newterm)tigetstrto read the capabilities forsgr,sgr0,smso,rmso,revtparmto format parameters forsgrif you use thatputpto write the strings read/formatted viatigetstrandtparm
The page Python curses.tigetstr Examples has some (mostly incomplete) examples using tigetstr, and reminds me that you could use setupterm as an alternative to newterm. If you visit that page searching for "curses.filter", it asserts there are examples of its use, but reading, I found nothing to report.
Further reading:
- Complete as-you-type on command line with python (shows
filterin use)
A demo with reverse-video:
import curses
curses.filter()
stdscr = curses.initscr()
stdscr.addstr("normal-")
stdscr.addstr("Hello world!", curses.A_REVERSE)
stdscr.addstr("-normal")
stdscr.refresh()
curses.endwin()
print
or
import curses
curses.setupterm()
curses.putp("normal-")
curses.putp(curses.tigetstr("rev"))
curses.putp("Hello world!")
curses.putp(curses.tigetstr("sgr0"))
curses.putp("-normal")
illustrates a point: text written with curses.putp bypasses the curses library optimization. You would use initscr or newterm to use the curses screen-optimization, but setupterm to not use it, just using the low-level capability- and output-functions.
- 1
- 1
- 51,086
- 7
- 70
- 105
-
Yes, I discovered that python has no interface to `terminfo`. Do you know of an example that shows `initscr` with `filter`? – vy32 Feb 08 '16 at 03:53
-
Not offhand in python - the [ncurses-examples](http://invisible-island.net/ncurses/ncurses-examples.html) have an example `filter.c` in C. But the functions I listed for terminfo are documented as part of python's C binding. – Thomas Dickey Feb 08 '16 at 09:02
-
You aren't aware of a python example? – vy32 Feb 08 '16 at 13:45