Skip to content

Commit

Permalink
typings: add JSDoc typings for readline
Browse files Browse the repository at this point in the history
Added JSDoc typings for the `readline` lib module.
  • Loading branch information
VoltrexKeyva authored and Trott committed May 2, 2021
1 parent 68e6673 commit e192a16
Showing 1 changed file with 84 additions and 8 deletions.
92 changes: 84 additions & 8 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ const { StringDecoder } = require('string_decoder');
// Lazy load Readable for startup performance.
let Readable;

/**
* @typedef {import('./stream.js').Readable} Readable
* @typedef {import('./stream.js').Writable} Writable
*/

const kHistorySize = 30;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
Expand All @@ -118,6 +123,27 @@ const kQuestionCancel = Symbol('kQuestionCancel');
// GNU readline library - keyseq-timeout is 500ms (default)
const ESCAPE_CODE_TIMEOUT = 500;

/**
* Creates a new `readline.Interface` instance.
* @param {Readable | {
* input: Readable;
* output: Writable;
* completer?: Function;
* terminal?: boolean;
* history?: string[];
* historySize?: number;
* removeHistoryDuplicates?: boolean;
* prompt?: string;
* crlfDelay?: number;
* escapeCodeTimeout?: number;
* tabSize?: number;
* signal?: AbortSignal;
* }} input
* @param {Writable} [output]
* @param {Function} [completer]
* @param {boolean} [terminal]
* @returns {Interface}
*/
function createInterface(input, output, completer, terminal) {
return new Interface(input, output, completer, terminal);
}
Expand Down Expand Up @@ -348,11 +374,19 @@ ObjectDefineProperty(Interface.prototype, 'columns', {
}
});

/**
* Sets the prompt written to the output.
* @param {string} prompt
* @returns {void}
*/
Interface.prototype.setPrompt = function(prompt) {
this._prompt = prompt;
};


/**
* Returns the current prompt used by `rl.prompt()`.
* @returns {string}
*/
Interface.prototype.getPrompt = function() {
return this._prompt;
};
Expand All @@ -368,7 +402,11 @@ Interface.prototype._setRawMode = function(mode) {
return wasInRawMode;
};


/**
* Writes the configured `prompt` to a new line in `output`.
* @param {boolean} [preserveCursor]
* @returns {void}
*/
Interface.prototype.prompt = function(preserveCursor) {
if (this.paused) this.resume();
if (this.terminal && process.env.TERM !== 'dumb') {
Expand All @@ -379,7 +417,13 @@ Interface.prototype.prompt = function(preserveCursor) {
}
};


/**
* Displays `query` by writing it to the `output`.
* @param {string} query
* @param {{ signal?: AbortSignal; }} [options]
* @param {Function} cb
* @returns {void}
*/
Interface.prototype.question = function(query, options, cb) {
cb = typeof options === 'function' ? options : cb;
options = typeof options === 'object' && options !== null ? options : {};
Expand Down Expand Up @@ -528,7 +572,10 @@ Interface.prototype._refreshLine = function() {
this.prevRows = cursorPos.rows;
};


/**
* Closes the `readline.Interface` instance.
* @returns {void}
*/
Interface.prototype.close = function() {
if (this.closed) return;
this.pause();
Expand All @@ -539,7 +586,10 @@ Interface.prototype.close = function() {
this.emit('close');
};


/**
* Pauses the `input` stream.
* @returns {void | Interface}
*/
Interface.prototype.pause = function() {
if (this.paused) return;
this.input.pause();
Expand All @@ -548,7 +598,10 @@ Interface.prototype.pause = function() {
return this;
};


/**
* Resumes the `input` stream if paused.
* @returns {void | Interface}
*/
Interface.prototype.resume = function() {
if (!this.paused) return;
this.input.resume();
Expand All @@ -557,7 +610,18 @@ Interface.prototype.resume = function() {
return this;
};


/**
* Writes either `data` or a `key` sequence identified by
* `key` to the `output`.
* @param {string} d
* @param {{
* ctrl?: boolean;
* meta?: boolean;
* shift?: boolean;
* name?: string;
* }} [key]
* @returns {void}
*/
Interface.prototype.write = function(d, key) {
if (this.paused) this.resume();
if (this.terminal) {
Expand Down Expand Up @@ -868,7 +932,14 @@ Interface.prototype._getDisplayPos = function(str) {
return { cols, rows };
};

// Returns current cursor's position and line
/**
* Returns the real position of the cursor in relation
* to the input prompt + string.
* @returns {{
* rows: number;
* cols: number;
* }}
*/
Interface.prototype.getCursorPos = function() {
const strBeforeCursor = this._prompt +
StringPrototypeSlice(this.line, 0, this.cursor);
Expand Down Expand Up @@ -1197,6 +1268,11 @@ Interface.prototype._ttyWrite = function(s, key) {
}
};

/**
* Creates an `AsyncIterator` object that iterates through
* each line in the input stream as a string.
* @returns {Symbol.AsyncIterator}
*/
Interface.prototype[SymbolAsyncIterator] = function() {
if (this[kLineObjectStream] === undefined) {
if (Readable === undefined) {
Expand Down

0 comments on commit e192a16

Please sign in to comment.