简体   繁体   English

在Python中获取curses中的回声/无回声状态

[英]Get echo/noecho state in curses in python

I can set the echo state in curses with echo() and noecho() , but how can I see what state I'm currently in? 我可以使用echo()noecho()在curses中设置回显状态,但是如何查看当前处于什么状态? Here's what I'm trying to do: 这是我想做的事情:

# 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()

I'm looking for something to accomplish screen.echo_is_on() . 我在寻找可以完成screen.echo_is_on()东西。

There's no getter function for echo in the Python curses binding . Python curses绑定中没有用于echogetter函数。

  • Python has methods that correspond to the curses C functions. Python具有与curses C函数相对应的方法。
  • The state of echo (and some other functions) in the C binding are stored in the SCREEN struct, which traditionally is opaque. C绑定中的echo状态(和某些其他函数)存储在SCREEN结构中,该结构通常是不透明的。
  • other state information is stored in WINDOW which was not opaque. 其他状态信息存储在不透明的WINDOW中。

There are extensions to the original curses interface. 原始curses界面有扩展 NetBSD introduced the notion of opaque structures for WINDOW , etc. Later, ncurses adopted that as an option to help hide/control state information for threaded applications. NetBSD引入了WINDOW等的不透明结构的概念。后来,ncurses将其用作帮助隐藏/控制线程应用程序的状态信息的选项。 To do this, ncurses added functions for accessing data from WINDOW (see curs_opaque(3x) ). 为此,ncurses添加了用于从WINDOW访问数据的函数(请参见curs_opaque(3x) )。

However, there are no new functions for accessing the contents of SCREEN . 但是,没有用于访问SCREEN内容的新功能。 The enable/disable functions for those are described in curs_inopts(3x) . curs_inopts(3x)中描述了这些功能的启用/禁用功能。

If you need a getter , you could write one yourself, eg, as a class which hides the actual calls to echo and noecho . 如果您需要一个吸气剂 ,则可以自己编写一个,例如,作为一个隐藏对echonoecho的实际调用的类。

I'm late to the party, but curses does have curses.wrapper which sets "cbreak mode, turns off echo, enables the terminal keypad, and initializes colors if the terminal has color support" then runs your function. 我来晚了,但是curses确实有curses.wrapper ,它可以设置“ cbreak模式,关闭回显,启用终端键盘,并在终端支持颜色的情况下初始化颜色”,然后运行您的功能。 When your function returns it restores "cooked mode, turns on echo, and disables the terminal keypad". 当您的函数返回时,它将恢复“已烹饪模式,打开回显,并禁用终端键盘”。 It also properly restores the terminal when your program throws an exception. 当程序引发异常时,它还可以正确还原终端。 A thorougly handy feature. 非常方便的功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM