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

[BUG] Colors doesn't work on Git Bash #330

Closed
naveen521kk opened this issue Oct 4, 2020 · 10 comments
Closed

[BUG] Colors doesn't work on Git Bash #330

naveen521kk opened this issue Oct 4, 2020 · 10 comments

Comments

@naveen521kk
Copy link

naveen521kk commented Oct 4, 2020

Describe the bug
I am Windows 7 and install python 3.8 where I used Git Bash(For a change.). I typed the following things in terminal where it should show the image as in ReadMe.
While I get a long error Message of UnicodeEncodeError
The output I get is

                                 Rich features

              Colors 256 colors or 16.7 million colors (if supported by your
                     terminal).








              Styles All ansi styles: bold, dim, italic, underline,
                     strikethrough, reverse, and even blink.

                Text Word wrap text. Justify left, center, right or full.

                     Lorem ipsum    Lorem ipsum     Lorem ipsum Lorem    ipsum
                     dolor sit       dolor sit        dolor sit dolor      sit
                     amet,             amet,              amet, amet,
                     consectetur    consectetur     consectetur consectetur
                     adipiscing     adipiscing       adipiscing adipiscing
                     elit. Quisque elit. Quisque  elit. Quisque elit.  Quisque
                     in metus sed  in metus sed    in metus sed in  metus  sed
                     sapien           sapien             sapien sapien
                     ultricies       ultricies        ultricies ultricies
                     pretium a at  pretium a at    pretium a at pretium  a  at
                     justo.           justo.             justo. justo.
                     Maecenas        Maecenas          Maecenas Maecenas
                     luctus velit  luctus velit    luctus velit luctus   velit
                     et auctor       et auctor        et auctor et      auctor
                     maximus.        maximus.    maximus. Donec maximus. Donec
                     Donec             Donec       faucibus vel faucibus   vel
                     faucibus vel  faucibus vel         arcu id arcu        id
                     arcu id          arcu id          pretium. pretium.
                     pretium.        pretium.

         CJK support Traceback (most recent call last):
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\__main__.py", line 188, in <module>
    print(line)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 187, in write_and_convert
    self.write_plain_text(text, cursor, start)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-31: character maps to <undefined>

and no colours in it.
image
Also, when I run py -m rich.logging
I get a ZeroDivisionError.

[17:18:29] INFO     Server starting...                           logging.py:155
           INFO     Listening on http://127.0.0.1:8080           logging.py:156
[17:18:30] INFO     GET /index.html 200 1298                     logging.py:159
           INFO     GET /imgs/backgrounds/back1.jpg 200 54386    logging.py:160
           INFO     GET /css/styles.css 200 54386                logging.py:161
           WARNING  GET /favicon.ico 404 242                     logging.py:162
[17:18:31] DEBUG    JSONRPC request                              logging.py:165
                    --> {'version': '1.1', 'method':
                    'confirmFruitPurchase', 'params': [['apple',
                    'orange', 'mangoes', 'pomelo'], 1.123],
                    'id': '194521489'}
                    <-- {'version': '1.1', 'result': True,
                    'error': None, 'id': '194521489'}
           DEBUG    Loading configuration file /adasd/asdasd/qeq logging.py:175
                    we/qwrqwrqwr/sdgsdgsdg/werwerwer/dfgerert/er
                    tertert/ertetert/werwerwer
           ERROR    Unable to find 'pomelo' in database!         logging.py:178
           INFO     POST /jsonrpc/ 200 65532                     logging.py:179
           INFO     POST /admin/ 401 42234                       logging.py:180
           WARNING  password was rejected for admin site.        logging.py:181
           ERROR    An error of some kind occurred!              logging.py:185
Traceback (most recent call last):
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\logging.py", line 183, in <module>
    1 / 0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\logging.py", line 185, in <module>
    log.exception("An error of some kind occurred!")
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1469, in exception
    self.error(msg, *args, exc_info=exc_info, **kwargs)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1463, in error
    self._log(ERROR, msg, args, **kwargs)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1577, in _log
    self.handle(record)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1587, in handle
    self.callHandlers(record)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1649, in callHandlers
    hdlr.handle(record)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 950, in handle
    self.emit(record)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\logging.py", line 128, in emit
    self.console.print(
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\console.py", line 1028, in print
    self._buffer.extend(new_segments)
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\console.py", line 507, in __exit__
    self._exit_buffer()
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\console.py", line 485, in _exit_buffer
    self._check_buffer()
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\site-packages\rich\console.py", line 1156, in _check_buffer
    write(line)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "C:\Users\Naveen User\AppData\Roaming\Python\Python38\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text
    self.wrapped.write(text[start:end])
  File "C:\Users\Naveen User\AppData\Local\Programs\Python\Python38\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 20-23: character maps to <undefined>
*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***

image
Btw, everything is fine with CMD or Powershell.

To Reproduce
Running py -m rich in Git Bash For Windows shows these errors.

Platform
I am on Windows 7. The terminal I am using is Git bash. The rich version is rich==8.0.0

@willmcgugan
Copy link
Collaborator

You are missing some environment variables that set the locale.

Try the following:

export LANG=en_US.UTF-8 
python -m rich

Or

PYTHONIOENCODING=utf-8 python -m rich

@naveen521kk
Copy link
Author

naveen521kk commented Oct 4, 2020

Ok now that render's fully but now it misses colour.
rich-no-colour

@willmcgugan
Copy link
Collaborator

What is your TERM env var set to?

echo $TERM
echo $COLORTERM

Does this give you None?

from rich.console import Console
console = Console()
print(repr(console.color_system))

@naveen521kk
Copy link
Author

$ echo $TERM
xterm
$ echo $COLORTERM

The below one gives me None.

from rich.console import Console
console = Console()
print(repr(console.color_system))

@willmcgugan
Copy link
Collaborator

How about this?

echo $NO_COLOR

and this...

print(console.is_terminal)

@naveen521kk
Copy link
Author

$ echo $NO_COLOR

The second one is False.

@willmcgugan
Copy link
Collaborator

If "is_terminal" is False it means that git bash isn't a real terminal, and Rich is defaulting to text without color.

You can force Rich to bypass terminal detection with the following:

console = Console(force_terminal=True)
console.print("[bold blue]Color")

I don't know much about git bash, but it looks like it is emulated and not a full terminal.

@naveen521kk
Copy link
Author

I got colour working with Poetry.(While colour doesn't work on cmd nor PowerShell but works in Git bash.) It uses Pastel. I think Git Bash is very similar to bash but I really don't know about it much.

@naveen521kk
Copy link
Author

naveen521kk commented Oct 4, 2020

Update: Trying the same on Msys2 MINGW32 bash shell I got the coloured output but it looks worse with tables.

image
There all the things came as in git bash except the last one

print(console.is_terminal)

^ returned True.

@willmcgugan
Copy link
Collaborator

It looks like it doesn't support unicode characters as used by the table borders. You should use box.ASCII when rendering tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants