diff --git a/ui-tui/packages/hermes-ink/src/ink/selection.test.ts b/ui-tui/packages/hermes-ink/src/ink/selection.test.ts index 0e0fba6573..83d77bf35d 100644 --- a/ui-tui/packages/hermes-ink/src/ink/selection.test.ts +++ b/ui-tui/packages/hermes-ink/src/ink/selection.test.ts @@ -55,6 +55,16 @@ describe('selection whitespace handling', () => { 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', () => { const { screen, styles } = screenWithText() const selection = createSelectionState() diff --git a/ui-tui/packages/hermes-ink/src/ink/selection.ts b/ui-tui/packages/hermes-ink/src/ink/selection.ts index 7c7505540b..76e776c22e 100644 --- a/ui-tui/packages/hermes-ink/src/ink/selection.ts +++ b/ui-tui/packages/hermes-ink/src/ink/selection.ts @@ -969,8 +969,8 @@ export function getSelectedText(s: SelectionState, screen: Screen): string { } for (let row = start.row; row <= end.row; row++) { - const rowStart = row === start.row ? start.col : 0 - const rowEnd = row === end.row ? end.col : screen.width - 1 + const rowStart = Math.max(0, row === start.row ? start.col : 0) + const rowEnd = Math.min(row === end.row ? end.col : screen.width - 1, screen.width - 1) const bounds = selectionContentBounds(screen, row, rowStart, rowEnd) joinRows(lines, bounds ? extractRowText(screen, row, bounds.first, bounds.last) : '', sw[row]! > 0)