/ ink / components / StdinContext.ts
StdinContext.ts
 1  import { createContext } from 'react'
 2  import { EventEmitter } from '../events/emitter.js'
 3  import type { TerminalQuerier } from '../terminal-querier.js'
 4  
 5  export type Props = {
 6    /**
 7     * Stdin stream passed to `render()` in `options.stdin` or `process.stdin` by default. Useful if your app needs to handle user input.
 8     */
 9    readonly stdin: NodeJS.ReadStream
10  
11    /**
12     * Ink exposes this function via own `<StdinContext>` to be able to handle Ctrl+C, that's why you should use Ink's `setRawMode` instead of `process.stdin.setRawMode`.
13     * If the `stdin` stream passed to Ink does not support setRawMode, this function does nothing.
14     */
15    readonly setRawMode: (value: boolean) => void
16  
17    /**
18     * A boolean flag determining if the current `stdin` supports `setRawMode`. A component using `setRawMode` might want to use `isRawModeSupported` to nicely fall back in environments where raw mode is not supported.
19     */
20    readonly isRawModeSupported: boolean
21  
22    readonly internal_exitOnCtrlC: boolean
23  
24    readonly internal_eventEmitter: EventEmitter
25  
26    /** Query the terminal and await responses (DECRQM, OSC 11, etc.).
27     *  Null only in the never-reached default context value. */
28    readonly internal_querier: TerminalQuerier | null
29  }
30  
31  /**
32   * `StdinContext` is a React context, which exposes input stream.
33   */
34  
35  const StdinContext = createContext<Props>({
36    stdin: process.stdin,
37  
38    internal_eventEmitter: new EventEmitter(),
39    setRawMode() {},
40    isRawModeSupported: false,
41  
42    internal_exitOnCtrlC: true,
43    internal_querier: null,
44  })
45  
46  // eslint-disable-next-line custom-rules/no-top-level-side-effects
47  StdinContext.displayName = 'InternalStdinContext'
48  
49  export default StdinContext