我正在研究一个Python脚本,有时需要用户输入输入。我在macOS Sierra上使用bash。
为了接收输入,我写了以下内容:
import readline # gnureadline 6.3.8
START = '\033[91m\033[1m'
END = '\033[0m'
response = raw_input(START + 'Enter text: ' + END)
我在START
和END
中使用ANSI转义序列来直观地区分脚本的提示和用户的文本条目。
不幸的是,一旦我开始输入文本,程序就会丢失用户输入文本的开头实际所在的位置。下面的第一个克拉是CTRLA现在映射到的地方,第二个是CTRLE带我的地方。箭头键同样相信用户输入的开始和结束是偏移的,如图所示。
Enter text: hello my name is
^ ^
我尝试了一些东西来调试这个。
START
和END
,这种方法非常有效。也就是说,脚本的可用性下降了 - 很难从该脚本的完整版本中找到所有其他文本中的提示。我是否有一些技巧可以使用ANSI转义序列格式化提示,而不会破坏用户使用CTRLA,CTRLE和选项箭头导航的能力?谢谢!
单独打印提示并要求输入两个命令。
Python等
print(START + 'Enter text: ' + END, end='')
response = input()
Python 2.x
import sys
# Use sys.stdout.write to avoid printing a trailing newline.
sys.stdout.write(START + 'Enter text: ' + END)
response = raw_input()
在我的情况下,接受的答案没有成功。
我开始了
START = '\x1b[7m\x1b[37m' # white on black
END = '\x1b[0m' # reset
response = input(START + 'QUERY ' + END)
这同样导致了
QUERY hello my name is hello my name is
^ ^
hello my name is hello my name is
使用上面的解决方案,
print(START + 'QUERY ' + END, end='')
response = input()
导致它考虑从一开始就开始的线
QUERY hello my name is hello my name is
^ ^
hello my name is hello my name is
可能是因为input("")
长0个字符。
知道那些控制字符被计为偏移量,我们可以再次使用它们来创建偏移量。
我的文字QUERY
长6个字符,所以我可以使用
print(START + 'QUERY ' + END, end='')
response = input('\033[D\033[C')
因为Cursor Forward, Cursor Back '\033', '[', 'D'
,'\033', '[', 'C'
欺骗readline
抵消6个字符。
QUERY hello my name is hello my name is
\[D\[C^ ^
hello my name is hello my name is
Readline期望\001
和\002
作为隐形字符周围的标记:
import readline # gnureadline 6.3.8
START = '\001\033[91m\033[1m\002'
END = '\001\033[0m\002'
response = raw_input(START + 'Enter text: ' + END)
对于ANSI转义序列,前缀为\001
,后缀为\002
,GNU readline将知道在确定提示字符串的长度时应排除这些字符。