Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Adding color overrides. #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/colorOverride.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var chalk = require('chalk');

var parser = require("../nomnom")
.script("runtests")
.setColors({
usageHeadingColor: chalk.green,
usageStringColor: chalk.bgCyan,
positionalHelpColor: chalk.red,
optionsHeaderColor: chalk.cyan,
helpColor: chalk.bgCyan,
requiredArgColor: chalk.magenta
})
.options({
path: {
position: 0,
help: "Test file to run",
list: true
},
config: {
abbr: 'c',
metavar: 'FILE',
help: "Config file with tests to run"
},
debug: {
abbr: 'd',
flag: true,
help: "Print debugging info"
}
});


var opts = parser.parse();

console.log('\nopts:\n', JSON.stringify(opts, undefined, 4));
24 changes: 24 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var parser = require("../nomnom")
.script("runtests")
.options({
path: {
position: 0,
help: "Test file to run",
list: true
},
config: {
abbr: 'c',
metavar: 'FILE',
help: "Config file with tests to run"
},
debug: {
abbr: 'd',
flag: true,
help: "Print debugging info"
}
});


var opts = parser.parse();

console.log('\nopts:\n', JSON.stringify(opts, undefined, 4));
104 changes: 61 additions & 43 deletions nomnom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
var _ = require("underscore"), chalk = require('chalk');


var retArgs = function(){return Array.prototype.slice.call(arguments);};

var noColorConfig = {
usageHeadingColor: retArgs,
usageStringColor: retArgs,
positionalHelpColor: retArgs,
optionsHeaderColor: retArgs,
helpColor: retArgs,
requiredArgColor: retArgs
};

var isWin = /^win/.test(process.platform);


var defaultColorConfig = _.extend(noColorConfig, {
usageHeadingColor: chalk.bold,
usageStringColor: chalk.stripColor,
positionalHelpColor: chalk.grey,
optionsHeaderColor: isWin ? chalk.cyan : chalk.blue,
helpColor: chalk.stripColor,
requiredArgColor: chalk.red
});


function ArgParser() {
this.commands = {}; // expected commands
this.specs = {}; // option specifications
Expand Down Expand Up @@ -106,8 +130,16 @@ ArgParser.prototype = {
return this;
},

_colorConfig: _.extend(defaultColorConfig),

setColors: function(colorConfig) {
this._colorConfig = _.extend(defaultColorConfig, colorConfig);
// deprecated - colors are on by default now
return this;
},

nocolors : function() {
this._nocolors = true;
this._colorConfig = _.extend(noColorConfig);
return this;
},

Expand Down Expand Up @@ -292,7 +324,8 @@ ArgParser.prototype = {
this.specs.forEach(function(opt) {
if (opt.required && options[opt.name] === undefined) {
var msg = opt.name + " argument is required";
msg = this._nocolors ? msg : chalk.red(msg);
msg = this._colorConfig.requiredArgColor(msg);
//msg = this._nocolors ? msg : chalk.red(msg);

this.print("\n" + msg + "\n" + this.getUsage(), 1);
}
Expand Down Expand Up @@ -320,18 +353,15 @@ ArgParser.prototype = {
}

// todo: use a template
var str = "\n"
if (!this._nocolors) {
str += chalk.bold("Usage:");
}
else {
str += "Usage:";
}
str += " " + this._script;
var str = "\n";

str += this._colorConfig.usageHeadingColor("Usage:");
str += this._colorConfig.usageStringColor(" " + this._script);


var positionals = _(this.specs).select(function(opt) {
return opt.position != undefined;
})
});
positionals = _(positionals).sortBy(function(opt) {
return opt.position;
});
Expand All @@ -341,7 +371,9 @@ ArgParser.prototype = {

// assume there are no gaps in the specified pos. args
positionals.forEach(function(pos) {
str += " ";

str += this._colorConfig.usageStringColor(" ");

var posStr = pos.string;
if (!posStr) {
posStr = pos.name || "arg" + pos.position;
Expand All @@ -354,21 +386,15 @@ ArgParser.prototype = {
posStr += "...";
}
}
str += posStr;
});
str += this._colorConfig.usageStringColor(posStr);
}, this);

if (options.length) {
if (!this._nocolors) {
// must be a better way to do this
str += chalk.blue(" [options]");
}
else {
str += " [options]";
}
str += this._colorConfig.optionsHeaderColor(" [options]");
}

if (options.length || positionals.length) {
str += "\n\n";
str += this._colorConfig.usageStringColor("\n\n");
}

function spaces(length) {
Expand All @@ -384,47 +410,39 @@ ArgParser.prototype = {

positionals.forEach(function(pos) {
var posStr = pos.string || pos.name;
str += posStr + spaces(longest - posStr.length) + " ";
if (!this._nocolors) {
str += chalk.grey(pos.help || "")
}
else {
str += (pos.help || "")
}
str += "\n";
str += this._colorConfig.usageStringColor(posStr + spaces(longest - posStr.length) + " ");

str += this._colorConfig.positionalHelpColor(pos.help || "");
str += this._colorConfig.usageStringColor("\n");

}, this);
if (positionals.length && options.length) {
str += "\n";
str += this._colorConfig.usageStringColor("\n");
}

if (options.length) {
if (!this._nocolors) {
str += chalk.blue("Options:");
}
else {
str += "Options:";
}
str += "\n"

str += this._colorConfig.optionsHeaderColor("Options:");
str += this._colorConfig.usageStringColor("\n");

var longest = options.reduce(function(max, opt) {
return opt.string.length > max && !opt.hidden ? opt.string.length : max;
}, 0);

options.forEach(function(opt) {
if (!opt.hidden) {
str += " " + opt.string + spaces(longest - opt.string.length) + " ";
str += this._colorConfig.usageStringColor(" " + opt.string + spaces(longest - opt.string.length) + " ");

var defaults = (opt.default != null ? " [" + opt.default + "]" : "");
var help = opt.help ? opt.help + defaults : "";
str += this._nocolors ? help: chalk.grey(help);

str += "\n";
str += this._colorConfig.helpColor(help);
str += this._colorConfig.usageStringColor("\n");
}
}, this);
}

if (this._help) {
str += "\n" + this._help;
str += this._colorConfig.usageStringColor("\n"+ this._help);
}
return str;
}
Expand Down
2 changes: 2 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ exports.testH = function(test) {
test.expect(1);

parser.printer(function(string) {
console.log('\n EXPECTED: ', expected);
console.log('\n ACTUAL: ', strip(string));
test.equal(strip(string), expected)
test.done();
})
Expand Down