From 065bfae81c78ac0e96cfd06a4dadf8b4d602aa6e Mon Sep 17 00:00:00 2001 From: Va Da Date: Mon, 4 Jun 2018 22:41:29 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20make=20LimitedInput=20wo?= =?UTF-8?q?rk=20with=20new=20inquirer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/LimitedInput.js | 49 ------------------------- lib/LimitedInputPrompt.js | 76 +++++++++++++++++++++++++++++++++++++++ lib/createPrompter.js | 4 +-- package.json | 2 +- 4 files changed, 79 insertions(+), 52 deletions(-) delete mode 100644 lib/LimitedInput.js create mode 100644 lib/LimitedInputPrompt.js diff --git a/lib/LimitedInput.js b/lib/LimitedInput.js deleted file mode 100644 index ef3f8089..00000000 --- a/lib/LimitedInput.js +++ /dev/null @@ -1,49 +0,0 @@ -const util = require('util'); -const inquirer = require('inquirer'); - -const LimitedInput = function (...args) { - inquirer.prompt.prompts.input.apply(this, args); - - if (!this.opt.maxLength) { - this.throwParamError('maxLength'); - } - this.originalMessage = this.opt.message; - this.spacer = new Array(this.opt.maxLength).fill('-').join(''); - - if (this.opt.leadingLabel) { - if (typeof this.opt.leadingLabel === 'function') { - this.leadingLabel = ' ' + this.opt.leadingLabel(this.answers); - } else { - this.leadingLabel = ' ' + this.opt.leadingLabel; - } - } else { - this.leadingLabel = ''; - } - - this.leadingLength = this.leadingLabel.length; - this.updateMessage(); -}; - -util.inherits(LimitedInput, inquirer.prompt.prompts.input); - -LimitedInput.prototype.updateMessage = function () { - this.opt.message = `${this.originalMessage} - [${this.spacer}] ${this.remainingChar()} remaining chars - ${this.leadingLabel}`; -}; - -LimitedInput.prototype.remainingChar = function () { - return this.opt.maxLength - this.leadingLength - this.rl.line.length; -}; - -LimitedInput.prototype.onKeypress = function () { - if (this.rl.line.length > this.opt.maxLength - this.leadingLength) { - this.rl.line = this.rl.line.slice(0, this.opt.maxLength - this.leadingLength); - this.rl.cursor--; - } - - this.updateMessage(); - this.render(); -}; - -module.exports = LimitedInput; diff --git a/lib/LimitedInputPrompt.js b/lib/LimitedInputPrompt.js new file mode 100644 index 00000000..97d63aad --- /dev/null +++ b/lib/LimitedInputPrompt.js @@ -0,0 +1,76 @@ +const chalk = require('chalk'); +const InputPrompt = require('inquirer/lib/prompts/input'); + +class LimitedInputPrompt extends InputPrompt { + constructor (...args) { + super(...args); + + if (!this.opt.maxLength) { + this.throwParamError('maxLength'); + } + this.originalMessage = this.opt.message; + this.spacer = new Array(this.opt.maxLength).fill('-').join(''); + + if (this.opt.leadingLabel) { + if (typeof this.opt.leadingLabel === 'function') { + this.leadingLabel = ' ' + this.opt.leadingLabel(this.answers); + } else { + this.leadingLabel = ' ' + this.opt.leadingLabel; + } + } else { + this.leadingLabel = ''; + } + + this.leadingLength = this.leadingLabel.length; + } + + remainingChar () { + return this.opt.maxLength - this.leadingLength - this.rl.line.length; + } + + onKeypress () { + if (this.rl.line.length > this.opt.maxLength - this.leadingLength) { + this.rl.line = this.rl.line.slice(0, this.opt.maxLength - this.leadingLength); + this.rl.cursor--; + } + + this.render(); + } + + getCharsLeftText () { + const chars = this.remainingChar(); + + if (chars > 15) { + return chalk.green(`${chars} chars left`); + } else if (chars > 5) { + return chalk.yellow(`${chars} chars left`); + } else { + return chalk.red(`${chars} chars left`); + } + } + + render (error) { + let bottomContent = ''; + let message = this.getQuestion(); + let appendContent = ''; + const isFinal = this.status === 'answered'; + + if (isFinal) { + appendContent = this.answer; + } else { + appendContent = this.rl.line; + } + + message = `${message} + [${this.spacer}] ${this.getCharsLeftText()} + ${this.leadingLabel} ${appendContent}`; + + if (error) { + bottomContent = chalk.red('>> ') + error; + } + + this.screen.render(message, bottomContent); + } +} + +module.exports = LimitedInputPrompt; diff --git a/lib/createPrompter.js b/lib/createPrompter.js index 8e580012..6363ee22 100644 --- a/lib/createPrompter.js +++ b/lib/createPrompter.js @@ -6,7 +6,7 @@ const { createPackagesQuestion, createQuestions } = require('./questions'); -const LimitedInput = require('./LimitedInput'); +const LimitedInputPrompt = require('./LimitedInputPrompt'); const { getAllPackages, getChangedPackages @@ -14,7 +14,7 @@ const { const MAX_LINE_WIDTH = 72; -inquirer.registerPrompt('limitedInput', LimitedInput); +inquirer.registerPrompt('limitedInput', LimitedInputPrompt); const IS_LERNA_PROJECT = fs.existsSync(appRoot.resolve('lerna.json')); diff --git a/package.json b/package.json index 5317bf58..0a3b37bc 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "license": "Unlicense", "dependencies": { "app-root-path": "^2.0.1", - "inquirer": "^3.1.1", + "inquirer": "^6.0.0", "word-wrap": "^1.2.3", "pad-right": "^0.2.2", "signale": "^1.1.0",