Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
feat: 🎸 make LimitedInput work with new inquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 4, 2018
1 parent c856b9d commit 065bfae
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 52 deletions.
49 changes: 0 additions & 49 deletions lib/LimitedInput.js

This file was deleted.

76 changes: 76 additions & 0 deletions lib/LimitedInputPrompt.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions lib/createPrompter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const {
createPackagesQuestion,
createQuestions
} = require('./questions');
const LimitedInput = require('./LimitedInput');
const LimitedInputPrompt = require('./LimitedInputPrompt');
const {
getAllPackages,
getChangedPackages
} = require('./lernaUtils');

const MAX_LINE_WIDTH = 72;

inquirer.registerPrompt('limitedInput', LimitedInput);
inquirer.registerPrompt('limitedInput', LimitedInputPrompt);

const IS_LERNA_PROJECT = fs.existsSync(appRoot.resolve('lerna.json'));

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 065bfae

Please sign in to comment.