Permalink
Browse files

Added color to terminal printer output

  • Loading branch information...
1 parent 9daf0d9 commit 239aa7d04a8b346ff215a451fba92c97ff3d83d1 @lukebayes committed Feb 16, 2011
Showing with 103 additions and 8 deletions.
  1. +66 −0 src/node_should/colored.js
  2. +37 −8 src/node_should/printer.js
@@ -0,0 +1,66 @@
+/**
+ * Copyright (c) 2009 Chris Lloyd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files
+ * (the 'Software'), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+(function(){
+
+ var colors = {
+ black: 30,
+ red: 31,
+ green: 32,
+ yellow: 33,
+ blue: 34,
+ magenta: 35,
+ cyan: 36,
+ white: 37
+ },
+ extras = {
+ reset: 0,
+ bold: 1,
+ underline: 4,
+ reversed: 7
+ };
+
+ function esc(str) {
+ return "\x1B["+str+'m';
+ }
+
+ function defineColoredFn(name, code) {
+ if(process && process.isTTY && !process.isTTY()) {
+ exports[name] = function(str) {
+ return (str || this)
+ }
+ } else {
+ exports[name] = function(str) {
+ return esc(code) + (str || this) + esc(extras.reset);
+ }
+ }
+ }
+
+ for(var name in colors) {
+ defineColoredFn(name, colors[name]);
+ };
+
+ for(var name in extras) {
+ defineColoredFn(name, extras[name])
+ };
+
+})();
@@ -1,3 +1,5 @@
+var sys = require('sys');
+var style = require('node_should/colored');
var Printer = function() {
this.contexts = [];
@@ -76,25 +78,25 @@ Printer.prototype._printInfo = function(message) {
Printer.prototype._printSuccess = function(message) {
//TODO(lukebayes) Decorate this message with ascii chars
//to turn it green in a terminal.
- this.out.write(message);
+ this.out.write(style.green(message));
}
Printer.prototype._printFailure = function(message) {
//TODO(lukebayes) Decorate this message with ascii chars
//to turn it red in a terminal.
- this.out.write(message);
+ this.out.write(style.red(message));
}
Printer.prototype._printError = function(message) {
//TODO(lukebayes) Decorate this message with ascii chars
//to turn it red in a terminal.
- this.out.write(message);
+ this.out.write(style.red(message));
}
Printer.prototype._printIgnore = function(message) {
//TODO(lukebayes) Decorate this message with ascii chars
//to turn it yellow in a terminal.
- this.out.write(message);
+ this.out.write(style.yellow(message));
}
Printer.prototype._addSuccess = function(test) {
@@ -154,14 +156,41 @@ Printer.prototype._printFinish = function() {
Printer.prototype._printSummary = function() {
this._printInfo('Test Count: ' + this.length + ', ');
- this._printSuccess('OK: ' + this.succeeded.length + ', ');
- this._printFailure('Failures: ' + this.failed.length + ', ');
- this._printError('Errors: ' + this.errored.length + ', ');
- this._printIgnore('Ignored: ' + this.ignored.length);
+ this._printInfo('OK: ' + this.succeeded.length + ', ');
+ this._printFailureSummary();
+ this._printErrorSummary();
+ this._printIgnoredSummary();
this._printInfo('\n');
this._printInfo('\n');
}
+Printer.prototype._printErrorSummary = function() {
+ var message = 'Errors: ' + this.errored.length + ', ';
+ if (this.errored.length > 0) {
+ this._printError(message);
+ } else {
+ this._printInfo(message);
+ }
+}
+
+Printer.prototype._printFailureSummary = function() {
+ var message = 'Failures: ' + this.failed.length + ', ';
+ if (this.failed.length > 0) {
+ this._printFailure(message);
+ } else {
+ this._printInfo(message);
+ }
+}
+
+Printer.prototype._printIgnoredSummary = function() {
+ var message = 'Ignored: ' + this.ignored.length;
+ if (this.ignored.length > 0) {
+ this._printIgnore(message);
+ } else {
+ this._printInfo(message);
+ }
+}
+
Printer.prototype._printDuration = function() {
this._printInfo('Total Time: ' + this._getDuration() + ' ms\n');
this._printInfo('\n');

0 comments on commit 239aa7d

Please sign in to comment.