Closed SepehrRasouli closed 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:
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
[91;49m>>[0m[91;49m>[0m[97;49m [0m[97;49mx[0m[97;49m [0m[91;49m=[0m[97;49m [0m[97;49m[[0m[37;49m1[0m[97;49m,[0m[97;49m [0m[37;49m2[0m[97;49m,[0m[97;49m [0m[37;49m3[0m[97;49m][0m
[91;49m>>[0m[91;49m>[0m[97;49m [0m[97;49mx[0m[91;49m.[0m[97;49mappend[0m[97;49m([0m[97;49m[[0m[37;49m4[0m[97;49m,[0m[97;49m [0m[37;49m5[0m[97;49m][0m[97;49m)[0m
[91;49m>>[0m[91;49m>[0m[97;49m [0m[97;49mprint[0m[97;49m([0m[97;49mx[0m[97;49m)[0m
[97;49m[[0m[37;49m1[0m[97;49m,[0m[97;49m [0m[37;49m2[0m[97;49m,[0m[97;49m [0m[37;49m3[0m[97;49m,[0m[97;49m [0m[97;49m[[0m[37;49m4[0m[97;49m,[0m[97;49m [0m[37;49m5[0m[97;49m][0m[97;49m][0m
I suggest using the colorama library as it might fix the problem.
According to this answer, powershell does support ANSI escape sequences but you have to enable it because it is not enabled by default:
Oh , thank you very much. This fixed my problem.
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()
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.
Also perhaps there is rich.init() or similar for windows users
Using colorama will work out of the box and the user won't need to do any changes to Powershell.
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.
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.
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.
Thanks a lot.
@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.
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?
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.
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 ?
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
Ah, very interesting. I updated my PR in https://github.com/gleitz/howdoi/pull/454/files#diff-4042f5742dd5a4939e7c7452b3dd2a4d13b02f84a4464add8bc5a24367abab36R821
Does that work for you now?
Yes, This works perfectly. Thank you for your help :)
What happened:
howdoi cant print out colors in Powershell Or CMD terminal in color mode.