tartley / colorama

Simple cross-platform colored terminal text in Python
BSD 3-Clause "New" or "Revised" License
3.52k stars 250 forks source link

colorama not working with input(...) #103

Open Zitrax opened 8 years ago

Zitrax commented 8 years ago

System: Windows 7 Shell: cmd.exe Python: 3.5.1 Colorama: Current master (69e4069)

For example:

from colorama import Fore, init
init()
input(Fore.RED + "test")

This prints ←[31mtest instead of of "test" in red.

There is a similar question on Stack Overflow without any good answer: http://stackoverflow.com/q/32872612/11722

And I can verify that this worked in Python 2.7.

wiggin15 commented 8 years ago

Looks like it's broken in Python 3.5, probably related to this change: http://bugs.python.org/issue24402 The input function now doesn't print to the wrapped stdout because it's different than the original. It's a little ironic because it sounds like the Python issue was supposed to work exactly for our case, and now it doesn't unless we add this method to StreamWrapper:

    def fileno():
        raise OSError()
ghost commented 7 years ago

So guys, any quick workarounds? @wiggin15

wiggin15 commented 7 years ago

Hi @al3xv3gas . I pushed the workaround with the 'fileno' method, and submitted a new bug in the Python issue tracker: http://bugs.python.org/issue28373 Please try the latest version in this repository and let me know if it works now.

ghost commented 7 years ago

I'm in achool at the moment, but I'll tell you today!:) @wiggin15

ghost commented 7 years ago

Okay feedback: I tried it with Python 3.6 and it actually worked! That is amazing! @wiggin15

ghost commented 7 years ago

I'd suggest using the terminal instead of IDLE or another editor. IDLE (3.5.2) didn't work for me - macOS terminal did.

tartley commented 7 years ago

At the risk of being somewhat off-topic, I just completed a transition from GUI app gVim to NeoVim in a terminal, I can't tell you how happy it has made me.

wiggin15 commented 7 years ago

The fix causes issues for dependent applications - see #130. I'm reverting the fix and reopening this issue. We'll find a different fix.

juliedwhite commented 6 years ago

This workaround is a little clumsy but prints "test" in red. Works on Windows 10 and Linux.

from colorama import Fore, init
init()
print(Fore.Red + "")
input("test")
heXaCo0l commented 5 years ago

print(Fore.Red + "") Thank you , That's work with me Windows 7 print("\033[1m" +"") input("test")

Appreciativeness commented 5 years ago

This issue is only with the default Command Line provided with windows. Tested OS: Windows 10 Home, Build 17134 Tested Python Version: 3.7.0

Using input(Fore.CYAN + Style.BRIGHT + "Input State: ") on Command Line

image If you really need to use that one, use the workaround posted by @juliedwhite

Using input(Fore.CYAN + Style.BRIGHT + "Input State: ") on Cmder for Windows

image Another benefit to using Cmder is that it does not require you to use init() or deinit() due to native colour support.

platomav commented 4 years ago

The easiest/cleanest thing to do as a workaround is to replace any input(message) calls with a custom function that simply prints the message (which we know works) and then pauses via a regular input() like so:

def input_colorama(message) :
    print(message, end = '')
    input()

The function above has the exact same output and behavior as regular input(message) would.

antbogomolov commented 3 years ago

This workaround is a little clumsy but prints "test" in red. Works on Windows 10 and Linux.

from colorama import Fore, init
init()
print(Fore.Red + "")
input("test")

Doesn't work on my win7 and python 3.8

tartley commented 3 years ago

@antbogomolov Hi there, thanks for the comment. Did you try the refinement of that workaround suggested by @platomav, above?

CyberGeneticist commented 3 years ago

Hi. Colorama is really nice. Found a bit of a niche issue playing around recently:

In Python 3.9.4 using colorama 0.4.4 input still causes problems, namely the same issue as described above happens when using multiline f-strings as the prompt parameter for input. For example:

import colorama

colorama.init()
prompt = f"""
this is a 
multiline f-string and it 
{colorama.Fore.BLACK}{colorama.Back.WHITE}causes problems{colorama.Style.RESET_ALL}
"""

input(prompt)

The above does not format correctly in some places - inside PyCharm's console it works great, however inside Windows 10's command line it breaks (the same way as shown above previously). The same prompt used with print works fine everywhere. Thanks.

Dr-Aniket commented 3 years ago

To get rid of this problem in the starting of the code add

**import os

os.system('cls')**

This will clear the screen and hence clear all the external factors blocking the colorama in input. This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.

I hope this will help all the creative minds trying to make their codes more colourful

tartley commented 2 years ago

@CyberGeneticist Try reformulating your code with platomav's suggestion slightly above your own. ie. print the prompt using 'print', then call 'input' with no arguments. I expect that should work.

MarceloA7 commented 1 year ago

Essa solução alternativa é um pouco desajeitada, mas imprime "teste" em vermelho. Funciona no Windows 10 e Linux.

from colorama import Fore, init
init()
print(Fore.Red + "")
input("test")

Thanks