TextArea gutter is too narrow when `line_number_start` is negative

Open Beginner friendly
#6,787 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
frontend

Research direction

Start with the TextArea widget, where gutter_width is computed from the last line number and render_line draws the gutter using line_number_start. Run the reproduction in the issue to see the misaligned -10 row. Done means the gutter is sized from the widest of the first and last numbers, so all rows start the text in the same column, with a test covering negative starts. Confirm first with a maintainer whether negative starts are supported.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets.

TextArea.gutter_width is sized from the last line number only. With a negative line_number_start the first number can be the longest (-10 against -8), so its line is pushed one cell to the right.

import asyncio

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

class GutterApp(App):
    def compose(self) -> ComposeResult:
        yield TextArea("aaa\nbbb\nccc", show_line_numbers=True, line_number_start=-10)

async def main() -> None:
    app = GutterApp()
    async with app.run_test(size=(30, 7)) as pilot:
        text_area = app.query_one(TextArea)
        await pilot.pause()
        print("gutter_width:", text_area.gutter_width)
        for y in range(3):
            print(repr(text_area.render_line(y).text.rstrip()))
    print("Textual", textual.__version__)

asyncio.run(main())

Output on Textual 8.2.7 (and the same on main at 5e5b7ef):

gutter_width: 4
'-10  aaa'
'-9  bbb'
'-8  ccc'

I expected the text to start in the same column on every line. I'm not sure negative starts are meant to be supported (nothing rejects them, and -1, 0, 1 hits the same thing), so is this worth fixing? If so, sizing the gutter from the longer of the first and last numbers is a small change, and I have it with a test.

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.