MCPcopy
hub / github.com/codeaashu/claude-code / useSelection

Function useSelection

src/ink/hooks/use-selection.ts:14–87  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12 * Returns no-op functions when fullscreen mode is disabled.
13 */
14export function useSelection(): {
15 copySelection: () => string
16 /** Copy without clearing the highlight (for copy-on-select). */
17 copySelectionNoClear: () => string
18 clearSelection: () => void
19 hasSelection: () => boolean
20 /** Read the raw mutable selection state (for drag-to-scroll). */
21 getState: () => SelectionState | null
22 /** Subscribe to selection mutations (start/update/finish/clear). */
23 subscribe: (cb: () => void) => () => void
24 /** Shift the anchor row by dRow, clamped to [minRow, maxRow]. */
25 shiftAnchor: (dRow: number, minRow: number, maxRow: number) => void
26 /** Shift anchor AND focus by dRow (keyboard scroll: whole selection
27 * tracks content). Clamped points get col reset to the full-width edge
28 * since their content was captured by captureScrolledRows. Reads
29 * screen.width from the ink instance for the col-reset boundary. */
30 shiftSelection: (dRow: number, minRow: number, maxRow: number) => void
31 /** Keyboard selection extension (shift+arrow): move focus, anchor fixed.
32 * Left/right wrap across rows; up/down clamp at viewport edges. */
33 moveFocus: (move: FocusMove) => void
34 /** Capture text from rows about to scroll out of the viewport (call
35 * BEFORE scrollBy so the screen buffer still has the outgoing rows). */
36 captureScrolledRows: (
37 firstRow: number,
38 lastRow: number,
39 side: 'above' | 'below',
40 ) => void
41 /** Set the selection highlight bg color (theme-piping; solid bg
42 * replaces the old SGR-7 inverse so syntax highlighting stays readable
43 * under selection). Call once on mount + whenever theme changes. */
44 setSelectionBgColor: (color: string) => void
45} {
46 // Look up the Ink instance via stdout — same pattern as instances map.
47 // StdinContext is available (it's always provided), and the Ink instance
48 // is keyed by stdout which we can get from process.stdout since there's
49 // only one Ink instance per process in practice.
50 useContext(StdinContext) // anchor to App subtree for hook rules
51 const ink = instances.get(process.stdout)
52 // Memoize so callers can safely use the return value in dependency arrays.
53 // ink is a singleton per stdout — stable across renders.
54 return useMemo(() => {
55 if (!ink) {
56 return {
57 copySelection: () => '',
58 copySelectionNoClear: () => '',
59 clearSelection: () => {},
60 hasSelection: () => false,
61 getState: () => null,
62 subscribe: () => () => {},
63 shiftAnchor: () => {},
64 shiftSelection: () => {},
65 moveFocus: () => {},
66 captureScrolledRows: () => {},
67 setSelectionBgColor: () => {},
68 }
69 }
70 return {
71 copySelection: () => ink.copySelection(),

Callers 2

ScrollKeybindingHandlerFunction · 0.85
ModeIndicatorFunction · 0.85

Calls 11

shiftAnchorFunction · 0.85
copySelectionMethod · 0.80
copySelectionNoClearMethod · 0.80
clearTextSelectionMethod · 0.80
hasTextSelectionMethod · 0.80
moveSelectionFocusMethod · 0.80
captureScrolledRowsMethod · 0.80
setSelectionBgColorMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected