在Windows命令行中漂亮打印

时间:2020-07-10 18:44:31

标签: python clint

我正在尝试使用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')))

我不知道为什么青色没有出现。附件是命令行视图。谢谢!

enter image description here

2 个答案:

答案 0 :(得分:0)

之所以不起作用,是因为prompt.py将您的输入转换为原始字符串,因此无法识别您选择的颜色。我认为这对他们来说是一个糟糕的实现。你可以通过运行来证明我的理论
user_number = prompt.query(str(colored.cyan('\nPlease enter something:\n')),batch=True)
这是一个无限循环,但它会打印出您的颜色,只是因为它没有在此case中转换为原始颜色。

答案 1 :(得分:0)

这是来自clint lib的文件hint.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")

enter image description here

注意::我认为所做的更改已经足够清楚,不需要解释,但是如果您有任何疑问,我将很乐意在评论部分予以答复