gleitz / howdoi

instant coding answers via the command line
http://blog.gleitzman.com/post/43330157197/howdoi-instant-coding-answers-via-the-command-line
MIT License
10.56k stars 867 forks source link

Cant Print out the colors in Powershell Or CMD Terminal #445

Closed SepehrRasouli closed 2 years ago

SepehrRasouli commented 2 years ago

What happened:

howdoi cant print out colors in Powershell Or CMD terminal in color mode.

> howdoi -c how to extend a list in python --explain
INFO: Version: 2.0.19
INFO: Fetching answers for query: how to extend a list in python
INFO: Using cached links
INFO: Links from stackoverflow.com found on google: 1
INFO: URL: https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend
INFO: Answers requested: 1, Starting at position: 1
INFO: Total answers returned: 1
>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]
gleitz commented 2 years ago

That's interesting. I haven't done too much testing with Windows and colorization. We use pygments to do the highlighting currently.

Would you mind checking out this branch and seeing if that works any better? It uses the rich library and perhaps they have figured out how to work with Powershell.

https://github.com/gleitz/howdoi/pull/443

Then run python -m howdoi -c how to extend a list in python --explain. Here's what I see:

Screen Shot 2022-01-24 at 12 34 20 PM
SepehrRasouli commented 2 years ago

The problem still persists. the rich library hasn't fixed the issue.

> python -m howdoi -c how to extend a list in python --explain
INFO: Version: 2.0.18
INFO: Fetching answers for query: how to extend a list in python
INFO: Searching google with URL: https://www.google.com/search?q=site:stackoverflow.com%20how%20to%20extend%20a%20list%20in%20python&hl=en
INFO: Links from stackoverflow.com found on google: 1
INFO: URL: https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend
INFO: Answers requested: 1, Starting at position: 1
INFO: Total answers returned: 1
>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]

I suggest using the colorama library as it might fix the problem.

gleitz commented 2 years ago

According to this answer, powershell does support ANSI escape sequences but you have to enable it because it is not enabled by default:

https://stackoverflow.com/a/51681675

SepehrRasouli commented 2 years ago

Oh , thank you very much. This fixed my problem.

SepehrRasouli commented 2 years ago

But can the program change to colorama library ? This might take a lot of time for a user to change. If the program uses colorama , this problem can get fixed by colorama.init()

gleitz commented 2 years ago

I wonder if colorama works out of the box or if you'll still need to make that change to Powershell.

Adding @V2dha who was working on the rich library change.

gleitz commented 2 years ago

Also perhaps there is rich.init() or similar for windows users

SepehrRasouli commented 2 years ago

Using colorama will work out of the box and the user won't need to do any changes to Powershell.

gleitz commented 2 years ago

Ah I see, you use colorama together with rich and it monitors stdout, rewriting the colors as necessary.

@V2dha would you mind giving that a try on your branch and seeing if the Windows colors look correct? @SepehrRasouli can help test as well.

SepehrRasouli commented 2 years ago

Yes, I think that's a good idea , but you should add colorama.init() to the code to work. I think we can use colorama for the Windows part, because @V2dha's code works perfectly on a linux machine.

V2dha commented 2 years ago

Sorry for the delay! Thanks for the suggestion @SepehrRasouli, I would try to use colorama with rich to see the changes and test it further.

SepehrRasouli commented 2 years ago

Thanks a lot.

gleitz commented 2 years ago

@SepehrRasouli check out this branch. Does it work for you in Powershell? You might have to undo the manual fix we found earlier to check it.

https://github.com/gleitz/howdoi/pull/454

SepehrRasouli commented 2 years ago

This still does not fix the problem, I think the problem is that the code is still using rich library. Can the code be changed to only use colorama?

gleitz commented 2 years ago

I don't think I'll have time to make that switch, but I could accept a PR for the readme on the changes your need to make to allow ANSI in Powershell.

SepehrRasouli commented 2 years ago

I tried to fix the problem myself by looking at the program flow and checking where the color formatting happens but i can't config howdoi to run in vsocde debug mode. It repeatedly gives this error:

ImportError: cannot import name '__version__' from partially initialized module 'howdoi' (most likely due to a circular import) 

I tried running it via wsl , importing it from a diffrent python file and etc. but it still dosen't work. Can you help me ?

SepehrRasouli commented 2 years ago

I've found the reason why colorama doesn't work. On line 816 Colored code is encoded with UTF-8 which breaks colors for windows, Also on line 818 results are printed with sys which breaks the colors too. When i changed them to not use UTF-8 encoding and print results with print() function everything worked perfectly fine. Can the code be changed to only use these ? For example not using encode function when printing colored codes for Windows
For non-windows operating systems howdoi should still use utf-8 otherwise colors won't be shown

gleitz commented 2 years ago

Ah, very interesting. I updated my PR in https://github.com/gleitz/howdoi/pull/454/files#diff-4042f5742dd5a4939e7c7452b3dd2a4d13b02f84a4464add8bc5a24367abab36R821

Does that work for you now?

SepehrRasouli commented 2 years ago

Yes, This works perfectly. Thank you for your help :) screenshot