Skip to content

[BUG] ellipsis rendering fails for non-unicode encoding in tables #3761

Closed as not planned
@dlax

Description

@dlax

Describe the bug

When using a non-unicode encoding, e.g. "ascii", rendering of ellipsis in truncated table cells fails:

# file: t.py
from rich.console import Console
from rich.table import Table

console = Console(width=10)
print(f"{console.encoding=}")

t = Table("column")
t.add_row("x" * 10)
console.print(t)
$ env PYTHONIOENCODING=ascii python t.py
console.encoding='ascii'
Traceback (most recent call last):
  File "/tmp/t.py", line 9, in <module>
    console.print(t)
    ~~~~~~~~~~~~~^^^
  File ".../rich/console.py", line 1692, in print
    with self:
         ^^^^
  File ".../rich/console.py", line 866, in __exit__
    self._exit_buffer()
    ~~~~~~~~~~~~~~~~~^^
  File ".../rich/console.py", line 824, in _exit_buffer
    self._check_buffer()
    ~~~~~~~~~~~~~~~~~~^^
  File ".../rich/console.py", line 2033, in _check_buffer
    self._write_buffer()
    ~~~~~~~~~~~~~~~~~~^^
  File "...rich/console.py", line 2102, in _write_buffer
    self.file.write(text)
    ~~~~~~~~~~~~~~~^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode character '\u2026' in position 64: ordinal not in range(128)
*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***

As a workaround, one can force the "overflow" method per-column:

t = Table()
t.add_column("column", overflow="fold")
t.add_row("x" * 10)

but this is a bit cumbersome and in fact prevents usage of "ellipsis" overflow method.

As the rest of the table, borders in particular, is aware of the encoding and have its rendering adjusted, I wonder if text truncation in Text.truncate() should have the same fallback and use an ASCII character (to be determined)?

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?
Alacritty, on Linux.

I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.

If you're using Rich in a terminal:

$ python -m rich.diagnose
+---------------------- <class 'rich.console.Console'> -----------------------+
| A high level console interface.                                             |
|                                                                             |
| +-------------------------------------------------------------------------+ |
| | <console width=79 ColorSystem.TRUECOLOR>                                | |
| +-------------------------------------------------------------------------+ |
|                                                                             |
|     color_system = 'truecolor'                                              |
|         encoding = 'ascii'                                                  |
|             file = <_io.TextIOWrapper name='<stdout>' mode='w'              |
|                    encoding='ascii'>                                        |
|           height = 49                                                       |
|    is_alt_screen = False                                                    |
| is_dumb_terminal = False                                                    |
|   is_interactive = True                                                     |
|       is_jupyter = False                                                    |
|      is_terminal = True                                                     |
|   legacy_windows = False                                                    |
|         no_color = False                                                    |
|          options = ConsoleOptions(                                          |
|                        size=ConsoleDimensions(width=79, height=49),         |
|                        legacy_windows=False,                                |
|                        min_width=1,                                         |
|                        max_width=79,                                        |
|                        is_terminal=True,                                    |
|                        encoding='ascii',                                    |
|                        max_height=49,                                       |
|                        justify=None,                                        |
|                        overflow=None,                                       |
|                        no_wrap=False,                                       |
|                        highlight=None,                                      |
|                        markup=None,                                         |
|                        height=None                                          |
|                    )                                                        |
|            quiet = False                                                    |
|           record = False                                                    |
|         safe_box = True                                                     |
|             size = ConsoleDimensions(width=79, height=49)                   |
|        soft_wrap = False                                                    |
|           stderr = False                                                    |
|            style = None                                                     |
|         tab_size = 8                                                        |
|            width = 79                                                       |
+-----------------------------------------------------------------------------+
+--- <class 'rich._windows.WindowsConsoleFeatures'> ----+
| Windows features available.                           |
|                                                       |
| +---------------------------------------------------+ |
| | WindowsConsoleFeatures(vt=False, truecolor=False) | |
| +---------------------------------------------------+ |
|                                                       |
| truecolor = False                                     |
|        vt = False                                     |
+-------------------------------------------------------+
+------ Environment Variables -------+
| {                                  |
|     'CLICOLOR': None,              |
|     'COLORTERM': 'truecolor',      |
|     'COLUMNS': None,               |
|     'JPY_PARENT_PID': None,        |
|     'JUPYTER_COLUMNS': None,       |
|     'JUPYTER_LINES': None,         |
|     'LINES': None,                 |
|     'NO_COLOR': None,              |
|     'TERM_PROGRAM': None,          |
|     'TERM': 'alacritty',           |
|     'TTY_COMPATIBLE': None,        |
|     'VSCODE_VERBOSE_LOGGING': None |
| }                                  |
+------------------------------------+
platform="Linux"
$ pip freeze | grep rich
-e git+https://github.com/willmcgugan/rich@c8f234f136b3ec60da6a96364705490b7ee9cf39#egg=rich

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions