a4939c0dbd24547ae9816e9a1da82ba8b4e54356fffcc82f3b8970df89640048671fca170a110a8826fceec2b1b2e5cc397e631e56b3a148defba8cfc688f5 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @since v17.0.0
  3. */
  4. declare module "readline/promises" {
  5. import { Abortable } from "node:events";
  6. import {
  7. CompleterResult,
  8. Direction,
  9. Interface as _Interface,
  10. ReadLineOptions as _ReadLineOptions,
  11. } from "node:readline";
  12. /**
  13. * Instances of the `readlinePromises.Interface` class are constructed using the `readlinePromises.createInterface()` method. Every instance is associated with a
  14. * single `input` `Readable` stream and a single `output` `Writable` stream.
  15. * The `output` stream is used to print prompts for user input that arrives on,
  16. * and is read from, the `input` stream.
  17. * @since v17.0.0
  18. */
  19. class Interface extends _Interface {
  20. /**
  21. * The `rl.question()` method displays the `query` by writing it to the `output`,
  22. * waits for user input to be provided on `input`, then invokes the `callback` function passing the provided input as the first argument.
  23. *
  24. * When called, `rl.question()` will resume the `input` stream if it has been
  25. * paused.
  26. *
  27. * If the `Interface` was created with `output` set to `null` or `undefined` the `query` is not written.
  28. *
  29. * If the question is called after `rl.close()`, it returns a rejected promise.
  30. *
  31. * Example usage:
  32. *
  33. * ```js
  34. * const answer = await rl.question('What is your favorite food? ');
  35. * console.log(`Oh, so your favorite food is ${answer}`);
  36. * ```
  37. *
  38. * Using an `AbortSignal` to cancel a question.
  39. *
  40. * ```js
  41. * const signal = AbortSignal.timeout(10_000);
  42. *
  43. * signal.addEventListener('abort', () => {
  44. * console.log('The food question timed out');
  45. * }, { once: true });
  46. *
  47. * const answer = await rl.question('What is your favorite food? ', { signal });
  48. * console.log(`Oh, so your favorite food is ${answer}`);
  49. * ```
  50. * @since v17.0.0
  51. * @param query A statement or query to write to `output`, prepended to the prompt.
  52. * @return A promise that is fulfilled with the user's input in response to the `query`.
  53. */
  54. question(query: string): Promise<string>;
  55. question(query: string, options: Abortable): Promise<string>;
  56. }
  57. /**
  58. * @since v17.0.0
  59. */
  60. class Readline {
  61. /**
  62. * @param stream A TTY stream.
  63. */
  64. constructor(
  65. stream: NodeJS.WritableStream,
  66. options?: {
  67. autoCommit?: boolean;
  68. },
  69. );
  70. /**
  71. * The `rl.clearLine()` method adds to the internal list of pending action an
  72. * action that clears current line of the associated `stream` in a specified
  73. * direction identified by `dir`.
  74. * Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` was passed to the constructor.
  75. * @since v17.0.0
  76. * @return this
  77. */
  78. clearLine(dir: Direction): this;
  79. /**
  80. * The `rl.clearScreenDown()` method adds to the internal list of pending action an
  81. * action that clears the associated stream from the current position of the
  82. * cursor down.
  83. * Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` was passed to the constructor.
  84. * @since v17.0.0
  85. * @return this
  86. */
  87. clearScreenDown(): this;
  88. /**
  89. * The `rl.commit()` method sends all the pending actions to the associated `stream` and clears the internal list of pending actions.
  90. * @since v17.0.0
  91. */
  92. commit(): Promise<void>;
  93. /**
  94. * The `rl.cursorTo()` method adds to the internal list of pending action an action
  95. * that moves cursor to the specified position in the associated `stream`.
  96. * Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` was passed to the constructor.
  97. * @since v17.0.0
  98. * @return this
  99. */
  100. cursorTo(x: number, y?: number): this;
  101. /**
  102. * The `rl.moveCursor()` method adds to the internal list of pending action an
  103. * action that moves the cursor _relative_ to its current position in the
  104. * associated `stream`.
  105. * Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` was passed to the constructor.
  106. * @since v17.0.0
  107. * @return this
  108. */
  109. moveCursor(dx: number, dy: number): this;
  110. /**
  111. * The `rl.rollback` methods clears the internal list of pending actions without
  112. * sending it to the associated `stream`.
  113. * @since v17.0.0
  114. * @return this
  115. */
  116. rollback(): this;
  117. }
  118. type Completer = (line: string) => CompleterResult | Promise<CompleterResult>;
  119. interface ReadLineOptions extends Omit<_ReadLineOptions, "completer"> {
  120. /**
  121. * An optional function used for Tab autocompletion.
  122. */
  123. completer?: Completer | undefined;
  124. }
  125. /**
  126. * The `readlinePromises.createInterface()` method creates a new `readlinePromises.Interface` instance.
  127. *
  128. * ```js
  129. * import readlinePromises from 'node:readline/promises';
  130. * const rl = readlinePromises.createInterface({
  131. * input: process.stdin,
  132. * output: process.stdout,
  133. * });
  134. * ```
  135. *
  136. * Once the `readlinePromises.Interface` instance is created, the most common case
  137. * is to listen for the `'line'` event:
  138. *
  139. * ```js
  140. * rl.on('line', (line) => {
  141. * console.log(`Received: ${line}`);
  142. * });
  143. * ```
  144. *
  145. * If `terminal` is `true` for this instance then the `output` stream will get
  146. * the best compatibility if it defines an `output.columns` property and emits
  147. * a `'resize'` event on the `output` if or when the columns ever change
  148. * (`process.stdout` does this automatically when it is a TTY).
  149. * @since v17.0.0
  150. */
  151. function createInterface(
  152. input: NodeJS.ReadableStream,
  153. output?: NodeJS.WritableStream,
  154. completer?: Completer,
  155. terminal?: boolean,
  156. ): Interface;
  157. function createInterface(options: ReadLineOptions): Interface;
  158. }
  159. declare module "node:readline/promises" {
  160. export * from "readline/promises";
  161. }