Skip to content

[BUG] Text objects pre-formatted with ANSI escapes miscalculate their width #1670

@0xDEC0DE

Description

@0xDEC0DE

When attempting to use "pre-formatted" strings from a third-party library with Rich tables, it seems to use the "whole width" of the string for header calculation rather than the "visible width"

Steps to reproduce

A simple test case, I'm using colored to illustrate:

from colored import bg, fg, attr

from rich.box import ASCII
from rich.console import Console
from rich.table import Table
from rich.text import Text

# simulate getting a "cooked" string from a library
text = bg(28) + fg(186) + 'this is test text' + attr(0)

table = Table(box=ASCII)
table.add_column('column')
table.add_row(Text(text))

Console().print(table)

Expected result

+-------------------+
| column            |
|-------------------|
| this is test text |
+-------------------+

Actual behavior

+-----------------------------------------+
| column                                  |
|-----------------------------------------|
| this is test text |
+-----------------------------------------+

Errata

I see class methods like Text.from_markup and Text.styled that would appear to be useful for creating Text objects from "cooked" inputs, but they don't appear to be made for this particular flavor. And attempting to adjust the Text._length property implicitly truncates the string.

Platform

╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=120 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 77                                                            │
│    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=120, height=77),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=120,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=77,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=120, height=77)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 120                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)
rich==10.13.0

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