Python,“过滤”行编辑,通过char读取stdin,没有回声

时间:2013-04-13 20:38:03

标签: python readline curses

我需要一个函数将输入读入缓冲区raw_input(),但是在返回完整一行之前不应该回显输入和阻塞,它应该抑制echo并在每次缓冲区更改时调用回调

我说“缓冲区更改”而不是“字符被读取”,因为,raw_input(),我希望它知道特殊键。例如,Backspace应该可以工作。

如果我想,例如,使用回调来模拟输入的大写回声,代码将如下所示:

def callback(text):
    print '\r' + text.upper()

read_input(callback)

我怎样才能做到这一点?

注: 我一直在尝试使用readlinecurses来达到目的,但两个Python绑定都不完整。 curses无法在不清除整个屏幕的情况下启动,readline在任何输入开始之前提供单个挂钩。

1 个答案:

答案 0 :(得分:11)

好吧,我亲自编写了代码。我将留下一个解释供将来参考。

要求

import sys, tty, termios, codecs, unicodedata
from contextlib import contextmanager

禁用行缓冲

简单地读取stdin时出现的第一个问题是行缓冲。我们希望单个字符在没有必要换行符的情况下到达我们的程序,这不是终端操作的默认方式。

为此,我编写了一个处理tty配置的上下文管理器:

@contextmanager
def cbreak():
    old_attrs = termios.tcgetattr(sys.stdin)
    tty.setcbreak(sys.stdin)
    try:
        yield
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attrs)

此管理器启用以下习语:

with cbreak():
    single_char_no_newline = sys.stdin.read(1)

完成后执行清理非常重要,否则终端可能需要reset

解码stdin

仅读取stdin的第二个问题是编码。非ascii unicode字符将逐字节到达我们,这是完全不合需要的。

为了正确解码stdin,我写了一个生成器,我们可以迭代unicode字符:

def uinput():
    reader = codecs.getreader(sys.stdin.encoding)(sys.stdin)
    with cbreak():
        while True:
            yield reader.read(1)

这可能会导致管道故障。我不确定。但是,对于我的用例,它会选择正确的编码并生成一个字符串。

处理特殊字符

首先,我们应该能够告诉可打印的角色与控制角色不同:

def is_printable(c):
    return not unicodedata.category(c).startswith('C')

除了printables,现在,我只想处理←backspace Ctrl D 序列:

def is_backspace(c):
    return c in ('\x08','\x7F')

def is_interrupt(c):
    return c == '\x04'

汇总:xinput()

现在一切都已到位。我想要的函数的原始合同是读取输入,处理特殊字符,调用回调。实施反映了这一点:

def xinput(callback):
    text = ''

    for c in uinput():
        if   is_printable(c): text += c
        elif is_backspace(c): text = text[:-1]
        elif is_interrupt(c): break

        callback(text)

    return text

试一试

def test(text):
    print 'Buffer now holds', text

xinput(test)

运行它并键入 Hellx ←退格 o世界显示:

Buffer now holds H
Buffer now holds He
Buffer now holds Hel
Buffer now holds Hell
Buffer now holds Hellx
Buffer now holds Hell
Buffer now holds Hello
Buffer now holds Hello 
Buffer now holds Hello w
Buffer now holds Hello wo
Buffer now holds Hello wor
Buffer now holds Hello worl
Buffer now holds Hello world