在终端上使用python显示反视频文本 [英] Display inverse video text with python on terminal
问题描述
Python有一个curses
模块.有没有简单的方法可以使用此模块显示反视频文本?我不想做一个成熟的curses应用程序,只希望文本带有反色(或彩色).
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).
推荐答案
如果使用 initscr
),curses应用程序只会更新屏幕的当前行(不会清除整个屏幕).那将是对 curses 库的最小使用.
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.
如果您想要较低的级别(无需优化,请自己完成),则必须使用该库的 terminfo 级别,即以下功能:
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:
-
initscr
和filter
(因为Python curses显然没有与 newterm ) -
tigetstr
来阅读sgr
,sgr0
,smso
,rmso
,rev
-
tparm
格式化如果您使用的话 -
putp
将字符串写为read/通过tigetstr
和tparm
格式化
initscr
withfilter
(since Python curses apparently has no interface to newterm)tigetstr
to read the capabilities forsgr
,sgr0
,smso
,rmso
,rev
tparm
to format parameters forsgr
if you use thatputp
to write the strings read/formatted viatigetstr
andtparm
页面 Python curses.tigetstr示例 有一些使用tigetstr
的示例(大部分是不完整的),并提醒我可以使用 setupterm
作为newterm
的替代.如果您访问该页面搜索"curses.filter",它肯定有使用示例,但阅读后,我什么都没报告.
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.
进一步阅读:
- 使用python在命令行中完成键入时的操作(显示正在使用的
filter
)
- Complete as-you-type on command line with python (shows
filter
in use)
带有反向视频的演示
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
或
import curses
curses.setupterm()
curses.putp("normal-")
curses.putp(curses.tigetstr("rev"))
curses.putp("Hello world!")
curses.putp(curses.tigetstr("sgr0"))
curses.putp("-normal")
说明了一个要点:用curses.putp
编写的文本绕过了curses库优化.您将使用initscr
或newterm
来进行curses屏幕优化,而使用setupterm
则不使用它,仅使用低级功能和输出功能.
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.
这篇关于在终端上使用python显示反视频文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!