2

I have yet another question regarding my little console program for the windows cmd.

I use colorama to color my text in the terminal which makes it look like this:

enter image description here

Then I found out how to color the text in an input()-method using a little "hack" with a print without linebreak, like this:

from colorama import init
init(autoreset=True)
YELLOW = "\x1b[1;33;40m" 

print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')
tournament = input()

this leads to the yellow line in the picture above.

But I still look for a way to color the very characters the user inputs - so here I would like to format the user input "sdffdgf..." in color as well.

enter image description here

has anyone a solution for me or will it just not be possible with the limited windows cmd?

Cut7er
  • 1,209
  • 9
  • 24

2 Answers2

2

Removing the init(autoreset=True) line from your code runs as you wish on my machine.

import colorama

from colorama import Fore,Style,Back
colorama.init()

YELLOW = "\x1b[1;33;40m" 
RED = "\x1b[1;31;40m"

print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')
tournament = input()
print(f"\n{RED}Turnier spielen? [T]: ", end='')
tournament2 = input()

code together with output to see it working

My colorama version colorama==0.3.9.

The Colorama docs state that when using autoreset=true it will reset your colour and styling options immediately after the print command, this happens before you get to your input command which is why you do not get the colours in the user typed text.

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
  • wow, thanks a lot for this late yet gold answer! I never suspected it to be caused by the autoreset....looks like I have to `{CLEAR}` after every line, then I can omit the `autoreset=True` altogether and finally get my colored inputs :-)! – Cut7er Oct 10 '18 at 13:16
  • @Cut7er you might even benefit of creating your own print function instead of adding the `{CLEAR}` to every line `print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')`, you could pass the desired string and colour as parameters and have the print statement in the function itself. That's personally how I would do it – WhatsThePoint Oct 10 '18 at 13:25
  • definitely an idea worth looking into! thank you very much :-) – Cut7er Oct 10 '18 at 14:15
1

https://docs.python.org/3/library/functions.html#input

You can pass input() a string to display before the actual input of the user.

from colorama import init
init(autoreset=True)
YELLOW = "\x1b[1;33;40m"
RED = "\x1b[1;31;40m"

print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')
tournament = input(RED)

You can probably get rid of the print(..., end='') call with this.

Ph3n0x
  • 306
  • 2
  • 5
  • that particular idea came to mind very soon, and was answered in my last question on SO - https://stackoverflow.com/questions/52102240/how-to-apply-coloring-formatting-to-the-displayed-text-in-input-function-simi - but since `print()` and `input()` do not use the same underlying mechanics, it does not work. So if I insert the {RED} into `input(f"{RED}")`, it will just display the ANSI string and will not apply any formatting to the inputted words. – Cut7er Sep 27 '18 at 11:55
  • 1
    How about turning off `autoreset` and doing this : `print(f"\n{YELLOW}Turnier spielen? [T]: {RED}", end='')` ? – Ph3n0x Sep 27 '18 at 12:10
  • sorry, not working - without autoreset, nothing is colored anymore. And I tried basically anything with placing the ANSI sequences before, after the text, inside print, input... there needs to be something completely different which I'm missing :| – Cut7er Sep 27 '18 at 12:16