在 python 的 curses 中获得 echo/noecho 状态

Get echo/noecho state in curses in python

我可以使用 echo()noecho() 在 curses 中设置 echo 状态,但是我如何才能看到我当前处于什么状态?这是我正在尝试做的事情:

# get current echo state
if screen.echo_is_on():
    my_echo_state=False   # echo is currently off
    curses.echo()         # turn echo on
else:
    my_echo_state=True    # echo is already on

# get input from user
input_str = screen.getstr(r, c) 

# return echo to previous state
if my_echo_state==False:
    curses.noecho()

我正在寻找可以完成的事情 screen.echo_is_on()

Python curses binding.

echo 没有 getter 函数
  • Python 具有对应于 curses C 函数的方法。
  • C 绑定中 echo(以及其他一些函数)的状态存储在 SCREEN 结构中,该结构通常是不透明的。
  • 其他状态信息存储在 WINDOW 中,它不是不透明的。

原始 curses 界面有扩展。 NetBSD 为 WINDOW 等引入了 opaque 结构的概念。后来,ncurses 将其作为一个选项来帮助 hide/control 线程应用程序的状态信息。为此,ncurses 添加了从 WINDOW 访问数据的函数(参见 curs_opaque(3x))。

但是,没有用于访问 SCREEN 内容的新函数。 enable/disable 函数在 curs_inopts(3x).

中有描述

如果你需要一个 getter,你可以自己写一个,例如 class 隐藏对 [=10= 的实际调用] 和 noecho.

我来晚了,但是 curses 确实有 curses.wrapper 设置 "cbreak mode, turns off echo, enables the terminal keypad, and initializes colors if the terminal has color support" 然后运行你的功能。当您的函数 returns 时,它会恢复 "cooked mode, turns on echo, and disables the terminal keypad"。当您的程序抛出异常时,它还会正确地恢复终端。一个非常方便的功能。

https://docs.python.org/2/library/curses.html