Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 165 additions & 13 deletions src/bun.js/readline.exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var { Array, RegExp, String, Bun } = import.meta.primordials;
var EventEmitter = import.meta.require("node:events");
var { clearTimeout, setTimeout } = import.meta.require("timers");
var { StringDecoder } = import.meta.require("string_decoder");
var isWritable;

var { inspect } = Bun;
var debug = process.env.BUN_JS_DEBUG ? console.log : () => {};
Expand Down Expand Up @@ -372,6 +373,14 @@ class ERR_USE_AFTER_CLOSE extends NodeError {
}
}

class AbortError extends Error {
code;
constructor() {
super("The operation was aborted");
this.code = "ABORT_ERR";
}
}

// Validators

/**
Expand Down Expand Up @@ -3081,6 +3090,157 @@ function _ttyWriteDumb(s, key) {
}
}

class Readline {
#autoCommit = false;
#stream;
#todo = [];

constructor(stream, options = undefined) {
isWritable ??= import.meta.require("node:stream").isWritable;
if (!isWritable(stream))
throw new ERR_INVALID_ARG_TYPE("stream", "Writable", stream);
this.#stream = stream;
if (options?.autoCommit != null) {
validateBoolean(options.autoCommit, "options.autoCommit");
this.#autoCommit = options.autoCommit;
}
}

/**
* Moves the cursor to the x and y coordinate on the given stream.
* @param {integer} x
* @param {integer} [y]
* @returns {Readline} this
*/
cursorTo(x, y = undefined) {
validateInteger(x, "x");
if (y != null) validateInteger(y, "y");

var data = y == null ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H`;
if (this.#autoCommit) process.nextTick(() => this.#stream.write(data));
else ArrayPrototypePush.call(this.#todo, data);

return this;
}

/**
* Moves the cursor relative to its current location.
* @param {integer} dx
* @param {integer} dy
* @returns {Readline} this
*/
moveCursor(dx, dy) {
if (dx || dy) {
validateInteger(dx, "dx");
validateInteger(dy, "dy");

var data = "";

if (dx < 0) {
data += CSI`${-dx}D`;
} else if (dx > 0) {
data += CSI`${dx}C`;
}

if (dy < 0) {
data += CSI`${-dy}A`;
} else if (dy > 0) {
data += CSI`${dy}B`;
}
if (this.#autoCommit) process.nextTick(() => this.#stream.write(data));
else ArrayPrototypePush.call(this.#todo, data);
}
return this;
}

/**
* Clears the current line the cursor is on.
* @param {-1|0|1} dir Direction to clear:
* -1 for left of the cursor
* +1 for right of the cursor
* 0 for the entire line
* @returns {Readline} this
*/
clearLine(dir) {
validateInteger(dir, "dir", -1, 1);

var data =
dir < 0 ? kClearToLineBeginning : dir > 0 ? kClearToLineEnd : kClearLine;
if (this.#autoCommit) process.nextTick(() => this.#stream.write(data));
else ArrayPrototypePush.call(this.#todo, data);
return this;
}

/**
* Clears the screen from the current position of the cursor down.
* @returns {Readline} this
*/
clearScreenDown() {
if (this.#autoCommit) {
process.nextTick(() => this.#stream.write(kClearScreenDown));
} else {
ArrayPrototypePush.call(this.#todo, kClearScreenDown);
}
return this;
}

/**
* Sends all the pending actions to the associated `stream` and clears the
* internal list of pending actions.
* @returns {Promise<void>} Resolves when all pending actions have been
* flushed to the associated `stream`.
*/
commit() {
return new Promise((resolve) => {
this.#stream.write(ArrayPrototypeJoin.call(this.#todo, ""), resolve);
this.#todo = [];
});
}

/**
* Clears the internal list of pending actions without sending it to the
* associated `stream`.
* @returns {Readline} this
*/
rollback() {
this.#todo = [];
return this;
}
}

var PromisesInterface = class Interface extends _Interface {
// eslint-disable-next-line no-useless-constructor
constructor(input, output, completer, terminal) {
super(input, output, completer, terminal);
}
question(query, options = kEmptyObject) {
return new Promise((resolve, reject) => {
var cb = resolve;

if (options?.signal) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do these validations above the function scope and then return Promise.reject?

validateAbortSignal(options.signal, "options.signal");
if (options.signal.aborted) {
return reject(
new AbortError(undefined, { cause: options.signal.reason }),
);
}

var onAbort = () => {
this[kQuestionCancel]();
reject(new AbortError(undefined, { cause: options.signal.reason }));
};
options.signal.addEventListener("abort", onAbort, { once: true });
cb = (answer) => {
options.signal.removeEventListener("abort", onAbort);
resolve(answer);
};
}

this[kQuestion](query, cb);
});
}
};

// ----------------------------------------------------------------------------
// Exports
// ----------------------------------------------------------------------------
Expand All @@ -3092,7 +3252,11 @@ export var cursorTo = cursorTo;
export var emitKeypressEvents = emitKeypressEvents;
export var moveCursor = moveCursor;
export var promises = {
[SymbolFor("__UNIMPLEMENTED__")]: true,
Readline,
Interface: PromisesInterface,
createInterface(input, output, completer, terminal) {
return new Interface(input, output, completer, terminal);
},
};

export default {
Expand All @@ -3107,22 +3271,10 @@ export default {

[SymbolFor("__BUN_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__")]: {
CSI,
_Interface,
utils: {
getStringWidth,
stripVTControlCharacters,
},
shared: {
kEmptyObject,
validateBoolean,
validateInteger,
validateAbortSignal,
ERR_INVALID_ARG_TYPE,
},
symbols: {
kQuestion,
kQuestionCancel,
},
},
[SymbolFor("CommonJS")]: 0,
};
Loading