Search code examples
pythonclint

Pretty printing in windows command line


I am trying to add colors to my input statement in the windows command line using the library Clint.

from clint.textui import colored, puts,prompt

puts(colored.yellow("Hello!!!\n\n"))
user_number = prompt.query(str(colored.cyan('\nPlease enter something:\n')))

I can't figure out why the cyan color not showing up. Attached is the view from the command line. Thanks!

enter image description here


Solution

  • Here is the definition of the query function in the file prompt.py from the clint lib

    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))
    

    you can see that you can't print a colorful text like you are trying to achieve, since it expects a literal string as an argument, we can alter the query function to work as expected like this

    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

    Note: I think the changes are clear enough and do not need explanation, but if you have some questions I would be glad to answer'em in the comments section