我正在运行Ubuntu 20.4 w/Python 3.8.2,在Python中的readline接口遇到了一个奇怪的行为。
我的目标是提供一种输入工具,允许在终端上的给定位置从给定字符串列表中进行选择,或者简单地输入所需的字符串。我的代码确实可以(几乎(工作,直到由于某些字符串的原因,结果被破坏。
我的代码看起来像这个
import readline
class __readlineCompleter(object):
"""
Cycles through a list of potential strings the user might want to enter
"""
def __init__(self, options):
self.options = list(options) #sorted([item for item in options])
self.index = 0
return
def complete(self, text, state):
if state == 0:
if text and not text in self.options:
self.options.append(text)
self.index += 1
if self.index >= len(self.options):
self.index = 0
return self.options[self.index]
else:
return None
def input_at(x_pos, y_pos, _text, _prefill, _choices):
"""
positions the cursor at x_pos, y_pos, prints text if given and reads a string from stdin
@param x_pos(int): row to print at
@param y_pos(int): columns to print at
@param _text(string): text to print before the input field
@param _prefill(string): text as default input
@param _choices(list): list of strings as proposed input values
@return: (string): input given by user, or False
"""
_myPrompt = "