Python 是否可以在不插入新行的情况下获取用户输入?

Python 是否可以在不插入新行的情况下获取用户输入?,python,Python,我知道我可以通过添加逗号来阻止print写换行符 print "Hello, world!", 但是我如何阻止raw\u input写换行符呢 print "Hello, ", name = raw_input() print ", how do you do?" 结果: 你好,托马斯 你好 我想要的结果是: 你好,托马斯,你好吗 但是如何阻止原始输入写入换行符呢 print "Hello, ", name = raw_input() print ", how do you do?" 简言

我知道我可以通过添加逗号来阻止print写换行符

print "Hello, world!",
但是我如何阻止
raw\u input
写换行符呢

print "Hello, ",
name = raw_input()
print ", how do you do?"
结果:

你好,托马斯
你好

我想要的结果是:

你好,托马斯,你好吗

但是如何阻止原始输入写入换行符呢

print "Hello, ",
name = raw_input()
print ", how do you do?"
简言之:你不能

raw\u input()
将始终回显用户输入的文本,包括尾随的换行符。这意味着用户键入的内容都将打印到标准输出


如果要防止出现这种情况,必须使用终端控制库,如模块。但是,这是不可移植的,例如,
诅咒在Windows系统上不可用。

这在某种程度上绕过了它,但没有为变量
名称指定任何内容:

print("Hello, {0}, how do you do?".format(raw_input("Enter name here: ")))

但在打印整个消息之前,它会提示用户输入一个名称。

我发现没有人给出有效的解决方案,所以我决定试试。 如上所述,在用户输入后,不打印新行是不可能获得
raw\u input()
的。然而,有可能回到你以前的路线。 我把它做成了一行。您可以使用:

print '\033[{}C\033[1A'.format(len(x) + y),
其中,
x
是给定用户输入长度的整数,
y
原始输入()字符串长度的整数。虽然它可能无法在所有终端上工作(正如我在了解此方法时所读到的),但它在我的终端上工作良好。我用的是Kubuntu 14.04。

字符串
'\033[4C'
用于向右跳转4个索引,因此它相当于
'*4
。同样,字符串
'\033[1A'
用于跳转1行。通过使用字符串最后一个索引上的字母
A
B
C
D
,可以分别向上、向下、向右和向左移动。


请注意,如果有一个打印字符,向上一行将删除该位置上的现有打印字符。

正如Nick K.所说,在回显换行符之前,您需要将文本光标移回。问题是,您无法轻松获得上一行的长度以便向右移动,以免存储打印的每个字符串,提示d输入到它自己的变量中

下面是一个类(适用于Python 3),它通过自动存储终端的最后一行来修复此问题(前提是您使用了它的方法)。与使用终端控件库相比,此功能的好处在于,它可以在最新版本的Windows和*NIX操作系统的标准终端中工作。它还可以在获取输入之前打印“Hello”提示

如果您使用的是Windows,而不是Windows 10的v1511,那么您需要安装
colorama
模块,否则这将不起作用,因为该版本中提供了ANSI光标移动支持

# For the sys.stdout file-like object
import sys
import platform

if platform.system() == 'Windows':
    try:
        import colorama
    except ImportError:
        import ctypes
        kernel32 = ctypes.windll.kernel32
        # Enable ANSI support on Windows 10 v1511
        kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
    else:
        colorama.init()
else:
    # Fix Linux arrow key support in Python scripts
    import readline


class TempHistory:
    """Record one line from the terminal.

    It is necessary to keep track of the last line on the terminal so we
    can move the text cursor rightward and upward back into the position
    before the newline from the `input` function was echoed.

    Note: I use the term 'echo' to refer to when text is
    shown on the terminal but might not be written to `sys.stdout`.

    """

    def __init__(self):
        """Initialise `line` and save the `print` and `input` functions.

        `line` is initially set to '\n' so that the `record` method
        doesn't raise an error about the string index being out of range.

        """
        self.line = '\n'
        self.builtin_print = print
        self.builtin_input = input

    def _record(self, text):
        """Append to `line` or overwrite it if it has ended."""
        if text == '':
            # You can't record nothing
            return
        # Take into account `text` being multiple lines
        lines = text.split('\n')
        if text[-1] == '\n':
            last_line = lines[-2] + '\n'
            # If `text` ended with a newline, then `text.split('\n')[-1]`
            # would have merely returned the newline, and not the text
            # preceding it
        else:
            last_line = lines[-1]
        # Take into account return characters which overwrite the line
        last_line = last_line.split('\r')[-1]
        # `line` is considered ended if it ends with a newline character
        if self.line[-1] == '\n':
            self.line = last_line
        else:
            self.line += last_line

    def _undo_newline(self):
        """Move text cursor back to its position before echoing newline.

        ANSI escape sequence: `\x1b[{count}{command}`
        `\x1b` is the escape code, and commands `A`, `B`, `C` and `D` are
        for moving the text cursor up, down, forward and backward {count}
        times respectively.

        Thus, after having echoed a newline, the final statement tells
        the terminal to move the text cursor forward to be inline with
        the end of the previous line, and then move up into said line
        (making it the current line again).

        """
        line_length = len(self.line)
        # Take into account (multiple) backspaces which would
        # otherwise artificially increase `line_length`
        for i, char in enumerate(self.line[1:]):
            if char == '\b' and self.line[i-1] != '\b':
                line_length -= 2
        self.print('\x1b[{}C\x1b[1A'.format(line_length),
                   end='', flush=True, record=False)

    def print(self, *args, sep=' ', end='\n', file=sys.stdout, flush=False,
              record=True):
        """Print to `file` and record the printed text.

        Other than recording the printed text, it behaves exactly like
        the built-in `print` function.

        """
        self.builtin_print(*args, sep=sep, end=end, file=file, flush=flush)
        if record:
            text = sep.join([str(arg) for arg in args]) + end
            self._record(text)

    def input(self, prompt='', newline=True, record=True):
        """Return one line of user input and record the echoed text.

        Other than storing the echoed text and optionally stripping the
        echoed newline, it behaves exactly like the built-in `input`
        function.

        """
        if prompt == '':
            # Prevent arrow key overwriting previously printed text by
            # ensuring the built-in `input` function's `prompt` argument
            # isn't empty
            prompt = ' \b'
        response = self.builtin_input(prompt)
        if record:
            self._record(prompt)
            self._record(response)
        if not newline:
            self._undo_newline()
        return response


record = TempHistory()
# For convenience
print = record.print
input = record.input

print('Hello, ', end='', flush=True)
name = input(newline=False)
print(', how do you do?)

回溯换行的另一种方法是定义您自己的函数,该函数模拟内置的
输入
函数,将每个击键回显并附加到
响应
变量(Enter除外,Enter将返回响应),同时还处理Backspace、Del、Home、End、箭头键、行历史记录、键盘中断、EOFError、SIGTSTP和从剪贴板粘贴。非常简单

请注意,在Windows上,如果您想像在通常的
input
功能中那样使用带有箭头键的行历史记录,则需要安装
pyradline
,尽管它不完整,因此功能仍然不完全正确。此外,如果您不在v1511或更高版本的Windows 10上,则需要安装
colorama
模块(如果您在Linux或macOS上,则无需执行任何操作)

另外,由于
msvcrt.getwch
使用“\xe0”表示特殊字符,您将无法键入“a”。不过您应该能够粘贴它

下面是在更新的Windows 10系统(至少v1511)、基于Debian的Linux发行版以及macOS和其他*NIX操作系统上实现此功能的代码。无论您是否在Windows上安装了
pyreadline
,它都应该可以工作,尽管它缺少一些功能

在windows_specific.py中

"""Windows-specific functions and variables for input_no_newline."""
import ctypes
from msvcrt import getwch  # pylint: disable=import-error, unused-import
from shared_stuff import ANSI

try:
    import colorama  # pylint: disable=import-error
except ImportError:
    kernel32 = ctypes.windll.kernel32
    # Enable ANSI support to move the text cursor
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
    colorama.init()


def get_clipboard_data():
    """Return string previously copied from Windows clipboard.

    Adapted from <http://stackoverflow.com/a/23285159/6379747>.

    """
    CF_TEXT = 1
    user32 = ctypes.windll.user32
    user32.OpenClipboard(0)
    try:
        if user32.IsClipboardFormatAvailable(CF_TEXT):
            data = user32.GetClipboardData(CF_TEXT)
            data_locked = kernel32.GlobalLock(data)
            text = ctypes.c_char_p(data_locked)
            kernel32.GlobalUnlock(data_locked)
    finally:
        user32.CloseClipboard()
    return text.value


def sigtstp():
    """Raise EOFError from Ctrl-Z since SIGTSTP doesn't exist on Windows."""
    raise EOFError


input_code = {
    **ANSI,
    'CSI': [['\xe0', '\x00'], ''],
    'up': 'H',
    'down': 'P',
    'right': 'M',
    'left': 'K',
    'end': 'O',
    'home': 'G',
    'backspace': '\b',
    'del': 'S',
}
"""Functions and variables for Debian-based Linux distros and macOS."""
import sys
import os
import tty
import signal
import termios
from shared_stuff import ANSI

def getwch():
    """Return a single character from user input without echoing.

    ActiveState code, adapted from
    <http://code.activestate.com/recipes/134892> by Danny Yoo under
    the Python Software Foundation license.

    """
    file_descriptor = sys.stdin.fileno()
    old_settings = termios.tcgetattr(file_descriptor)
    try:
        tty.setraw(file_descriptor)
        char = sys.stdin.read(1)
    finally:
        termios.tcsetattr(file_descriptor, termios.TCSADRAIN, old_settings)
    return char


def get_clipboard_data():
    """Return nothing; *NIX systems automagically change sys.stdin."""
    return ''


def sigtstp():
    """Suspend the script."""
    os.kill(os.getpid(), signal.SIGTSTP)


input_code = {
    **ANSI,
    'CSI': ['\x1b', '['],
    'backspace': '\x7f',
    'del': ['3', '~'],
}
"""Provide functions for up and down arrows if readline is installed.

Basically to prevent duplicate code and make it work on systems without
readline.

"""
try:
    import readline
except ImportError:
    import pyreadline as readline
from shared_stuff import move_cursor


def init_history_index():
    """Return index for last element of readline.get_history_item."""
    # readline.get_history_item is one-based
    return readline.get_current_history_length() + 1


def restore_history(history_index, replaced, cursor_position):
    """Replace 'replaced' with history and return the replacement."""
    try:
        replacement = readline.get_history_item(history_index)
    except IndexError:
        replacement = None
    if replacement is not None:
        move_cursor('right', len(replaced) - cursor_position)
        print('\b \b' * len(replaced), end='', flush=True)
        print(replacement, end='', flush=True)
        return replacement
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Store history and then replace it."""
    old_history[history_index] = readline.get_history_item(history_index)
    try:
        readline.replace_history_item(history_index - 1, replacement)
    except AttributeError:
    # pyreadline is incomplete
        pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some up-arrow logic."""
    try:
        history = readline.get_history_item(history_index - 1)
    except IndexError:
        history = None
    if history is not None:
        if history_index > readline.get_current_history_length():
            readline.add_history(replaced)
            input_replaced = True
        else:
            store_and_replace_history(
                history_index, replaced, old_history)
            history_modified = True
        history_index -= 1
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some down-arrow logic."""
    try:
        history = readline.get_history_item(history_index + 1)
    except IndexError:
        history = None
    if history is not None:
        store_and_replace_history(history_index, replaced, old_history)
        history_modified = True
        history_index += 1
        input_replaced = (not history_index
                            == readline.get_current_history_length())
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Change history before the response will be returned elsewhere."""
    try:
        if input_replaced:
            readline.remove_history_item(history_index - 1)
        elif history_modified:
            readline.remove_history_item(history_index - 1)
            readline.add_history(old_history[history_index - 1])
    except AttributeError:
    # pyreadline is also missing remove_history_item
        pass
    readline.add_history(response)
"""Provide dummy functions for if readline isn't available."""
# pylint: disable-msg=unused-argument


def init_history_index():
    """Return an index of 1 which probably won't ever change."""
    return 1


def restore_history(history_index, replaced, cursor_position):
    """Return the replaced thing without replacing it."""
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Don't store history."""
    pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Return 'input_replaced' and 'history_modified' without change."""
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Also return 'input_replaced' and 'history_modified'."""
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Don't change nonexistent history."""
    pass
"""Provide platform-independent functions and variables."""
ANSI = {
    'CSI': '\x1b[',
    'up': 'A',
    'down': 'B',
    'right': 'C',
    'left': 'D',
    'end': 'F',
    'home': 'H',
    'enter': '\r',
    '^C': '\x03',
    '^D': '\x04',
    '^V': '\x16',
    '^Z': '\x1a',
}


def move_cursor(direction, count=1):
    """Move the text cursor 'count' times in the specified direction."""
    if direction not in ['up', 'down', 'right', 'left']:
        raise ValueError("direction should be either 'up', 'down', 'right' "
                         "or 'left'")
    # A 'count' of zero still moves the cursor, so this needs to be
    # tested for.
    if count != 0:
        print(ANSI['CSI'] + str(count) + ANSI[direction], end='', flush=True)


def line_insert(text, extra=''):
    """Insert text between terminal line and reposition cursor."""
    if not extra:
    # It's not guaranteed that the new line will completely overshadow
    # the old one if there is no extra. Maybe something was 'deleted'?
        move_cursor('right', len(text) + 1)
        print('\b \b' * (len(text)+1), end='', flush=True)
    print(extra + text, end='', flush=True)
    move_cursor('left', len(text))
#!/usr/bin/python3
"""Provide an input function that doesn't echo a newline."""
try:
from windows_specific import getwch, get_clipboard_data, sigtstp, input_code
except ImportError:
    from unix_specific import getwch, get_clipboard_data, sigtstp, input_code
try:
    from readline_available import (init_history_index, restore_history,
                                    store_and_replace_history,
                                    handle_prev_history, handle_next_history,
                                    finalise_history)
except ImportError:
    from readline_unavailable import (init_history_index, restore_history,
                                      store_and_replace_history,
                                      handle_prev_history, handle_next_history,
                                      finalise_history)
from shared_stuff import ANSI, move_cursor, line_insert


def input_no_newline(prompt=''):  # pylint: disable=too-many-branches, too-many-statements
    """Echo and return user input, except for the newline."""
    print(prompt, end='', flush=True)
    response = ''
    position = 0
    history_index = init_history_index()
    input_replaced = False
    history_modified = False
    replacements = {}

    while True:
        char = getwch()
        if char in input_code['CSI'][0]:
            char = getwch()
            # Relevant input codes are made of two to four characters
            if char == input_code['CSI'][1]:
                # *NIX uses at least three characters, only the third is
                # important
                char = getwch()
            if char == input_code['up']:
                (history_index, input_replaced, history_modified) = (
                    handle_prev_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['down']:
                (history_index, input_replaced, history_modified) = (
                    handle_next_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['right'] and position < len(response):
                move_cursor('right')
                position += 1
            elif char == input_code['left'] and position > 0:
                move_cursor('left')
                position -= 1
            elif char == input_code['end']:
                move_cursor('right', len(response) - position)
                position = len(response)
            elif char == input_code['home']:
                move_cursor('left', position)
                position = 0
            elif char == input_code['del'][0]:
                if ''.join(input_code['del']) == '3~':
                    # *NIX uses '\x1b[3~' as its del key code, but only
                    # '\x1b[3' has currently been read from sys.stdin
                    getwch()
                backlog = response[position+1 :]
                response = response[:position] + backlog
                line_insert(backlog)
        elif char == input_code['backspace']:
            if position > 0:
                backlog = response[position:]
                response = response[: position-1] + backlog
                print('\b', end='', flush=True)
                position -= 1
                line_insert(backlog)
        elif char == input_code['^C']:
            raise KeyboardInterrupt
        elif char == input_code['^D']:
            raise EOFError
        elif char == input_code['^V']:
            paste = get_clipboard_data()
            backlog = response[position:]
            response = response[:position] + paste + backlog
            position += len(paste)
            line_insert(backlog, extra=paste)
        elif char == input_code['^Z']:
            sigtstp()
        elif char == input_code['enter']:
            finalise_history(history_index, response, replacements,
                             input_replaced, history_modified)
            move_cursor('right', len(response) - position)
            return response
        else:
            backlog = response[position:]
            response = response[:position] + char + backlog
            position += 1
            line_insert(backlog, extra=char)


def main():
    """Called if script isn't imported."""
    # "print(text, end='')" is equivalent to "print text,", and 'flush'
    # forces the text to appear, even if the line isn't terminated with
    # a '\n'
    print('Hello, ', end='', flush=True)
    name = input_no_newline()  # pylint: disable=unused-variable
    print(', how do you do?')


if __name__ == '__main__':
    main()
readline\u available.py中

"""Windows-specific functions and variables for input_no_newline."""
import ctypes
from msvcrt import getwch  # pylint: disable=import-error, unused-import
from shared_stuff import ANSI

try:
    import colorama  # pylint: disable=import-error
except ImportError:
    kernel32 = ctypes.windll.kernel32
    # Enable ANSI support to move the text cursor
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
    colorama.init()


def get_clipboard_data():
    """Return string previously copied from Windows clipboard.

    Adapted from <http://stackoverflow.com/a/23285159/6379747>.

    """
    CF_TEXT = 1
    user32 = ctypes.windll.user32
    user32.OpenClipboard(0)
    try:
        if user32.IsClipboardFormatAvailable(CF_TEXT):
            data = user32.GetClipboardData(CF_TEXT)
            data_locked = kernel32.GlobalLock(data)
            text = ctypes.c_char_p(data_locked)
            kernel32.GlobalUnlock(data_locked)
    finally:
        user32.CloseClipboard()
    return text.value


def sigtstp():
    """Raise EOFError from Ctrl-Z since SIGTSTP doesn't exist on Windows."""
    raise EOFError


input_code = {
    **ANSI,
    'CSI': [['\xe0', '\x00'], ''],
    'up': 'H',
    'down': 'P',
    'right': 'M',
    'left': 'K',
    'end': 'O',
    'home': 'G',
    'backspace': '\b',
    'del': 'S',
}
"""Functions and variables for Debian-based Linux distros and macOS."""
import sys
import os
import tty
import signal
import termios
from shared_stuff import ANSI

def getwch():
    """Return a single character from user input without echoing.

    ActiveState code, adapted from
    <http://code.activestate.com/recipes/134892> by Danny Yoo under
    the Python Software Foundation license.

    """
    file_descriptor = sys.stdin.fileno()
    old_settings = termios.tcgetattr(file_descriptor)
    try:
        tty.setraw(file_descriptor)
        char = sys.stdin.read(1)
    finally:
        termios.tcsetattr(file_descriptor, termios.TCSADRAIN, old_settings)
    return char


def get_clipboard_data():
    """Return nothing; *NIX systems automagically change sys.stdin."""
    return ''


def sigtstp():
    """Suspend the script."""
    os.kill(os.getpid(), signal.SIGTSTP)


input_code = {
    **ANSI,
    'CSI': ['\x1b', '['],
    'backspace': '\x7f',
    'del': ['3', '~'],
}
"""Provide functions for up and down arrows if readline is installed.

Basically to prevent duplicate code and make it work on systems without
readline.

"""
try:
    import readline
except ImportError:
    import pyreadline as readline
from shared_stuff import move_cursor


def init_history_index():
    """Return index for last element of readline.get_history_item."""
    # readline.get_history_item is one-based
    return readline.get_current_history_length() + 1


def restore_history(history_index, replaced, cursor_position):
    """Replace 'replaced' with history and return the replacement."""
    try:
        replacement = readline.get_history_item(history_index)
    except IndexError:
        replacement = None
    if replacement is not None:
        move_cursor('right', len(replaced) - cursor_position)
        print('\b \b' * len(replaced), end='', flush=True)
        print(replacement, end='', flush=True)
        return replacement
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Store history and then replace it."""
    old_history[history_index] = readline.get_history_item(history_index)
    try:
        readline.replace_history_item(history_index - 1, replacement)
    except AttributeError:
    # pyreadline is incomplete
        pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some up-arrow logic."""
    try:
        history = readline.get_history_item(history_index - 1)
    except IndexError:
        history = None
    if history is not None:
        if history_index > readline.get_current_history_length():
            readline.add_history(replaced)
            input_replaced = True
        else:
            store_and_replace_history(
                history_index, replaced, old_history)
            history_modified = True
        history_index -= 1
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some down-arrow logic."""
    try:
        history = readline.get_history_item(history_index + 1)
    except IndexError:
        history = None
    if history is not None:
        store_and_replace_history(history_index, replaced, old_history)
        history_modified = True
        history_index += 1
        input_replaced = (not history_index
                            == readline.get_current_history_length())
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Change history before the response will be returned elsewhere."""
    try:
        if input_replaced:
            readline.remove_history_item(history_index - 1)
        elif history_modified:
            readline.remove_history_item(history_index - 1)
            readline.add_history(old_history[history_index - 1])
    except AttributeError:
    # pyreadline is also missing remove_history_item
        pass
    readline.add_history(response)
"""Provide dummy functions for if readline isn't available."""
# pylint: disable-msg=unused-argument


def init_history_index():
    """Return an index of 1 which probably won't ever change."""
    return 1


def restore_history(history_index, replaced, cursor_position):
    """Return the replaced thing without replacing it."""
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Don't store history."""
    pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Return 'input_replaced' and 'history_modified' without change."""
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Also return 'input_replaced' and 'history_modified'."""
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Don't change nonexistent history."""
    pass
"""Provide platform-independent functions and variables."""
ANSI = {
    'CSI': '\x1b[',
    'up': 'A',
    'down': 'B',
    'right': 'C',
    'left': 'D',
    'end': 'F',
    'home': 'H',
    'enter': '\r',
    '^C': '\x03',
    '^D': '\x04',
    '^V': '\x16',
    '^Z': '\x1a',
}


def move_cursor(direction, count=1):
    """Move the text cursor 'count' times in the specified direction."""
    if direction not in ['up', 'down', 'right', 'left']:
        raise ValueError("direction should be either 'up', 'down', 'right' "
                         "or 'left'")
    # A 'count' of zero still moves the cursor, so this needs to be
    # tested for.
    if count != 0:
        print(ANSI['CSI'] + str(count) + ANSI[direction], end='', flush=True)


def line_insert(text, extra=''):
    """Insert text between terminal line and reposition cursor."""
    if not extra:
    # It's not guaranteed that the new line will completely overshadow
    # the old one if there is no extra. Maybe something was 'deleted'?
        move_cursor('right', len(text) + 1)
        print('\b \b' * (len(text)+1), end='', flush=True)
    print(extra + text, end='', flush=True)
    move_cursor('left', len(text))
#!/usr/bin/python3
"""Provide an input function that doesn't echo a newline."""
try:
from windows_specific import getwch, get_clipboard_data, sigtstp, input_code
except ImportError:
    from unix_specific import getwch, get_clipboard_data, sigtstp, input_code
try:
    from readline_available import (init_history_index, restore_history,
                                    store_and_replace_history,
                                    handle_prev_history, handle_next_history,
                                    finalise_history)
except ImportError:
    from readline_unavailable import (init_history_index, restore_history,
                                      store_and_replace_history,
                                      handle_prev_history, handle_next_history,
                                      finalise_history)
from shared_stuff import ANSI, move_cursor, line_insert


def input_no_newline(prompt=''):  # pylint: disable=too-many-branches, too-many-statements
    """Echo and return user input, except for the newline."""
    print(prompt, end='', flush=True)
    response = ''
    position = 0
    history_index = init_history_index()
    input_replaced = False
    history_modified = False
    replacements = {}

    while True:
        char = getwch()
        if char in input_code['CSI'][0]:
            char = getwch()
            # Relevant input codes are made of two to four characters
            if char == input_code['CSI'][1]:
                # *NIX uses at least three characters, only the third is
                # important
                char = getwch()
            if char == input_code['up']:
                (history_index, input_replaced, history_modified) = (
                    handle_prev_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['down']:
                (history_index, input_replaced, history_modified) = (
                    handle_next_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['right'] and position < len(response):
                move_cursor('right')
                position += 1
            elif char == input_code['left'] and position > 0:
                move_cursor('left')
                position -= 1
            elif char == input_code['end']:
                move_cursor('right', len(response) - position)
                position = len(response)
            elif char == input_code['home']:
                move_cursor('left', position)
                position = 0
            elif char == input_code['del'][0]:
                if ''.join(input_code['del']) == '3~':
                    # *NIX uses '\x1b[3~' as its del key code, but only
                    # '\x1b[3' has currently been read from sys.stdin
                    getwch()
                backlog = response[position+1 :]
                response = response[:position] + backlog
                line_insert(backlog)
        elif char == input_code['backspace']:
            if position > 0:
                backlog = response[position:]
                response = response[: position-1] + backlog
                print('\b', end='', flush=True)
                position -= 1
                line_insert(backlog)
        elif char == input_code['^C']:
            raise KeyboardInterrupt
        elif char == input_code['^D']:
            raise EOFError
        elif char == input_code['^V']:
            paste = get_clipboard_data()
            backlog = response[position:]
            response = response[:position] + paste + backlog
            position += len(paste)
            line_insert(backlog, extra=paste)
        elif char == input_code['^Z']:
            sigtstp()
        elif char == input_code['enter']:
            finalise_history(history_index, response, replacements,
                             input_replaced, history_modified)
            move_cursor('right', len(response) - position)
            return response
        else:
            backlog = response[position:]
            response = response[:position] + char + backlog
            position += 1
            line_insert(backlog, extra=char)


def main():
    """Called if script isn't imported."""
    # "print(text, end='')" is equivalent to "print text,", and 'flush'
    # forces the text to appear, even if the line isn't terminated with
    # a '\n'
    print('Hello, ', end='', flush=True)
    name = input_no_newline()  # pylint: disable=unused-variable
    print(', how do you do?')


if __name__ == '__main__':
    main()
readline\u unavailable.py中

"""Windows-specific functions and variables for input_no_newline."""
import ctypes
from msvcrt import getwch  # pylint: disable=import-error, unused-import
from shared_stuff import ANSI

try:
    import colorama  # pylint: disable=import-error
except ImportError:
    kernel32 = ctypes.windll.kernel32
    # Enable ANSI support to move the text cursor
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
    colorama.init()


def get_clipboard_data():
    """Return string previously copied from Windows clipboard.

    Adapted from <http://stackoverflow.com/a/23285159/6379747>.

    """
    CF_TEXT = 1
    user32 = ctypes.windll.user32
    user32.OpenClipboard(0)
    try:
        if user32.IsClipboardFormatAvailable(CF_TEXT):
            data = user32.GetClipboardData(CF_TEXT)
            data_locked = kernel32.GlobalLock(data)
            text = ctypes.c_char_p(data_locked)
            kernel32.GlobalUnlock(data_locked)
    finally:
        user32.CloseClipboard()
    return text.value


def sigtstp():
    """Raise EOFError from Ctrl-Z since SIGTSTP doesn't exist on Windows."""
    raise EOFError


input_code = {
    **ANSI,
    'CSI': [['\xe0', '\x00'], ''],
    'up': 'H',
    'down': 'P',
    'right': 'M',
    'left': 'K',
    'end': 'O',
    'home': 'G',
    'backspace': '\b',
    'del': 'S',
}
"""Functions and variables for Debian-based Linux distros and macOS."""
import sys
import os
import tty
import signal
import termios
from shared_stuff import ANSI

def getwch():
    """Return a single character from user input without echoing.

    ActiveState code, adapted from
    <http://code.activestate.com/recipes/134892> by Danny Yoo under
    the Python Software Foundation license.

    """
    file_descriptor = sys.stdin.fileno()
    old_settings = termios.tcgetattr(file_descriptor)
    try:
        tty.setraw(file_descriptor)
        char = sys.stdin.read(1)
    finally:
        termios.tcsetattr(file_descriptor, termios.TCSADRAIN, old_settings)
    return char


def get_clipboard_data():
    """Return nothing; *NIX systems automagically change sys.stdin."""
    return ''


def sigtstp():
    """Suspend the script."""
    os.kill(os.getpid(), signal.SIGTSTP)


input_code = {
    **ANSI,
    'CSI': ['\x1b', '['],
    'backspace': '\x7f',
    'del': ['3', '~'],
}
"""Provide functions for up and down arrows if readline is installed.

Basically to prevent duplicate code and make it work on systems without
readline.

"""
try:
    import readline
except ImportError:
    import pyreadline as readline
from shared_stuff import move_cursor


def init_history_index():
    """Return index for last element of readline.get_history_item."""
    # readline.get_history_item is one-based
    return readline.get_current_history_length() + 1


def restore_history(history_index, replaced, cursor_position):
    """Replace 'replaced' with history and return the replacement."""
    try:
        replacement = readline.get_history_item(history_index)
    except IndexError:
        replacement = None
    if replacement is not None:
        move_cursor('right', len(replaced) - cursor_position)
        print('\b \b' * len(replaced), end='', flush=True)
        print(replacement, end='', flush=True)
        return replacement
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Store history and then replace it."""
    old_history[history_index] = readline.get_history_item(history_index)
    try:
        readline.replace_history_item(history_index - 1, replacement)
    except AttributeError:
    # pyreadline is incomplete
        pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some up-arrow logic."""
    try:
        history = readline.get_history_item(history_index - 1)
    except IndexError:
        history = None
    if history is not None:
        if history_index > readline.get_current_history_length():
            readline.add_history(replaced)
            input_replaced = True
        else:
            store_and_replace_history(
                history_index, replaced, old_history)
            history_modified = True
        history_index -= 1
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some down-arrow logic."""
    try:
        history = readline.get_history_item(history_index + 1)
    except IndexError:
        history = None
    if history is not None:
        store_and_replace_history(history_index, replaced, old_history)
        history_modified = True
        history_index += 1
        input_replaced = (not history_index
                            == readline.get_current_history_length())
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Change history before the response will be returned elsewhere."""
    try:
        if input_replaced:
            readline.remove_history_item(history_index - 1)
        elif history_modified:
            readline.remove_history_item(history_index - 1)
            readline.add_history(old_history[history_index - 1])
    except AttributeError:
    # pyreadline is also missing remove_history_item
        pass
    readline.add_history(response)
"""Provide dummy functions for if readline isn't available."""
# pylint: disable-msg=unused-argument


def init_history_index():
    """Return an index of 1 which probably won't ever change."""
    return 1


def restore_history(history_index, replaced, cursor_position):
    """Return the replaced thing without replacing it."""
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Don't store history."""
    pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Return 'input_replaced' and 'history_modified' without change."""
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Also return 'input_replaced' and 'history_modified'."""
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Don't change nonexistent history."""
    pass
"""Provide platform-independent functions and variables."""
ANSI = {
    'CSI': '\x1b[',
    'up': 'A',
    'down': 'B',
    'right': 'C',
    'left': 'D',
    'end': 'F',
    'home': 'H',
    'enter': '\r',
    '^C': '\x03',
    '^D': '\x04',
    '^V': '\x16',
    '^Z': '\x1a',
}


def move_cursor(direction, count=1):
    """Move the text cursor 'count' times in the specified direction."""
    if direction not in ['up', 'down', 'right', 'left']:
        raise ValueError("direction should be either 'up', 'down', 'right' "
                         "or 'left'")
    # A 'count' of zero still moves the cursor, so this needs to be
    # tested for.
    if count != 0:
        print(ANSI['CSI'] + str(count) + ANSI[direction], end='', flush=True)


def line_insert(text, extra=''):
    """Insert text between terminal line and reposition cursor."""
    if not extra:
    # It's not guaranteed that the new line will completely overshadow
    # the old one if there is no extra. Maybe something was 'deleted'?
        move_cursor('right', len(text) + 1)
        print('\b \b' * (len(text)+1), end='', flush=True)
    print(extra + text, end='', flush=True)
    move_cursor('left', len(text))
#!/usr/bin/python3
"""Provide an input function that doesn't echo a newline."""
try:
from windows_specific import getwch, get_clipboard_data, sigtstp, input_code
except ImportError:
    from unix_specific import getwch, get_clipboard_data, sigtstp, input_code
try:
    from readline_available import (init_history_index, restore_history,
                                    store_and_replace_history,
                                    handle_prev_history, handle_next_history,
                                    finalise_history)
except ImportError:
    from readline_unavailable import (init_history_index, restore_history,
                                      store_and_replace_history,
                                      handle_prev_history, handle_next_history,
                                      finalise_history)
from shared_stuff import ANSI, move_cursor, line_insert


def input_no_newline(prompt=''):  # pylint: disable=too-many-branches, too-many-statements
    """Echo and return user input, except for the newline."""
    print(prompt, end='', flush=True)
    response = ''
    position = 0
    history_index = init_history_index()
    input_replaced = False
    history_modified = False
    replacements = {}

    while True:
        char = getwch()
        if char in input_code['CSI'][0]:
            char = getwch()
            # Relevant input codes are made of two to four characters
            if char == input_code['CSI'][1]:
                # *NIX uses at least three characters, only the third is
                # important
                char = getwch()
            if char == input_code['up']:
                (history_index, input_replaced, history_modified) = (
                    handle_prev_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['down']:
                (history_index, input_replaced, history_modified) = (
                    handle_next_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['right'] and position < len(response):
                move_cursor('right')
                position += 1
            elif char == input_code['left'] and position > 0:
                move_cursor('left')
                position -= 1
            elif char == input_code['end']:
                move_cursor('right', len(response) - position)
                position = len(response)
            elif char == input_code['home']:
                move_cursor('left', position)
                position = 0
            elif char == input_code['del'][0]:
                if ''.join(input_code['del']) == '3~':
                    # *NIX uses '\x1b[3~' as its del key code, but only
                    # '\x1b[3' has currently been read from sys.stdin
                    getwch()
                backlog = response[position+1 :]
                response = response[:position] + backlog
                line_insert(backlog)
        elif char == input_code['backspace']:
            if position > 0:
                backlog = response[position:]
                response = response[: position-1] + backlog
                print('\b', end='', flush=True)
                position -= 1
                line_insert(backlog)
        elif char == input_code['^C']:
            raise KeyboardInterrupt
        elif char == input_code['^D']:
            raise EOFError
        elif char == input_code['^V']:
            paste = get_clipboard_data()
            backlog = response[position:]
            response = response[:position] + paste + backlog
            position += len(paste)
            line_insert(backlog, extra=paste)
        elif char == input_code['^Z']:
            sigtstp()
        elif char == input_code['enter']:
            finalise_history(history_index, response, replacements,
                             input_replaced, history_modified)
            move_cursor('right', len(response) - position)
            return response
        else:
            backlog = response[position:]
            response = response[:position] + char + backlog
            position += 1
            line_insert(backlog, extra=char)


def main():
    """Called if script isn't imported."""
    # "print(text, end='')" is equivalent to "print text,", and 'flush'
    # forces the text to appear, even if the line isn't terminated with
    # a '\n'
    print('Hello, ', end='', flush=True)
    name = input_no_newline()  # pylint: disable=unused-variable
    print(', how do you do?')


if __name__ == '__main__':
    main()
shared_stuff.py
中:

"""Windows-specific functions and variables for input_no_newline."""
import ctypes
from msvcrt import getwch  # pylint: disable=import-error, unused-import
from shared_stuff import ANSI

try:
    import colorama  # pylint: disable=import-error
except ImportError:
    kernel32 = ctypes.windll.kernel32
    # Enable ANSI support to move the text cursor
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
    colorama.init()


def get_clipboard_data():
    """Return string previously copied from Windows clipboard.

    Adapted from <http://stackoverflow.com/a/23285159/6379747>.

    """
    CF_TEXT = 1
    user32 = ctypes.windll.user32
    user32.OpenClipboard(0)
    try:
        if user32.IsClipboardFormatAvailable(CF_TEXT):
            data = user32.GetClipboardData(CF_TEXT)
            data_locked = kernel32.GlobalLock(data)
            text = ctypes.c_char_p(data_locked)
            kernel32.GlobalUnlock(data_locked)
    finally:
        user32.CloseClipboard()
    return text.value


def sigtstp():
    """Raise EOFError from Ctrl-Z since SIGTSTP doesn't exist on Windows."""
    raise EOFError


input_code = {
    **ANSI,
    'CSI': [['\xe0', '\x00'], ''],
    'up': 'H',
    'down': 'P',
    'right': 'M',
    'left': 'K',
    'end': 'O',
    'home': 'G',
    'backspace': '\b',
    'del': 'S',
}
"""Functions and variables for Debian-based Linux distros and macOS."""
import sys
import os
import tty
import signal
import termios
from shared_stuff import ANSI

def getwch():
    """Return a single character from user input without echoing.

    ActiveState code, adapted from
    <http://code.activestate.com/recipes/134892> by Danny Yoo under
    the Python Software Foundation license.

    """
    file_descriptor = sys.stdin.fileno()
    old_settings = termios.tcgetattr(file_descriptor)
    try:
        tty.setraw(file_descriptor)
        char = sys.stdin.read(1)
    finally:
        termios.tcsetattr(file_descriptor, termios.TCSADRAIN, old_settings)
    return char


def get_clipboard_data():
    """Return nothing; *NIX systems automagically change sys.stdin."""
    return ''


def sigtstp():
    """Suspend the script."""
    os.kill(os.getpid(), signal.SIGTSTP)


input_code = {
    **ANSI,
    'CSI': ['\x1b', '['],
    'backspace': '\x7f',
    'del': ['3', '~'],
}
"""Provide functions for up and down arrows if readline is installed.

Basically to prevent duplicate code and make it work on systems without
readline.

"""
try:
    import readline
except ImportError:
    import pyreadline as readline
from shared_stuff import move_cursor


def init_history_index():
    """Return index for last element of readline.get_history_item."""
    # readline.get_history_item is one-based
    return readline.get_current_history_length() + 1


def restore_history(history_index, replaced, cursor_position):
    """Replace 'replaced' with history and return the replacement."""
    try:
        replacement = readline.get_history_item(history_index)
    except IndexError:
        replacement = None
    if replacement is not None:
        move_cursor('right', len(replaced) - cursor_position)
        print('\b \b' * len(replaced), end='', flush=True)
        print(replacement, end='', flush=True)
        return replacement
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Store history and then replace it."""
    old_history[history_index] = readline.get_history_item(history_index)
    try:
        readline.replace_history_item(history_index - 1, replacement)
    except AttributeError:
    # pyreadline is incomplete
        pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some up-arrow logic."""
    try:
        history = readline.get_history_item(history_index - 1)
    except IndexError:
        history = None
    if history is not None:
        if history_index > readline.get_current_history_length():
            readline.add_history(replaced)
            input_replaced = True
        else:
            store_and_replace_history(
                history_index, replaced, old_history)
            history_modified = True
        history_index -= 1
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some down-arrow logic."""
    try:
        history = readline.get_history_item(history_index + 1)
    except IndexError:
        history = None
    if history is not None:
        store_and_replace_history(history_index, replaced, old_history)
        history_modified = True
        history_index += 1
        input_replaced = (not history_index
                            == readline.get_current_history_length())
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Change history before the response will be returned elsewhere."""
    try:
        if input_replaced:
            readline.remove_history_item(history_index - 1)
        elif history_modified:
            readline.remove_history_item(history_index - 1)
            readline.add_history(old_history[history_index - 1])
    except AttributeError:
    # pyreadline is also missing remove_history_item
        pass
    readline.add_history(response)
"""Provide dummy functions for if readline isn't available."""
# pylint: disable-msg=unused-argument


def init_history_index():
    """Return an index of 1 which probably won't ever change."""
    return 1


def restore_history(history_index, replaced, cursor_position):
    """Return the replaced thing without replacing it."""
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Don't store history."""
    pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Return 'input_replaced' and 'history_modified' without change."""
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Also return 'input_replaced' and 'history_modified'."""
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Don't change nonexistent history."""
    pass
"""Provide platform-independent functions and variables."""
ANSI = {
    'CSI': '\x1b[',
    'up': 'A',
    'down': 'B',
    'right': 'C',
    'left': 'D',
    'end': 'F',
    'home': 'H',
    'enter': '\r',
    '^C': '\x03',
    '^D': '\x04',
    '^V': '\x16',
    '^Z': '\x1a',
}


def move_cursor(direction, count=1):
    """Move the text cursor 'count' times in the specified direction."""
    if direction not in ['up', 'down', 'right', 'left']:
        raise ValueError("direction should be either 'up', 'down', 'right' "
                         "or 'left'")
    # A 'count' of zero still moves the cursor, so this needs to be
    # tested for.
    if count != 0:
        print(ANSI['CSI'] + str(count) + ANSI[direction], end='', flush=True)


def line_insert(text, extra=''):
    """Insert text between terminal line and reposition cursor."""
    if not extra:
    # It's not guaranteed that the new line will completely overshadow
    # the old one if there is no extra. Maybe something was 'deleted'?
        move_cursor('right', len(text) + 1)
        print('\b \b' * (len(text)+1), end='', flush=True)
    print(extra + text, end='', flush=True)
    move_cursor('left', len(text))
#!/usr/bin/python3
"""Provide an input function that doesn't echo a newline."""
try:
from windows_specific import getwch, get_clipboard_data, sigtstp, input_code
except ImportError:
    from unix_specific import getwch, get_clipboard_data, sigtstp, input_code
try:
    from readline_available import (init_history_index, restore_history,
                                    store_and_replace_history,
                                    handle_prev_history, handle_next_history,
                                    finalise_history)
except ImportError:
    from readline_unavailable import (init_history_index, restore_history,
                                      store_and_replace_history,
                                      handle_prev_history, handle_next_history,
                                      finalise_history)
from shared_stuff import ANSI, move_cursor, line_insert


def input_no_newline(prompt=''):  # pylint: disable=too-many-branches, too-many-statements
    """Echo and return user input, except for the newline."""
    print(prompt, end='', flush=True)
    response = ''
    position = 0
    history_index = init_history_index()
    input_replaced = False
    history_modified = False
    replacements = {}

    while True:
        char = getwch()
        if char in input_code['CSI'][0]:
            char = getwch()
            # Relevant input codes are made of two to four characters
            if char == input_code['CSI'][1]:
                # *NIX uses at least three characters, only the third is
                # important
                char = getwch()
            if char == input_code['up']:
                (history_index, input_replaced, history_modified) = (
                    handle_prev_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['down']:
                (history_index, input_replaced, history_modified) = (
                    handle_next_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['right'] and position < len(response):
                move_cursor('right')
                position += 1
            elif char == input_code['left'] and position > 0:
                move_cursor('left')
                position -= 1
            elif char == input_code['end']:
                move_cursor('right', len(response) - position)
                position = len(response)
            elif char == input_code['home']:
                move_cursor('left', position)
                position = 0
            elif char == input_code['del'][0]:
                if ''.join(input_code['del']) == '3~':
                    # *NIX uses '\x1b[3~' as its del key code, but only
                    # '\x1b[3' has currently been read from sys.stdin
                    getwch()
                backlog = response[position+1 :]
                response = response[:position] + backlog
                line_insert(backlog)
        elif char == input_code['backspace']:
            if position > 0:
                backlog = response[position:]
                response = response[: position-1] + backlog
                print('\b', end='', flush=True)
                position -= 1
                line_insert(backlog)
        elif char == input_code['^C']:
            raise KeyboardInterrupt
        elif char == input_code['^D']:
            raise EOFError
        elif char == input_code['^V']:
            paste = get_clipboard_data()
            backlog = response[position:]
            response = response[:position] + paste + backlog
            position += len(paste)
            line_insert(backlog, extra=paste)
        elif char == input_code['^Z']:
            sigtstp()
        elif char == input_code['enter']:
            finalise_history(history_index, response, replacements,
                             input_replaced, history_modified)
            move_cursor('right', len(response) - position)
            return response
        else:
            backlog = response[position:]
            response = response[:position] + char + backlog
            position += 1
            line_insert(backlog, extra=char)


def main():
    """Called if script isn't imported."""
    # "print(text, end='')" is equivalent to "print text,", and 'flush'
    # forces the text to appear, even if the line isn't terminated with
    # a '\n'
    print('Hello, ', end='', flush=True)
    name = input_no_newline()  # pylint: disable=unused-variable
    print(', how do you do?')


if __name__ == '__main__':
    main()
最后,在
input\u no\u newline.py中:

"""Windows-specific functions and variables for input_no_newline."""
import ctypes
from msvcrt import getwch  # pylint: disable=import-error, unused-import
from shared_stuff import ANSI

try:
    import colorama  # pylint: disable=import-error
except ImportError:
    kernel32 = ctypes.windll.kernel32
    # Enable ANSI support to move the text cursor
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
    colorama.init()


def get_clipboard_data():
    """Return string previously copied from Windows clipboard.

    Adapted from <http://stackoverflow.com/a/23285159/6379747>.

    """
    CF_TEXT = 1
    user32 = ctypes.windll.user32
    user32.OpenClipboard(0)
    try:
        if user32.IsClipboardFormatAvailable(CF_TEXT):
            data = user32.GetClipboardData(CF_TEXT)
            data_locked = kernel32.GlobalLock(data)
            text = ctypes.c_char_p(data_locked)
            kernel32.GlobalUnlock(data_locked)
    finally:
        user32.CloseClipboard()
    return text.value


def sigtstp():
    """Raise EOFError from Ctrl-Z since SIGTSTP doesn't exist on Windows."""
    raise EOFError


input_code = {
    **ANSI,
    'CSI': [['\xe0', '\x00'], ''],
    'up': 'H',
    'down': 'P',
    'right': 'M',
    'left': 'K',
    'end': 'O',
    'home': 'G',
    'backspace': '\b',
    'del': 'S',
}
"""Functions and variables for Debian-based Linux distros and macOS."""
import sys
import os
import tty
import signal
import termios
from shared_stuff import ANSI

def getwch():
    """Return a single character from user input without echoing.

    ActiveState code, adapted from
    <http://code.activestate.com/recipes/134892> by Danny Yoo under
    the Python Software Foundation license.

    """
    file_descriptor = sys.stdin.fileno()
    old_settings = termios.tcgetattr(file_descriptor)
    try:
        tty.setraw(file_descriptor)
        char = sys.stdin.read(1)
    finally:
        termios.tcsetattr(file_descriptor, termios.TCSADRAIN, old_settings)
    return char


def get_clipboard_data():
    """Return nothing; *NIX systems automagically change sys.stdin."""
    return ''


def sigtstp():
    """Suspend the script."""
    os.kill(os.getpid(), signal.SIGTSTP)


input_code = {
    **ANSI,
    'CSI': ['\x1b', '['],
    'backspace': '\x7f',
    'del': ['3', '~'],
}
"""Provide functions for up and down arrows if readline is installed.

Basically to prevent duplicate code and make it work on systems without
readline.

"""
try:
    import readline
except ImportError:
    import pyreadline as readline
from shared_stuff import move_cursor


def init_history_index():
    """Return index for last element of readline.get_history_item."""
    # readline.get_history_item is one-based
    return readline.get_current_history_length() + 1


def restore_history(history_index, replaced, cursor_position):
    """Replace 'replaced' with history and return the replacement."""
    try:
        replacement = readline.get_history_item(history_index)
    except IndexError:
        replacement = None
    if replacement is not None:
        move_cursor('right', len(replaced) - cursor_position)
        print('\b \b' * len(replaced), end='', flush=True)
        print(replacement, end='', flush=True)
        return replacement
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Store history and then replace it."""
    old_history[history_index] = readline.get_history_item(history_index)
    try:
        readline.replace_history_item(history_index - 1, replacement)
    except AttributeError:
    # pyreadline is incomplete
        pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some up-arrow logic."""
    try:
        history = readline.get_history_item(history_index - 1)
    except IndexError:
        history = None
    if history is not None:
        if history_index > readline.get_current_history_length():
            readline.add_history(replaced)
            input_replaced = True
        else:
            store_and_replace_history(
                history_index, replaced, old_history)
            history_modified = True
        history_index -= 1
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Handle some down-arrow logic."""
    try:
        history = readline.get_history_item(history_index + 1)
    except IndexError:
        history = None
    if history is not None:
        store_and_replace_history(history_index, replaced, old_history)
        history_modified = True
        history_index += 1
        input_replaced = (not history_index
                            == readline.get_current_history_length())
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Change history before the response will be returned elsewhere."""
    try:
        if input_replaced:
            readline.remove_history_item(history_index - 1)
        elif history_modified:
            readline.remove_history_item(history_index - 1)
            readline.add_history(old_history[history_index - 1])
    except AttributeError:
    # pyreadline is also missing remove_history_item
        pass
    readline.add_history(response)
"""Provide dummy functions for if readline isn't available."""
# pylint: disable-msg=unused-argument


def init_history_index():
    """Return an index of 1 which probably won't ever change."""
    return 1


def restore_history(history_index, replaced, cursor_position):
    """Return the replaced thing without replacing it."""
    return replaced


def store_and_replace_history(history_index, replacement, old_history):
    """Don't store history."""
    pass


def handle_prev_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Return 'input_replaced' and 'history_modified' without change."""
    return (history_index, input_replaced, history_modified)


def handle_next_history(history_index, replaced, old_history,
                        input_replaced, history_modified):
    """Also return 'input_replaced' and 'history_modified'."""
    return (history_index, input_replaced, history_modified)


def finalise_history(history_index, response, old_history,
                     input_replaced, history_modified):
    """Don't change nonexistent history."""
    pass
"""Provide platform-independent functions and variables."""
ANSI = {
    'CSI': '\x1b[',
    'up': 'A',
    'down': 'B',
    'right': 'C',
    'left': 'D',
    'end': 'F',
    'home': 'H',
    'enter': '\r',
    '^C': '\x03',
    '^D': '\x04',
    '^V': '\x16',
    '^Z': '\x1a',
}


def move_cursor(direction, count=1):
    """Move the text cursor 'count' times in the specified direction."""
    if direction not in ['up', 'down', 'right', 'left']:
        raise ValueError("direction should be either 'up', 'down', 'right' "
                         "or 'left'")
    # A 'count' of zero still moves the cursor, so this needs to be
    # tested for.
    if count != 0:
        print(ANSI['CSI'] + str(count) + ANSI[direction], end='', flush=True)


def line_insert(text, extra=''):
    """Insert text between terminal line and reposition cursor."""
    if not extra:
    # It's not guaranteed that the new line will completely overshadow
    # the old one if there is no extra. Maybe something was 'deleted'?
        move_cursor('right', len(text) + 1)
        print('\b \b' * (len(text)+1), end='', flush=True)
    print(extra + text, end='', flush=True)
    move_cursor('left', len(text))
#!/usr/bin/python3
"""Provide an input function that doesn't echo a newline."""
try:
from windows_specific import getwch, get_clipboard_data, sigtstp, input_code
except ImportError:
    from unix_specific import getwch, get_clipboard_data, sigtstp, input_code
try:
    from readline_available import (init_history_index, restore_history,
                                    store_and_replace_history,
                                    handle_prev_history, handle_next_history,
                                    finalise_history)
except ImportError:
    from readline_unavailable import (init_history_index, restore_history,
                                      store_and_replace_history,
                                      handle_prev_history, handle_next_history,
                                      finalise_history)
from shared_stuff import ANSI, move_cursor, line_insert


def input_no_newline(prompt=''):  # pylint: disable=too-many-branches, too-many-statements
    """Echo and return user input, except for the newline."""
    print(prompt, end='', flush=True)
    response = ''
    position = 0
    history_index = init_history_index()
    input_replaced = False
    history_modified = False
    replacements = {}

    while True:
        char = getwch()
        if char in input_code['CSI'][0]:
            char = getwch()
            # Relevant input codes are made of two to four characters
            if char == input_code['CSI'][1]:
                # *NIX uses at least three characters, only the third is
                # important
                char = getwch()
            if char == input_code['up']:
                (history_index, input_replaced, history_modified) = (
                    handle_prev_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['down']:
                (history_index, input_replaced, history_modified) = (
                    handle_next_history(
                        history_index, response, replacements, input_replaced,
                        history_modified))
                response = restore_history(history_index, response, position)
                position = len(response)
            elif char == input_code['right'] and position < len(response):
                move_cursor('right')
                position += 1
            elif char == input_code['left'] and position > 0:
                move_cursor('left')
                position -= 1
            elif char == input_code['end']:
                move_cursor('right', len(response) - position)
                position = len(response)
            elif char == input_code['home']:
                move_cursor('left', position)
                position = 0
            elif char == input_code['del'][0]:
                if ''.join(input_code['del']) == '3~':
                    # *NIX uses '\x1b[3~' as its del key code, but only
                    # '\x1b[3' has currently been read from sys.stdin
                    getwch()
                backlog = response[position+1 :]
                response = response[:position] + backlog
                line_insert(backlog)
        elif char == input_code['backspace']:
            if position > 0:
                backlog = response[position:]
                response = response[: position-1] + backlog
                print('\b', end='', flush=True)
                position -= 1
                line_insert(backlog)
        elif char == input_code['^C']:
            raise KeyboardInterrupt
        elif char == input_code['^D']:
            raise EOFError
        elif char == input_code['^V']:
            paste = get_clipboard_data()
            backlog = response[position:]
            response = response[:position] + paste + backlog
            position += len(paste)
            line_insert(backlog, extra=paste)
        elif char == input_code['^Z']:
            sigtstp()
        elif char == input_code['enter']:
            finalise_history(history_index, response, replacements,
                             input_replaced, history_modified)
            move_cursor('right', len(response) - position)
            return response
        else:
            backlog = response[position:]
            response = response[:position] + char + backlog
            position += 1
            line_insert(backlog, extra=char)


def main():
    """Called if script isn't imported."""
    # "print(text, end='')" is equivalent to "print text,", and 'flush'
    # forces the text to appear, even if the line isn't terminated with
    # a '\n'
    print('Hello, ', end='', flush=True)
    name = input_no_newline()  # pylint: disable=unused-variable
    print(', how do you do?')


if __name__ == '__main__':
    main()
!/usr/bin/python3
“”“提供一个不回显换行符的输入函数。”“”
尝试:
从特定于windows的导入getwch,获取剪贴板数据,sigtstp,输入代码
除恐怖外:
从unix_特定导入getwch,获取_剪贴板_数据,sigtstp,输入_代码
尝试:
从readline\u可用导入(初始化\u历史记录\u索引、还原\u历史记录、,
存储和替换历史记录,
处理上一个历史记录,处理下一个历史记录,
定稿(历史)
除恐怖外:
从readline\u不可用导入(初始化\u历史记录\u索引、还原\u历史记录、,
存储和替换历史记录,
处理上一个历史记录,处理下一个历史记录,
定稿(历史)
从共享输入ANSI,移动光标,插入行
def input_no_新行(提示符=“”):#pylint:disable=分支太多,语句太多
“”“回显并返回用户输入,换行除外。”“”
打印(提示,结束=“”,刷新=真)
响应=“”
位置=0
历史索引=初始历史索引()
输入被替换=错误
修改历史记录=错误
替换={}
尽管如此:
char=getwch()
如果输入_代码['CSI'][0]中有字符:
char=getwch()
#相关输入代码由两到四个字符组成
如果char==输入_代码['CSI'][1]:
#*NIX至少使用三个字符