rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] rich.table.Table._get_padding_width() forgets to account for self.pad_edge

Open mgedmin opened this issue 1 month ago • 2 comments

  • [x] I've checked docs and closed issues for possible solutions.
  • [x] I can't find my issue in the FAQ.

Describe the bug

When you create a rich.table.Table() with columns that have fixed widths and set that table's pad_edge=False, the first and the last columns get extra padding on the right side. The first column gets left padding's worth of extra space, but places it on the right. The last column gets right padding's worth of extra space.

import io

import pytest
from rich.console import Console
from rich.table import Column, Table


@pytest.fixture
def output() -> io.StringIO:
    return io.StringIO()


@pytest.fixture
def console(output: io.StringIO) -> Console:
    return Console(
        width=60,
        file=output,
        force_terminal=True,
        legacy_windows=False,
        color_system=None,
        _environ={},
    )


def test_table_with_fixed_column_widths(
    console: Console, output: io.StringIO
) -> None:
    table = Table(
        Column('verb', width=5),
        Column('noun', width=5),
        Column('wh', width=2),
        box=None,
        pad_edge=False,
        expand=False,
        show_header=True,
    )
    table.add_row('hello', 'world', 'oh')
    console.print(table)
    assert output.getvalue().rstrip('\n') == '\n'.join([
        'verb   noun   wh',
        'hello  world  oh'
    ])

Platform

Click to expand

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

Ubuntu 25.10, gnome-terminal

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=190 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 45                                                            │
│    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=190, height=45),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=190,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=45,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=190, height=45)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 190                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <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': 'xterm-256color',      │
│     'TTY_COMPATIBLE': None,        │
│     'TTY_INTERACTIVE': None,       │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"


uv pip freeze | grep rich
rich==14.2.0

mgedmin avatar Nov 19 '25 14:11 mgedmin

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

Rich was created by Will McGugan. Consider sponsoring Will's work on Rich.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Nov 19 '25 14:11 github-actions[bot]

This is a fix:

diff --git i/rich/table.py w/rich/table.py
index 942175dc..fb880dbb 100644
--- i/rich/table.py
+++ w/rich/table.py
@@ -702,6 +702,11 @@ class Table(JupyterMixin):
         if self.collapse_padding:
             if column_index > 0:
                 pad_left = max(0, pad_left - pad_right)
+        if not self.pad_edge:
+            if column_index == 0:
+                pad_left = 0
+            if column_index == len(self.columns) - 1:
+                pad_right = 0
         return pad_left + pad_right
 
     def _measure_column(

I will create a pull request, once I figure out how to create pull requests properly (#3882 is the one I'm currently attempting, but I can't run the tests locally without unrelated failures, and GitHub CI is still waiting for maintainer's approval).

mgedmin avatar Nov 19 '25 14:11 mgedmin