【问题标题】:python readline lib deletes whole line when user hits backspace once当用户按一次退格键时,python readline lib会删除整行
【发布时间】:2012-08-05 05:37:10
【问题描述】:

如果使用sys.stdout.writeprint 而不是raw_input 编写提示,则会发生这种情况。下面的脚本演示了它:

$ cat overwrite.py
import readline, sys

if 'libedit' in readline.__doc__:
    readline.parse_and_bind('bind ^I rl_complete')
else:
    readline.parse_and_bind('tab: complete')
def set_completer(choices):
    choices = sorted(map(str,choices))
    def completer(txt, state):
        if state == 0:
            completer.options = [c for c in choices if c.startswith(txt)]
        if state < len(completer.options):
            return completer.options[state]
        return None
    readline.set_completer(completer)

set_completer(['foo','flup'])
sys.stdout.write('input: ')
x = raw_input()
print x

如果你运行python overwrite.py,你会得到预期的提示:“input:”。如果您按一次退格键,则不会删除任何内容(我猜 readline 认为它已经在行首)。但是,如果您按“f”然后退格键,则包括提示符在内的整行都会被擦除。

必须遍历并替换我写到 stdout 的所有地方并且希望通过调用 raw_input 从用户那里获得输入,所以我希望没有必要使用raw_input。 python 文档在 readline 方面异常稀疏。

【问题讨论】:

    标签: python readline


    【解决方案1】:

    没有其他真正的方法可以解决这个问题;虽然 readline 有 rl_already_prompted 变量,但它仍然需要传入提示,以便 readline 的函数可以正确管理输入行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2016-10-14
      • 2020-11-16
      • 2020-06-12
      • 1970-01-01
      相关资源
      最近更新 更多