fix(tui): clamp copied selection bounds

Clamp copied selection columns to the screen width before scanning rendered cells.
This commit is contained in:
Brooklyn Nicholson 2026-04-25 15:32:45 -05:00
parent b1c18e5a41
commit 31d7f1951a
2 changed files with 12 additions and 2 deletions

View file

@ -55,6 +55,16 @@ describe('selection whitespace handling', () => {
expect(getSelectedText(selection, screen)).toBe(' x') expect(getSelectedText(selection, screen)).toBe(' x')
}) })
it('clamps copied selection bounds to screen width', () => {
const { screen } = screenWithText()
const selection = createSelectionState()
startSelection(selection, 0, 1)
updateSelection(selection, 99, 1)
expect(getSelectedText(selection, screen)).toBe('hi')
})
it('does not paint selection background on leading/trailing empty cells or empty rows', () => { it('does not paint selection background on leading/trailing empty cells or empty rows', () => {
const { screen, styles } = screenWithText() const { screen, styles } = screenWithText()
const selection = createSelectionState() const selection = createSelectionState()

View file

@ -969,8 +969,8 @@ export function getSelectedText(s: SelectionState, screen: Screen): string {
} }
for (let row = start.row; row <= end.row; row++) { for (let row = start.row; row <= end.row; row++) {
const rowStart = row === start.row ? start.col : 0 const rowStart = Math.max(0, row === start.row ? start.col : 0)
const rowEnd = row === end.row ? end.col : screen.width - 1 const rowEnd = Math.min(row === end.row ? end.col : screen.width - 1, screen.width - 1)
const bounds = selectionContentBounds(screen, row, rowStart, rowEnd) const bounds = selectionContentBounds(screen, row, rowStart, rowEnd)
joinRows(lines, bounds ? extractRowText(screen, row, bounds.first, bounds.last) : '', sw[row]! > 0) joinRows(lines, bounds ? extractRowText(screen, row, bounds.first, bounds.last) : '', sw[row]! > 0)