Repository navigation
feat(cli): mouse text selection and copy in VP mode #6937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e006d13
docs(cli): design for VP mode mouse text selection and copy
qinqi-ali 21dc34c
docs(cli): revise VP selection design per feasibility audit
qinqi-ali 166b15a
feat(cli): M0 Ink frame-buffer controller for VP text selection
qinqi-ali 2b65084
feat(cli): M1 mouse text selection with live highlight in VP mode
qinqi-ali e87cecb
feat(cli): gate VP text selection behind ui.textSelection settings
qinqi-ali f5e5ef4
feat(cli): M4 word/line select, selection invalidation, and docs
qinqi-ali 7ad8dfa
Merge branch 'main' into feat/vp-mouse-selection
wenshao e98a206
fix(cli): drop redundant resize listener in selection controller
qinqi-ali 06e3647
Merge remote-tracking branch 'origin/main' into feat/vp-mouse-selection
qinqi-ali e980512
Merge remote-tracking branch 'origin/main' into feat/vp-mouse-selection
qinqi-ali 9001b2f
fix(cli): close VP selection review gaps
qinqi-ali 595dadf
Merge remote-tracking branch 'origin/main' into feat/vp-mouse-selection
qinqi-ali cd6ce8b
refactor(cli): make VP text selection unconditional
qinqi-ali 20b2fa5
fix(cli): make VP mouse selection usable
qinqi-ali 652a937
fix(cli): preserve selection in thought blocks
qinqi-ali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, afterEach } from 'vitest'; | ||
| import { Text } from 'ink'; | ||
| import { render } from 'ink-testing-library'; | ||
| import { getScreenBuffer } from './screen-buffer.js'; | ||
|
|
||
| /** The background escape the patched renderer appends to selected cells. */ | ||
| const SELECTION_BG = '[48;5;240m'; | ||
|
|
||
| type Rendered = ReturnType<typeof render>; | ||
|
|
||
| let current: Rendered | undefined; | ||
|
|
||
| afterEach(() => { | ||
| current?.unmount(); | ||
| current = undefined; | ||
| }); | ||
|
|
||
| describe('ScreenBuffer (Ink frame-controller M0)', () => { | ||
| it('exposes the composited frame as addressable cells', () => { | ||
| current = render(<Text>hello 中文</Text>); | ||
| const buffer = getScreenBuffer( | ||
| current.stdout as unknown as NodeJS.WriteStream, | ||
| ); | ||
|
|
||
| expect(buffer).toBeDefined(); | ||
| expect(buffer!.dimensions.height).toBeGreaterThan(0); | ||
| expect(buffer!.lineText(0)).toBe('hello 中文'); | ||
| expect(buffer!.getCellAt(0, 0)?.value).toBe('h'); | ||
| }); | ||
|
|
||
| it('handles wide characters with a leading cell and a spacer', () => { | ||
| current = render(<Text>hello 中文</Text>); | ||
| const buffer = getScreenBuffer( | ||
| current.stdout as unknown as NodeJS.WriteStream, | ||
| )!; | ||
|
|
||
| // "hello " occupies columns 0-5; the first wide glyph starts at column 6. | ||
| const wide = buffer.getCellAt(6, 0); | ||
| expect(wide?.value).toBe('中'); | ||
| expect(wide?.fullWidth).toBe(true); | ||
| // The trailing half of a wide glyph is an empty spacer cell. | ||
| expect(buffer.getCellAt(7, 0)?.value).toBe(''); | ||
| }); | ||
|
|
||
| it('highlights the selected range before serialization and clears it', () => { | ||
| current = render(<Text>hello world</Text>); | ||
| const buffer = getScreenBuffer( | ||
| current.stdout as unknown as NodeJS.WriteStream, | ||
| )!; | ||
|
|
||
| expect(current.lastFrame()).not.toContain(SELECTION_BG); | ||
|
|
||
| buffer.setSelection({ sx: 0, sy: 0, ex: 4, ey: 0 }); | ||
| expect(current.lastFrame()).toContain(SELECTION_BG); | ||
|
|
||
| buffer.setSelection(null); | ||
| expect(current.lastFrame()).not.toContain(SELECTION_BG); | ||
| }); | ||
|
|
||
| it('does not leak the highlight onto identical text elsewhere on screen', () => { | ||
| // "abc" appears twice; selecting the first must not highlight the second. | ||
| current = render(<Text>abc{'\n'}abc</Text>); | ||
| const buffer = getScreenBuffer( | ||
| current.stdout as unknown as NodeJS.WriteStream, | ||
| )!; | ||
|
|
||
| buffer.setSelection({ sx: 0, sy: 0, ex: 2, ey: 0 }); | ||
| const frame = current.lastFrame() ?? ''; | ||
| const [firstLine, secondLine] = frame.split('\n'); | ||
|
|
||
| expect(firstLine).toContain(SELECTION_BG); | ||
| expect(secondLine).not.toContain(SELECTION_BG); | ||
| }); | ||
|
|
||
| it('publishes exactly one frame per distinct selection change (no loop, deduped)', () => { | ||
| current = render(<Text>hello world</Text>); | ||
| const buffer = getScreenBuffer( | ||
| current.stdout as unknown as NodeJS.WriteStream, | ||
| )!; | ||
|
|
||
| let publishes = 0; | ||
| buffer.subscribe(() => { | ||
| publishes++; | ||
| }); | ||
|
|
||
| buffer.setSelection({ sx: 0, sy: 0, ex: 2, ey: 0 }); | ||
| expect(publishes).toBe(1); | ||
|
|
||
| // Identical selection is deduplicated: no extra render. | ||
| buffer.setSelection({ sx: 0, sy: 0, ex: 2, ey: 0 }); | ||
| expect(publishes).toBe(1); | ||
|
|
||
| // A different selection renders once more. | ||
| buffer.setSelection({ sx: 0, sy: 0, ex: 4, ey: 0 }); | ||
| expect(publishes).toBe(2); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| getFrameController, | ||
| type FrameController, | ||
| type ReadonlyFrame, | ||
| type ScreenSelection, | ||
| } from 'ink'; | ||
|
|
||
| /** A single addressable cell of the composited screen. */ | ||
| export interface ScreenCell { | ||
| /** The character in this cell; empty string for a wide-character spacer. */ | ||
| value: string; | ||
| /** Whether this cell holds the leading half of a wide (2-column) character. */ | ||
| fullWidth: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Read side of the frame-buffer bridge. Wraps Ink's {@link FrameController} to | ||
| * answer "what character is at (x, y)" against the latest composited frame and | ||
| * to push a selection range that the renderer highlights before serialization. | ||
| * | ||
| * This is the foundation the selection state machine (M1) builds on; it does | ||
| * not itself own any mouse or keyboard handling. | ||
| */ | ||
| export class ScreenBuffer { | ||
| constructor(private readonly controller: FrameController) {} | ||
|
|
||
| get frame(): ReadonlyFrame | null { | ||
| return this.controller.getFrame(); | ||
| } | ||
|
|
||
| get dimensions(): { width: number; height: number } { | ||
| const frame = this.frame; | ||
| return { width: frame?.width ?? 0, height: frame?.height ?? 0 }; | ||
| } | ||
|
|
||
| getCellAt(x: number, y: number): ScreenCell | null { | ||
| const cell = this.frame?.cells[y]?.[x]; | ||
| return cell ? { value: cell.value, fullWidth: cell.fullWidth } : null; | ||
| } | ||
|
|
||
| /** Visual text of a row, with trailing padding trimmed. */ | ||
| lineText(y: number): string { | ||
| const row = this.frame?.cells[y]; | ||
| if (!row) { | ||
| return ''; | ||
| } | ||
| return row | ||
| .map((cell) => cell.value) | ||
| .join('') | ||
| .replace(/\s+$/u, ''); | ||
| } | ||
|
|
||
| setSelection(selection: ScreenSelection | null): void { | ||
| this.controller.setSelection(selection); | ||
| } | ||
|
|
||
| subscribe(listener: (frame: ReadonlyFrame) => void): () => void { | ||
| return this.controller.subscribe(listener); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the screen buffer for the given output stream, or `undefined` if no | ||
| * Ink instance is rendering to it (e.g. non-TTY or before render). | ||
| */ | ||
| export function getScreenBuffer( | ||
| stdout: NodeJS.WriteStream, | ||
| ): ScreenBuffer | undefined { | ||
| const controller = getFrameController(stdout); | ||
| return controller ? new ScreenBuffer(controller) : undefined; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.