diff --git a/src/textual/widgets/_text_area.py b/src/textual/widgets/_text_area.py index c9597e10259..10eed7ee439 100644 --- a/src/textual/widgets/_text_area.py +++ b/src/textual/widgets/_text_area.py @@ -164,6 +164,13 @@ class TextArea(ScrollView): | `text-area--matching-bracket` | Target matching brackets. | """ + LINE_NUMBER_START = 1 + """ + Allow a subclass to change the number lines starts at. + Gutter size will **not** be adjusted for negative signs, 0-indexed lines + will work as expected. + """ + BINDINGS = [ # Cursor movement Binding("up", "cursor_up", "cursor up", show=False), @@ -1133,7 +1140,9 @@ def render_line(self, y: int) -> Strip: gutter_style = theme.gutter_style gutter_width_no_margin = gutter_width - 2 - gutter_content = str(line_index + 1) if section_offset == 0 else "" + gutter_content = ( + str(line_index + self.LINE_NUMBER_START) if section_offset == 0 else "" + ) gutter = Text( f"{gutter_content:>{gutter_width_no_margin}} ", style=gutter_style or "", @@ -1458,7 +1467,8 @@ def gutter_width(self) -> int: # The longest number in the gutter plus two extra characters: `│ `. gutter_margin = 2 gutter_width = ( - len(str(self.document.line_count)) + gutter_margin + len(str(self.document.line_count - 1 + self.LINE_NUMBER_START)) + + gutter_margin if self.show_line_numbers else 0 )