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

Commit

Permalink
feat: add custom limited input component
Browse files Browse the repository at this point in the history
  • Loading branch information
brokenmass committed Oct 20, 2016
1 parent 18ccbd9 commit 1ba869a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"type": "git",
"url": "git@github.com:MailOnline/mol-conventional-changelog.git"
},
"license": "UNLICENSED"
"license": "UNLICENSED",
"dependencies": {
"inquirer": "^1.2.2"
}
}
53 changes: 53 additions & 0 deletions prompt/LimitedInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const inquirer = require('inquirer');
const util = require('util');

function LimitedInput() {
inquirer.prompt.prompts.input.apply(this, arguments);

if (!this.opt.maxLength) {
this.throwParamError('maxLength');
}
this.opt._message = 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();

console.log(this.leadingLength)
}



util.inherits(LimitedInput, inquirer.prompt.prompts.input);

LimitedInput.prototype.updateMessage = function(e) {
this.opt.message = `${this.opt._message}
[${this.spacer}] ${this.remainingChar()} remaining chars
${this.leadingLabel}`;
};

LimitedInput.prototype.remainingChar = function(e) {
return (this.opt.maxLength - this.leadingLength) - this.rl.line.length;
};

LimitedInput.prototype.onKeypress = function(e) {
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;

0 comments on commit 1ba869a

Please sign in to comment.