Skip to content

[BUG] tables crop content with long words #3903

@greatvovan

Description

@greatvovan

Describe the bug

Long words that did not fit into a narrow cell are cropped instead of wrapping.

from rich.console import Console
from rich.table import Table

table = Table(width=30)
table.add_column("No")
table.add_column("Text")

table.add_row("1", "com company collections styles examples unicorn")
table.add_row("2", "com.company.collections.styles.examples.unicorn")
table.add_row("3", "wrap wrap wrap com.company.collections.styles.examples.unicorn")

console = Console()
console.print(table)

Output:

┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
┃ No ┃ Text                  ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
│ 1  │ com company           │
│    │ collections styles    │
│    │ examples unicorn      │
│ 2  │ com.company.collecti… │
│ 3  │ wrap wrap wrap        │
│    │ com.company.collecti… │
└────┴───────────────────────┘

I think, we never should trim content unless instructed by a special parameter.
Desired behaviour is wrapping by actual border of the cell:

┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
┃ No ┃ Text                  ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
│ 1  │ com company           │
│    │ collections styles    │
│    │ examples unicorn      │
│ 2  │ com.company.collectio │
|    | ns.styles.examples.un |
|    | icorn                 |
│ 3  │ wrap wrap wrap        │
│    │ com.company.collectio │
|    | ns.styles.examples.un |
|    | icorn                 |
└────┴───────────────────────┘

Probably we should also add a special character like to indicate that forced wrapping occured.

┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
┃ No ┃ Text                  ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
│ 1  │ com company           │
│    │ collections styles    │
│    │ examples unicorn      │
│ 2  │ com.company.collectio↩│
|    | ns.styles.examples.un↩|
|    | icorn                 |
│ 3  │ wrap wrap wrap        │
│    │ com.company.collectio↩│
|    | ns.styles.examples.un↩|
|    | icorn                 |
└────┴───────────────────────┘

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