| 66 | } |
| 67 | |
| 68 | export function KeypressProvider({ |
| 69 | children, |
| 70 | kittyProtocolEnabled, |
| 71 | config, |
| 72 | }: { |
| 73 | children: React.ReactNode; |
| 74 | kittyProtocolEnabled: boolean; |
| 75 | config?: Config; |
| 76 | }) { |
| 77 | const { stdin, setRawMode } = useStdin(); |
| 78 | const subscribers = useRef<Set<KeypressHandler>>(new Set()).current; |
| 79 | |
| 80 | const subscribe = useCallback( |
| 81 | (handler: KeypressHandler) => { |
| 82 | subscribers.add(handler); |
| 83 | }, |
| 84 | [subscribers], |
| 85 | ); |
| 86 | |
| 87 | const unsubscribe = useCallback( |
| 88 | (handler: KeypressHandler) => { |
| 89 | subscribers.delete(handler); |
| 90 | }, |
| 91 | [subscribers], |
| 92 | ); |
| 93 | |
| 94 | useEffect(() => { |
| 95 | setRawMode(true); |
| 96 | |
| 97 | const keypressStream = new PassThrough(); |
| 98 | let usePassthrough = false; |
| 99 | const nodeMajorVersion = parseInt(process.versions.node.split('.')[0], 10); |
| 100 | if ( |
| 101 | nodeMajorVersion < 20 || |
| 102 | process.env['PASTE_WORKAROUND'] === '1' || |
| 103 | process.env['PASTE_WORKAROUND'] === 'true' |
| 104 | ) { |
| 105 | usePassthrough = true; |
| 106 | } |
| 107 | |
| 108 | let isPaste = false; |
| 109 | let pasteBuffer = Buffer.alloc(0); |
| 110 | let kittySequenceBuffer = ''; |
| 111 | let backslashTimeout: NodeJS.Timeout | null = null; |
| 112 | let waitingForEnterAfterBackslash = false; |
| 113 | |
| 114 | const parseKittySequence = (sequence: string): Key | null => { |
| 115 | const kittyPattern = new RegExp(`^${ESC}\\[(\\d+)(;(\\d+))?([u~])$`); |
| 116 | const match = sequence.match(kittyPattern); |
| 117 | if (!match) return null; |
| 118 | |
| 119 | const keyCode = parseInt(match[1], 10); |
| 120 | const modifiers = match[3] ? parseInt(match[3], 10) : 1; |
| 121 | const modifierBits = modifiers - 1; |
| 122 | const shift = (modifierBits & 1) === 1; |
| 123 | const alt = (modifierBits & 2) === 2; |
| 124 | const ctrl = (modifierBits & 4) === 4; |
| 125 | |