`text-wrap: nowrap` with `line-pad` filling the width raises `ValueError: range() arg 3 must not be zero`

Open Beginner friendly
#6,782 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

Research direction

The crash comes from get_height in textual/content.py (around line 613), which passes width - line_pad * 2 into _wrap_and_format, where the nowrap + fold branch (around line 670) uses it as a range step. Run the reproduction app with Static { width: 2; line-pad: 1; text-wrap: nowrap; } to confirm the ValueError. Done when that layout returns a height without raising, and the zero-width case is handled the same way negative widths already are. The wrapping path reaching Rich's divide_line with width 0 (rich#4234) is a separate issue and should stay out of scope.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets. When a widget's line-pad takes up its whole content width and text-wrap is nowrap, measuring its height crashes.

from textual.app import App, ComposeResult
from textual.widgets import Static

class LinePadApp(App):
    CSS = "Static { width: 2; line-pad: 1; text-wrap: nowrap; }"

    def compose(self) -> ComposeResult:
        yield Static("Hello")

if __name__ == "__main__":
    LinePadApp().run()

Expected: an (empty-looking) two-cell widget.

Actual (on Textual 8.2.7 with Rich 15.0.0, and on main at 5e5b7ef with Rich 14.2.0 and with Rich main):

  File "textual/content.py", line 613, in get_height
    lines = self.without_spans._wrap_and_format(
        width - line_pad, overflow=overflow, no_wrap=no_wrap
    )
  File "textual/content.py", line 670, in _wrap_and_format
    cuts = list(range(0, line.cell_length, width))[1:]
ValueError: range() arg 3 must not be zero

get_height passes width - line_pad * 2, which is 0 here, and the nowrap + fold branch uses it as a range step. A width less than 0 (for example width: 1) gives an empty range and doesn't raise.

With wrapping on (the default), the same layout reaches Rich's divide_line with width 0, which is rich#4234. A plain Button with width: 2; min-width: 0 hits that one, with Rich 15.

I'd suggest skipping the cuts when the width is 0 or less, which is what already happens for negative widths. I'm happy to send a PR.

Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.