windows 命令行中的漂亮打印
Pretty printing in windows command line
我正在尝试使用库 Clint 在 windows 命令行中为我的输入语句添加颜色。
from clint.textui import colored, puts,prompt
puts(colored.yellow("Hello!!!\n\n"))
user_number = prompt.query(str(colored.cyan('\nPlease enter something:\n')))
我不明白为什么没有显示青色。附件是来自命令行的视图。谢谢!
这不起作用的原因是 prompt.py 将您的输入转换为原始字符串,因此无法识别您选择的颜色。我认为从他们的角度来看这是一个糟糕的实施。你可以通过 运行
来证明我的理论
user_number = prompt.query(str(colored.cyan('\nPlease enter something:\n')),batch=True)
这是一个无限循环,但它会打印你的颜色只是因为它没有在 case.
中转换为原始颜色
这里是来自 clint lib
的文件 prompt.py 中查询函数的定义
def query(prompt, default='', validators=None, batch=False):
# Set the nonempty validator as default
if validators is None:
validators = [RegexValidator(r'.+')]
# Let's build the prompt
if prompt[-1] is not ' ':
prompt += ' '
if default:
prompt += '[' + default + '] '
# If input is not valid keep asking
while True:
# If batch option is True then auto reply
# with default input
if not batch:
user_input = raw_input(prompt).strip() or default
else:
print(prompt)
user_input = ''
# Validate the user input
try:
for validator in validators:
user_input = validator(user_input)
return user_input
except Exception as e:
puts(yellow(e.message))
你可以看到你不能像你想要的那样打印彩色文本,因为它期望
一个文字字符串作为参数,我们可以像这样改变查询函数以按预期工作
from clint.textui import colored, puts, prompt, validators as validators_module
from re import match
prompt_colors = {
"red": colored.red, "green": colored.green, "yellow": colored.yellow, "blue": colored.blue,
"black": colored.black, "magenta": colored.magenta, "cyan": colored.cyan, "white": colored.white
}
def new_query(prompt, color, default='', validators=None, batch=False):
# Set the nonempty validator as default
if validators is None:
validators = [validators_module.RegexValidator(r'.+')]
# Let's build the prompt
if prompt[-1] is not ' ':
prompt += ' '
if default:
prompt += '[' + default + '] '
# If input is not valid keep asking
while True:
# If batch option is True then auto reply
# with default input
if not batch:
# now the output is colored as expected
puts(prompt_colors[color](prompt))
user_input = input().strip() or default
else:
print(prompt)
user_input = ''
# Validate the user input
try:
for validator in validators:
user_input = validator(user_input)
return user_input
except Exception as e:
puts(yellow(e.message))
# now the query function is customized as per our needs
prompt.query = new_query
puts(colored.yellow("Hello!!!\n\n"))
user_input = prompt.query("Please enter something: ", "cyan")
注:我觉得变化已经很清楚了,不需要解释,但是如果你有什么问题,我很乐意在评论部分回答
我正在尝试使用库 Clint 在 windows 命令行中为我的输入语句添加颜色。
from clint.textui import colored, puts,prompt
puts(colored.yellow("Hello!!!\n\n"))
user_number = prompt.query(str(colored.cyan('\nPlease enter something:\n')))
我不明白为什么没有显示青色。附件是来自命令行的视图。谢谢!
这不起作用的原因是 prompt.py 将您的输入转换为原始字符串,因此无法识别您选择的颜色。我认为从他们的角度来看这是一个糟糕的实施。你可以通过 运行
来证明我的理论
user_number = prompt.query(str(colored.cyan('\nPlease enter something:\n')),batch=True)
这是一个无限循环,但它会打印你的颜色只是因为它没有在 case.
这里是来自 clint lib
的文件 prompt.py 中查询函数的定义def query(prompt, default='', validators=None, batch=False):
# Set the nonempty validator as default
if validators is None:
validators = [RegexValidator(r'.+')]
# Let's build the prompt
if prompt[-1] is not ' ':
prompt += ' '
if default:
prompt += '[' + default + '] '
# If input is not valid keep asking
while True:
# If batch option is True then auto reply
# with default input
if not batch:
user_input = raw_input(prompt).strip() or default
else:
print(prompt)
user_input = ''
# Validate the user input
try:
for validator in validators:
user_input = validator(user_input)
return user_input
except Exception as e:
puts(yellow(e.message))
你可以看到你不能像你想要的那样打印彩色文本,因为它期望 一个文字字符串作为参数,我们可以像这样改变查询函数以按预期工作
from clint.textui import colored, puts, prompt, validators as validators_module
from re import match
prompt_colors = {
"red": colored.red, "green": colored.green, "yellow": colored.yellow, "blue": colored.blue,
"black": colored.black, "magenta": colored.magenta, "cyan": colored.cyan, "white": colored.white
}
def new_query(prompt, color, default='', validators=None, batch=False):
# Set the nonempty validator as default
if validators is None:
validators = [validators_module.RegexValidator(r'.+')]
# Let's build the prompt
if prompt[-1] is not ' ':
prompt += ' '
if default:
prompt += '[' + default + '] '
# If input is not valid keep asking
while True:
# If batch option is True then auto reply
# with default input
if not batch:
# now the output is colored as expected
puts(prompt_colors[color](prompt))
user_input = input().strip() or default
else:
print(prompt)
user_input = ''
# Validate the user input
try:
for validator in validators:
user_input = validator(user_input)
return user_input
except Exception as e:
puts(yellow(e.message))
# now the query function is customized as per our needs
prompt.query = new_query
puts(colored.yellow("Hello!!!\n\n"))
user_input = prompt.query("Please enter something: ", "cyan")
注:我觉得变化已经很清楚了,不需要解释,但是如果你有什么问题,我很乐意在评论部分回答