Skip to content

Fix cursor_coordinate going out of bounds into header for empty tables#6411

Open
krishraghuram wants to merge 1 commit intoTextualize:mainfrom
krishraghuram:patch-1
Open

Fix cursor_coordinate going out of bounds into header for empty tables#6411
krishraghuram wants to merge 1 commit intoTextualize:mainfrom
krishraghuram:patch-1

Conversation

@krishraghuram
Copy link

Description:
When a DataTable is empty (no rows or columns), pressing arrow keys could cause cursor_coordinate to become (-1, 0), which corresponds to the header row. This caused the header cell to be highlighted as if it were a selected data cell.

The issue was in _clamp_cursor_coordinate:

row = clamp(row, 0, self.row_count - 1)

When row_count = 0, this becomes clamp(-1, 0, -1), which returns -1, causing the issue.

Fix: Ensure the maximum bound is never negative:

row = clamp(row, 0, max(0, self.row_count - 1))
column = clamp(column, 0, max(0, len(self.columns) - 1))

Steps to reproduce:

  1. Create a DataTable and call clear()
  2. Press arrow up key
  3. Move mouse over the table
  4. Observe header cell is highlighted with cursor style

Video: https://github.com/user-attachments/assets/45096f0a-259a-4c75-b898-eae7df5f273b

@willmcgugan
Copy link
Member

Regression test please.

Jah-yee pushed a commit to Jah-yee/textual that referenced this pull request Mar 13, 2026
When a DataTable is empty (no rows), pressing arrow keys could cause
cursor_coordinate to become (-1, 0), highlighting the header incorrectly.

Fix: Use max(0, self.row_count - 1) to ensure the upper bound is never
negative, which prevents clamp() from returning -1.

Closes Textualize#6411
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants