UNPKG

@ayonli/jsext

Version:

A JavaScript extension package for building strong and modern applications.

152 lines (147 loc) 5.78 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var bytes = require('../../bytes.js'); var string = require('../../string.js'); require('../../env.js'); require('../../external/event-target-polyfill/index.js'); require('../../path.js'); var cli_constants = require('../../cli/constants.js'); var cli_common = require('../../cli/common.js'); const { BS, CTRL_A, CTRL_C, CTRL_E, CR, DEL, ESC, LF } = cli_constants.ControlKeys; const { UP, DOWN, LEFT, RIGHT } = cli_constants.NavigationKeys; const { CLR_RIGHT } = cli_constants.ControlSequences; function getMasks(mask, length) { return new Array(length).fill(mask).join(""); } async function question(message, options = {}) { const { defaultValue = "", mask } = options; const buf = []; let cursor = 0; await cli_common.writeStdout(bytes.default(message)); if (defaultValue) { const _chars = string.chars(defaultValue); buf.push(..._chars); cursor += _chars.length; if (mask === undefined) { await cli_common.writeStdout(bytes.default(defaultValue)); } else if (mask) { await cli_common.writeStdout(bytes.default(getMasks(mask, _chars.length))); } } while (true) { const input = await cli_common.readStdin(); if (!input.length || bytes.equals(input, UP) || bytes.equals(input, DOWN)) { continue; } else if (bytes.equals(input, LEFT)) { if (cursor > 0) { const char = buf[--cursor]; if (mask === undefined) { await cli_common.moveLeftBy(char); } else if (mask) { await cli_common.moveLeftBy(mask); } } } else if (bytes.equals(input, RIGHT)) { if (cursor < buf.length) { const char = buf[cursor++]; if (mask === undefined) { await cli_common.moveRightBy(char); } else if (mask) { await cli_common.moveRightBy(mask); } } } else if (bytes.equals(input, CTRL_A)) { const left = buf.slice(0, cursor); if (left.length) { cursor = 0; if (mask === undefined) { await cli_common.moveLeftBy(left.join("")); } else if (mask) { await cli_common.moveLeftBy(getMasks(mask, left.length)); } } } else if (bytes.equals(input, CTRL_E)) { const right = buf.slice(cursor); if (right.length) { cursor = buf.length; if (mask === undefined) { await cli_common.moveRightBy(right.join("")); } else if (mask) { await cli_common.moveRightBy(getMasks(mask, right.length)); } } } else if (bytes.equals(input, ESC) || bytes.equals(input, CTRL_C)) { await cli_common.writeStdout(LF); return null; } else if (bytes.equals(input, CR) || bytes.equals(input, LF)) { await cli_common.writeStdout(LF); return buf.join(""); } else if (bytes.equals(input, BS) || bytes.equals(input, DEL)) { if (cursor > 0) { cursor--; const [char] = buf.splice(cursor, 1); const rest = buf.slice(cursor); if (mask === undefined) { await cli_common.moveLeftBy(char); await cli_common.writeStdout(CLR_RIGHT); if (rest.length) { const output = rest.join(""); await cli_common.writeStdout(bytes.default(output)); await cli_common.moveLeftBy(output); } } else if (mask) { await cli_common.moveLeftBy(mask); await cli_common.writeStdout(CLR_RIGHT); if (rest.length) { const output = getMasks(mask, rest.length); await cli_common.writeStdout(bytes.default(output)); await cli_common.moveLeftBy(output); } } } } else if (cli_common.isTypingInput(input)) { const _chars = string.chars(String(input)); if (cursor === buf.length) { buf.push(..._chars); cursor += _chars.length; if (mask === undefined) { await cli_common.writeStdout(input); } else if (mask) { await cli_common.writeStdout(bytes.default(getMasks(mask, _chars.length))); } } else { buf.splice(cursor, 0, ..._chars); cursor += _chars.length; if (mask === undefined) { const rest = buf.slice(cursor).join(""); await cli_common.writeStdout(bytes.concat(input, bytes.default(rest))); await cli_common.moveLeftBy(rest); } else if (mask) { const output = getMasks(mask, _chars.length); const rest = getMasks(mask, buf.slice(cursor).length); await cli_common.writeStdout(bytes.default(output + rest)); await cli_common.moveLeftBy(rest); } } } } } exports.default = question; //# sourceMappingURL=question.js.map