Skip to content

Commit

Permalink
Merge pull request #232 from milesj/index-view
Browse files Browse the repository at this point in the history
Add descriptors support: title, version, banner
  • Loading branch information
dthree authored May 19, 2017
2 parents 8bb46ad + e1e296f commit 604a966
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dist/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var UI = function (_EventEmitter) {
_this._sigintCount = 0;
_this._sigint = function () {
if (_this._sigintCount > 1) {
_this.parent.emit('vorpal_exit');
process.exit(0);
} else {
var text = _this.input();
Expand Down Expand Up @@ -141,7 +142,7 @@ var UI = function (_EventEmitter) {
var render = inquirer.prompt.prompts[promptType].prototype.render;
inquirer.prompt.prompts[promptType].prototype.render = function () {
self._activePrompt = this;
return render.call(this);
return render.apply(this, arguments);
};
};

Expand Down
12 changes: 12 additions & 0 deletions dist/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,18 @@ var util = {
return str + Array(len + 1).join(delimiter);
},

/**
* Pad a row on the start and end with spaces.
*
* @param {String} str
* @return {String}
*/
padRow: function padRow(str) {
return str.split('\n').map(function (row) {
return ' ' + row + ' ';
}).join('\n');
},

// When passing down applied args, we need to turn
// them from `{ '0': 'foo', '1': 'bar' }` into ['foo', 'bar']
// instead.
Expand Down
84 changes: 83 additions & 1 deletion dist/vorpal.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ function Vorpal() {
// Exposed through vorpal.version(str);
this._version = '';

// Program title
this._title = '';

// Program description
this._description = '';

// Program baner
this._banner = '';

// Command line history instance
this.cmdHistory = new this.CmdHistoryExtension();

Expand Down Expand Up @@ -187,6 +196,45 @@ vorpal.version = function (version) {
return this;
};

/**
* Sets the title of your application.
*
* @param {String} title
* @return {Vorpal}
* @api public
*/

vorpal.title = function (title) {
this._title = title;
return this;
};

/**
* Sets the description of your application.
*
* @param {String} description
* @return {Vorpal}
* @api public
*/

vorpal.description = function (description) {
this._description = description;
return this;
};

/**
* Sets the banner of your application.
*
* @param {String} banner
* @return {Vorpal}
* @api public
*/

vorpal.banner = function (banner) {
this._banner = banner;
return this;
};

/**
* Sets the permanent delimiter for this
* Vorpal server instance.
Expand Down Expand Up @@ -1125,11 +1173,44 @@ vorpal._commandHelp = function (command) {

var groupsString = groups.length < 1 ? '' : ' Command Groups:\n\n' + groups.join('\n') + '\n';

var results = String(invalidString + commandsString + '\n' + groupsString).replace(/\n\n\n/g, '\n\n').replace(/\n\n$/, '\n');
var results = String(this._helpHeader(!!invalidString) + invalidString + commandsString + '\n' + groupsString).replace(/\n\n\n/g, '\n\n').replace(/\n\n$/, '\n');

return results;
};

vorpal._helpHeader = function (hideTitle) {
var header = [];

if (this._banner) {
header.push(VorpalUtil.padRow(this._banner), '');
}

// Only show under specific conditions
if (this._title && !hideTitle) {
var title = this._title;

if (this._version) {
title += ' v' + this._version;
}

header.push(VorpalUtil.padRow(title));

if (this._description) {
var descWidth = process.stdout.columns * 0.75; // Only 75% of the screen

header.push(VorpalUtil.padRow(wrap(this._description, descWidth)));
}
}

// Pad the top and bottom
if (header.length) {
header.unshift('');
header.push('');
}

return header.join('\n');
};

/**
* Abstracts the logic for sending and
* receiving sockets upstream and downstream.
Expand Down Expand Up @@ -1229,6 +1310,7 @@ vorpal.getSessionById = function (id) {

vorpal.exit = function (options) {
var ssn = this.getSessionById(options.sessionId);
this.emit('vorpal_exit');
if (ssn.isLocal()) {
process.exit(0);
} else {
Expand Down
51 changes: 51 additions & 0 deletions examples/descriptors/descriptors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var vorpal = require('./../../')();
var chalk = vorpal.chalk;

vorpal
.title(chalk.magenta('Vorpal'))
.version('1.4.0')
.description(chalk.cyan('Conquer the command-line.'))
.banner(chalk.gray(` (O)
<M
o <M
/| ...... /:M\\------------------------------------------------,,,,,,
(O)[ vorpal ]::@+}==========================================------------>
\\| ^^^^^^ \\:W/------------------------------------------------''''''
o <W
<W
(O)`));

vorpal.command('build', 'Build the application.')
.option('-d')
.option('-a')
.action(function (args, cb) {
this.log(args);
cb();
});

vorpal.command('clean', 'Clean the local workspace.')
.option('-f')
.action(function (args, cb) {
this.log(args);
cb();
});

vorpal.command('compress', 'Compress assets.')
.option('-gzip')
.action(function (args, cb) {
this.log(args);
cb();
});

vorpal
.catch('', 'Displays the index view.')
.action(function (args, cb) {
this.log(this.parent._commandHelp(args.command));
cb();
});

vorpal
.delimiter('vorpal:')
.parse(process.argv);
12 changes: 12 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ const util = {
return str + Array(len + 1).join(delimiter);
},

/**
* Pad a row on the start and end with spaces.
*
* @param {String} str
* @return {String}
*/
padRow: function (str) {
return str.split('\n').map(function (row) {
return ' ' + row + ' ';
}).join('\n');
},

// When passing down applied args, we need to turn
// them from `{ '0': 'foo', '1': 'bar' }` into ['foo', 'bar']
// instead.
Expand Down
90 changes: 89 additions & 1 deletion lib/vorpal.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ function Vorpal() {
// Exposed through vorpal.version(str);
this._version = '';

// Program title
this._title = '';

// Program description
this._description = '';

// Program baner
this._banner = '';

// Command line history instance
this.cmdHistory = new this.CmdHistoryExtension();

Expand Down Expand Up @@ -187,6 +196,45 @@ vorpal.version = function (version) {
return this;
};

/**
* Sets the title of your application.
*
* @param {String} title
* @return {Vorpal}
* @api public
*/

vorpal.title = function (title) {
this._title = title;
return this;
};

/**
* Sets the description of your application.
*
* @param {String} description
* @return {Vorpal}
* @api public
*/

vorpal.description = function (description) {
this._description = description;
return this;
};

/**
* Sets the banner of your application.
*
* @param {String} banner
* @return {Vorpal}
* @api public
*/

vorpal.banner = function (banner) {
this._banner = banner;
return this;
};

/**
* Sets the permanent delimiter for this
* Vorpal server instance.
Expand Down Expand Up @@ -1138,11 +1186,51 @@ vorpal._commandHelp = function (command) {
'' :
' Command Groups:\n\n' + groups.join('\n') + '\n';

var results = String(invalidString + commandsString + '\n' + groupsString).replace(/\n\n\n/g, '\n\n').replace(/\n\n$/, '\n');
var results = String(
this._helpHeader(!!invalidString) +
invalidString +
commandsString + '\n' +
groupsString
)
.replace(/\n\n\n/g, '\n\n')
.replace(/\n\n$/, '\n');

return results;
};

vorpal._helpHeader = function (hideTitle) {
var header = [];

if (this._banner) {
header.push(VorpalUtil.padRow(this._banner), '');
}

// Only show under specific conditions
if (this._title && !hideTitle) {
var title = this._title;

if (this._version) {
title += ' v' + this._version;
}

header.push(VorpalUtil.padRow(title));

if (this._description) {
var descWidth = process.stdout.columns * 0.75; // Only 75% of the screen

header.push(VorpalUtil.padRow(wrap(this._description, descWidth)));
}
}

// Pad the top and bottom
if (header.length) {
header.unshift('');
header.push('');
}

return header.join('\n');
};

/**
* Abstracts the logic for sending and
* receiving sockets upstream and downstream.
Expand Down
29 changes: 29 additions & 0 deletions test/vorpal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

var Vorpal = require('../');
var should = require('should');
var assert = require('assert');
var intercept = require('../dist/intercept');

var vorpal;
Expand Down Expand Up @@ -348,3 +349,31 @@ describe('help menu', function () {
stdout.should.equal(fixture);
});
});

describe('descriptors', function () {
var instance;

beforeEach(function () {
instance = Vorpal();
});

it('sets the version', function () {
instance.version('1.2.3');
assert.equal(instance._version, '1.2.3');
});

it('sets the title', function () {
instance.title('Vorpal');
assert.equal(instance._title, 'Vorpal');
});

it('sets the description', function () {
instance.description('A CLI tool.');
assert.equal(instance._description, 'A CLI tool.');
});

it('sets the banner', function () {
instance.banner('VORPAL');
assert.equal(instance._banner, 'VORPAL');
});
});

0 comments on commit 604a966

Please sign in to comment.