aboutsummaryrefslogtreecommitdiff
path: root/sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js
diff options
context:
space:
mode:
authorDanijel Andjelkovic <adanijel99@gmail.com>2022-03-01 21:54:41 +0100
committerDanijel Andjelkovic <adanijel99@gmail.com>2022-03-01 21:54:41 +0100
commit6c8128f9fd5a5d0be115806c35a21b3d683df8d6 (patch)
treef46c2f6b3b9b294ff32bd75c08ccdc9e7a8cc4ef /sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js
parent2400b84e95913665da6279114168148444b8f9ab (diff)
parent7d3640f824f46490b47bd95f1c5a16644f712068 (diff)
Merge branch 'dev' of http://gitlab.pmf.kg.ac.rs/igrannonica/neuronstellar into logo
Diffstat (limited to 'sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js')
-rw-r--r--sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js278
1 files changed, 278 insertions, 0 deletions
diff --git a/sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js b/sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js
new file mode 100644
index 00000000..4f60b77e
--- /dev/null
+++ b/sandbox/testAppNevena/Front/node_modules/inquirer/lib/prompts/checkbox.js
@@ -0,0 +1,278 @@
+'use strict';
+/**
+ * `list` type prompt
+ */
+
+const _ = {
+ isArray: require('lodash/isArray'),
+ map: require('lodash/map'),
+ isString: require('lodash/isString'),
+};
+const chalk = require('chalk');
+const cliCursor = require('cli-cursor');
+const figures = require('figures');
+const { map, takeUntil } = require('rxjs/operators');
+const Base = require('./base');
+const observe = require('../utils/events');
+const Paginator = require('../utils/paginator');
+const incrementListIndex = require('../utils/incrementListIndex');
+
+class CheckboxPrompt extends Base {
+ constructor(questions, rl, answers) {
+ super(questions, rl, answers);
+
+ if (!this.opt.choices) {
+ this.throwParamError('choices');
+ }
+
+ if (_.isArray(this.opt.default)) {
+ this.opt.choices.forEach(function (choice) {
+ if (this.opt.default.indexOf(choice.value) >= 0) {
+ choice.checked = true;
+ }
+ }, this);
+ }
+
+ this.pointer = 0;
+
+ // Make sure no default is set (so it won't be printed)
+ this.opt.default = null;
+
+ const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
+ this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
+ }
+
+ /**
+ * Start the Inquiry session
+ * @param {Function} cb Callback when prompt is done
+ * @return {this}
+ */
+
+ _run(cb) {
+ this.done = cb;
+
+ const events = observe(this.rl);
+
+ const validation = this.handleSubmitEvents(
+ events.line.pipe(map(this.getCurrentValue.bind(this)))
+ );
+ validation.success.forEach(this.onEnd.bind(this));
+ validation.error.forEach(this.onError.bind(this));
+
+ events.normalizedUpKey
+ .pipe(takeUntil(validation.success))
+ .forEach(this.onUpKey.bind(this));
+ events.normalizedDownKey
+ .pipe(takeUntil(validation.success))
+ .forEach(this.onDownKey.bind(this));
+ events.numberKey
+ .pipe(takeUntil(validation.success))
+ .forEach(this.onNumberKey.bind(this));
+ events.spaceKey
+ .pipe(takeUntil(validation.success))
+ .forEach(this.onSpaceKey.bind(this));
+ events.aKey.pipe(takeUntil(validation.success)).forEach(this.onAllKey.bind(this));
+ events.iKey.pipe(takeUntil(validation.success)).forEach(this.onInverseKey.bind(this));
+
+ // Init the prompt
+ cliCursor.hide();
+ this.render();
+ this.firstRender = false;
+
+ return this;
+ }
+
+ /**
+ * Render the prompt to screen
+ * @return {CheckboxPrompt} self
+ */
+
+ render(error) {
+ // Render question
+ let message = this.getQuestion();
+ let bottomContent = '';
+
+ if (!this.dontShowHints) {
+ message +=
+ '(Press ' +
+ chalk.cyan.bold('<space>') +
+ ' to select, ' +
+ chalk.cyan.bold('<a>') +
+ ' to toggle all, ' +
+ chalk.cyan.bold('<i>') +
+ ' to invert selection, and ' +
+ chalk.cyan.bold('<enter>') +
+ ' to proceed)';
+ }
+
+ // Render choices or answer depending on the state
+ if (this.status === 'answered') {
+ message += chalk.cyan(this.selection.join(', '));
+ } else {
+ const choicesStr = renderChoices(this.opt.choices, this.pointer);
+ const indexPosition = this.opt.choices.indexOf(
+ this.opt.choices.getChoice(this.pointer)
+ );
+ const realIndexPosition =
+ this.opt.choices.reduce((acc, value, i) => {
+ // Dont count lines past the choice we are looking at
+ if (i > indexPosition) {
+ return acc;
+ }
+ // Add line if it's a separator
+ if (value.type === 'separator') {
+ return acc + 1;
+ }
+
+ let l = value.name;
+ // Non-strings take up one line
+ if (typeof l !== 'string') {
+ return acc + 1;
+ }
+
+ // Calculate lines taken up by string
+ l = l.split('\n');
+ return acc + l.length;
+ }, 0) - 1;
+ message +=
+ '\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
+ }
+
+ if (error) {
+ bottomContent = chalk.red('>> ') + error;
+ }
+
+ this.screen.render(message, bottomContent);
+ }
+
+ /**
+ * When user press `enter` key
+ */
+
+ onEnd(state) {
+ this.status = 'answered';
+ this.dontShowHints = true;
+ // Rerender prompt (and clean subline error)
+ this.render();
+
+ this.screen.done();
+ cliCursor.show();
+ this.done(state.value);
+ }
+
+ onError(state) {
+ this.render(state.isValid);
+ }
+
+ getCurrentValue() {
+ const choices = this.opt.choices.filter(
+ (choice) => Boolean(choice.checked) && !choice.disabled
+ );
+
+ this.selection = _.map(choices, 'short');
+ return _.map(choices, 'value');
+ }
+
+ onUpKey() {
+ this.pointer = incrementListIndex(this.pointer, 'up', this.opt);
+ this.render();
+ }
+
+ onDownKey() {
+ this.pointer = incrementListIndex(this.pointer, 'down', this.opt);
+ this.render();
+ }
+
+ onNumberKey(input) {
+ if (input <= this.opt.choices.realLength) {
+ this.pointer = input - 1;
+ this.toggleChoice(this.pointer);
+ }
+
+ this.render();
+ }
+
+ onSpaceKey() {
+ this.toggleChoice(this.pointer);
+ this.render();
+ }
+
+ onAllKey() {
+ const shouldBeChecked = Boolean(
+ this.opt.choices.find((choice) => choice.type !== 'separator' && !choice.checked)
+ );
+
+ this.opt.choices.forEach((choice) => {
+ if (choice.type !== 'separator') {
+ choice.checked = shouldBeChecked;
+ }
+ });
+
+ this.render();
+ }
+
+ onInverseKey() {
+ this.opt.choices.forEach((choice) => {
+ if (choice.type !== 'separator') {
+ choice.checked = !choice.checked;
+ }
+ });
+
+ this.render();
+ }
+
+ toggleChoice(index) {
+ const item = this.opt.choices.getChoice(index);
+ if (item !== undefined) {
+ this.opt.choices.getChoice(index).checked = !item.checked;
+ }
+ }
+}
+
+/**
+ * Function for rendering checkbox choices
+ * @param {Number} pointer Position of the pointer
+ * @return {String} Rendered content
+ */
+
+function renderChoices(choices, pointer) {
+ let output = '';
+ let separatorOffset = 0;
+
+ choices.forEach((choice, i) => {
+ if (choice.type === 'separator') {
+ separatorOffset++;
+ output += ' ' + choice + '\n';
+ return;
+ }
+
+ if (choice.disabled) {
+ separatorOffset++;
+ output += ' - ' + choice.name;
+ output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
+ } else {
+ const line = getCheckbox(choice.checked) + ' ' + choice.name;
+ if (i - separatorOffset === pointer) {
+ output += chalk.cyan(figures.pointer + line);
+ } else {
+ output += ' ' + line;
+ }
+ }
+
+ output += '\n';
+ });
+
+ return output.replace(/\n$/, '');
+}
+
+/**
+ * Get the checkbox
+ * @param {Boolean} checked - add a X or not to the checkbox
+ * @return {String} Composited checkbox string
+ */
+
+function getCheckbox(checked) {
+ return checked ? chalk.green(figures.radioOn) : figures.radioOff;
+}
+
+module.exports = CheckboxPrompt;