diff options
| author | Billy Biggs <[email protected]> | 2025-06-27 10:57:32 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-27 17:57:32 +0000 |
| commit | 4fbffdf617b2fb87c1b663391fbe488c5c81beb8 (patch) | |
| tree | aa0f984ccd95016693a52826ff11e6b30bc422c7 /packages/cli/src/ui/hooks/useBracketedPaste.ts | |
| parent | 5fd6664c4b1c4a1ca84119ea709e5dac2a9fce70 (diff) | |
Handle stdin for prompts using readline for escape character parsing (#1972)
Diffstat (limited to 'packages/cli/src/ui/hooks/useBracketedPaste.ts')
| -rw-r--r-- | packages/cli/src/ui/hooks/useBracketedPaste.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/cli/src/ui/hooks/useBracketedPaste.ts b/packages/cli/src/ui/hooks/useBracketedPaste.ts new file mode 100644 index 00000000..ae58be3b --- /dev/null +++ b/packages/cli/src/ui/hooks/useBracketedPaste.ts @@ -0,0 +1,37 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useEffect } from 'react'; + +const ENABLE_BRACKETED_PASTE = '\x1b[?2004h'; +const DISABLE_BRACKETED_PASTE = '\x1b[?2004l'; + +/** + * Enables and disables bracketed paste mode in the terminal. + * + * This hook ensures that bracketed paste mode is enabled when the component + * mounts and disabled when it unmounts or when the process exits. + */ +export const useBracketedPaste = () => { + const cleanup = () => { + process.stdout.write(DISABLE_BRACKETED_PASTE); + }; + + useEffect(() => { + process.stdout.write(ENABLE_BRACKETED_PASTE); + + process.on('exit', cleanup); + process.on('SIGINT', cleanup); + process.on('SIGTERM', cleanup); + + return () => { + cleanup(); + process.removeListener('exit', cleanup); + process.removeListener('SIGINT', cleanup); + process.removeListener('SIGTERM', cleanup); + }; + }, []); +}; |
