在终端上使用python显示反视频文本 [英] Display inverse video text with python on terminal

查看:116
本文介绍了在终端上使用python显示反视频文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

  • initscrfilter(因为Python curses显然没有与 newterm )
  • tigetstr 来阅读sgrsgr0smsormsorev
  • tparm 格式化如果您使用的话
  • putp 将字符串写为read/通过tigetstrtparm
  • 格式化
  • initscr with filter (since Python curses apparently has no interface to newterm)
  • tigetstr to read the capabilities for sgr, sgr0, smso, rmso, rev
  • tparm to format parameters for sgr if you use that
  • putp to write the strings read/formatted via tigetstr and tparm

页面 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.

进一步阅读:

  • 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库优化.您将使用initscrnewterm来进行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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆