Skip to content

Commit

Permalink
widgets: _text_area: Support zero-indexed line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
royatt committed May 2, 2024
1 parent 17e975d commit 4b13b49
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 "",
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit 4b13b49

Please sign in to comment.