UNPKG

cli-select

Version:

Simple and interactive solution to provide a list of selectable items on the command line

78 lines (63 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _readline = _interopRequireDefault(require("readline")); var _ansiEscapes = require("ansi-escapes"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Response renderer */ class Renderer { /** * Renderer constructor * * @param {object} options - renderer options * @param {any} stream - stream to write to (optional) */ constructor(options, stream = process.stdout) { this.options = options; this.stream = stream; this.values = []; this.initialRender = true; } /** * Set the available values * * @param {array} values - all available values */ setValues(values) { this.values = values; } /** * Render the values * * @param {number} selectedValue - selected value (optional) */ render(selectedValue = 0) { if (this.initialRender) { // hide the cursor initially this.initialRender = false; this.stream.write(_ansiEscapes.cursorHide); } else { // remove previous lines and values this.stream.write((0, _ansiEscapes.eraseLines)(this.values.length)); } // output the current values this.values.forEach((value, index) => { const symbol = selectedValue === index ? this.options.selected : this.options.unselected; const indentation = ' '.repeat(this.options.indentation); const renderedValue = this.options.valueRenderer(value, selectedValue === index); const end = index !== this.values.length - 1 ? '\n' : ''; this.stream.write(indentation + symbol + ' ' + renderedValue + end); }); } /** * Cleanup the console at the end */ cleanup() { this.stream.write((0, _ansiEscapes.eraseLines)(this.values.length)); this.stream.write(_ansiEscapes.cursorShow); } } exports.default = Renderer;