Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken support of Windows Powershell #79

Closed
HexPandaa opened this issue Jul 20, 2023 · 9 comments · Fixed by #80 or #85
Closed

Broken support of Windows Powershell #79

HexPandaa opened this issue Jul 20, 2023 · 9 comments · Fixed by #80 or #85
Labels
bug Something isn't working

Comments

@HexPandaa
Copy link

Hello and thank you for your great module!

I'm encountering a strange problem when using the module in a native Windows Powershell window.

Here's the sample code I'm using:

import argparse
from rich_argparse import RichHelpFormatter
from rich import print as rprint

def main():
    rprint("[italic blue]It's working[/]")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=RichHelpFormatter)
    parser.parse_args()
    main()

Here's the output in a native Windows Powershell window, the output from rich_argparse is broken, however the output from rich is ok.
ps

The output from Windows Terminal shows that both rich_argparse and rich work.
wt

@hamdanal
Copy link
Owner

Thank you for the report.

This should be fixed on main 🤞🏻. Could you please check if it works in your terminal?
You can install it with python -m pip install "git+https://github.com/hamdanal/rich-argparse".

@HexPandaa
Copy link
Author

Thank you for the report.

This should be fixed on main 🤞🏻. Could you please check if it works in your terminal? You can install it with python -m pip install "git+https://github.com/hamdanal/rich-argparse".

The problem seems to be solved, thanks a lot!
image

Just a quick question: I guess the Usage and Options strings are not colored because native PowerShell don't know these colours?

@hamdanal hamdanal reopened this Jul 27, 2023
@hamdanal
Copy link
Owner

Thanks for checking.

Just a quick question: I guess the Usage and Options strings are not colored because native PowerShell don't know these colours?

This is weird because it works on my machine
ra-windows-ps

I will need more information to debug this further. Could you please execute these commands in the same terminal and paste their output here:

  1. python -m rich.diagnose
  2. python -c 'import sys; print(sys.getwindowsversion())'
  3. python -c 'import sys; print(sys.version)'

@hamdanal hamdanal added the awaiting response Waiting for further information from author label Aug 6, 2023
@Dobatymo
Copy link

Dobatymo commented Aug 11, 2023

I think my issue is related. I am using the default Windows cmd on Windows 7. Latest version on pypi shows weird control codes like ←[0m] [←[36m instead of colors. Latest version on main shows no control codes, but no colors either.

rich.diagnose is nice and colorful

┌───────────────────────── <class 'rich.console.Console'> ─────────────────────────┐
│ A high level console interface.                                                  │
│                                                                                  │
│ ┌──────────────────────────────────────────────────────────────────────────────┐ │
│ │ <console width=159 ColorSystem.WINDOWS>                                      │ │
│ └──────────────────────────────────────────────────────────────────────────────┘ │
│                                                                                  │
│     color_system = 'windows'                                                     │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 41                                                            │
│    is_alt_screen = False                                                         │
│ is_dumb_terminal = False                                                         │
│   is_interactive = True                                                          │
│       is_jupyter = False                                                         │
│      is_terminal = True                                                          │
│   legacy_windows = True                                                          │
│         no_color = False                                                         │
│          options = ConsoleOptions(                                               │
│                        size=ConsoleDimensions(width=159, height=41),             │
│                        legacy_windows=True,                                      │
│                        min_width=1,                                              │
│                        max_width=159,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=41,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=159, height=41)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 159                                                           │
└──────────────────────────────────────────────────────────────────────────────────┘
┌─── <class 'rich._windows.WindowsConsoleFeatures'> ────┐
│ Windows features available.                           │
│                                                       │
│ ┌───────────────────────────────────────────────────┐ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ └───────────────────────────────────────────────────┘ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
└───────────────────────────────────────────────────────┘
┌────── Environment Variables ───────┐
│ {                                  │
│     'TERM': None,                  │
│     'COLORTERM': None,             │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
└────────────────────────────────────┘
platform="Windows"

py -c "import sys; print(sys.getwindowsversion())"

sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')

py -c "import sys; print(sys.version)"

3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]

@hamdanal
Copy link
Owner

Thank you @Dobatymo. This behavior is expected with CMD on older versions of Windows including Windows 7. The rich-argparse version on GitHub main removes colors on old Windows versions that don't support ANSI escape sequences. We may be able to improve this situation but I don't have a Windows 7 machine handy to test.
If someone could test the following in CMD or PowerShell consoles on Windows 7 we may be able to get colors working there.

  1. What is the output of this code? (You need rich, install with pip install rich)
    from rich import print
    print("[red]This is red.[/red]\n[dark_orange]This is dark orange.[/dark_orange]\n[cyan]This is cyan.[/cyan]")
  2. And this? (You need colorama and rich-argparse, pip install colorama rich-argparse)
    import argparse
    import colorama
    from rich_argparse import RichHelpFormatter
    
    colorama.just_fix_windows_console()
    argparse.ArgumentParser(prog="PROG", formatter_class=RichHelpFormatter).print_help()

@Dobatymo
Copy link

Dobatymo commented Aug 14, 2023

  1. is colorful and looks like expected.
  2. doesn't have any colors and no broken escape sequences (on main and 1.2.0 pypi)

cmd

@hamdanal
Copy link
Owner

Thank you @Dobatymo this is very helpful. One last test is to replace the colorama.just_fix_windows_console() call in case 2 by colorama.init(). Only rich-argparse pypi version is needed.

@Dobatymo
Copy link

Dobatymo commented Aug 14, 2023

@hamdanal Using .init() it's nice and colorful using the pypi version (and colorless using main)!

cmd

@hamdanal
Copy link
Owner

Thank you @Dobatymo and @HexPandaa. The newest version on main fixed support for CMD and PowerShell. On Windows 10 and newer, it works out of the box. On older versions of windows, you'll need to call colorama.init() otherwise colors are stripped from the output. This is now documented here: https://github.com/hamdanal/rich-argparse#legacy-windows-support. I'll make a new release later today.

@hamdanal hamdanal removed the awaiting response Waiting for further information from author label Aug 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants