console-dope
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -5,15 +5,34 @@ "use strict"; | ||
var log = console.log, | ||
err = console.error, | ||
format = util.format, | ||
otherFlags = [], | ||
out = console.writeStream || process.stdout; | ||
/* | ||
Select Graphic Rendition | ||
http://en.wikipedia.org/wiki/ANSI_escape_code#graphics | ||
*/ | ||
var sgr = { | ||
black: 30, | ||
red: 31, | ||
green: 32, | ||
yellow: 33, | ||
blue: 34, | ||
magenta: 35, | ||
cyan: 36, | ||
white: 37, | ||
bold: 1, | ||
underline: 4 | ||
codes: { | ||
reset: 0, | ||
bold: 1, | ||
italic: 3, | ||
underline: 4, | ||
black: 30, | ||
red: 31, | ||
green: 32, | ||
yellow: 33, | ||
blue: 34, | ||
magenta: 35, | ||
cyan: 36, | ||
white: 37 | ||
}, | ||
flags: [], | ||
format: "\x1b[%sm%s\x1b[0m", | ||
seq: function(code){ | ||
return format("\x1b[%sm", this.codes[code]); | ||
} | ||
}; | ||
var sgrFormat = "\x1b[%sm%s\x1b[0m"; | ||
var otherCodes = { | ||
@@ -24,14 +43,8 @@ hide: "?25l", | ||
var log = console.log, | ||
err = console.error, | ||
sgrFlags = [], | ||
otherFlags = [], | ||
out = console.writeStream || process.stdout; | ||
var format = util.format; | ||
util.format = function(){ | ||
var output = format.apply(null, arguments); | ||
output = output.replace(/%u{(.*?)}/g, format(sgrFormat, sgr.underline, "$1")); | ||
// process.stdout.write(output + "\n"); | ||
Object.keys(sgr.codes).forEach(function(code){ | ||
var re = new RegExp("%" + code + "{(.*?)}", "g"); | ||
output = output.replace(re, sgr.seq(code) + "$1"); | ||
}); | ||
return output; | ||
@@ -44,3 +57,3 @@ }; | ||
get: function(){ | ||
sgrFlags.push(flag); | ||
sgr.flags.push(flag); | ||
return console; | ||
@@ -50,3 +63,3 @@ } | ||
} | ||
Object.keys(sgr).forEach(addSgrProperty); | ||
Object.keys(sgr.codes).forEach(addSgrProperty); | ||
@@ -67,11 +80,11 @@ function addOtherProperty(flag){ | ||
var args = wodge.array(arguments), | ||
codes = sgrFlags.map(function(format){ return sgr[format]; }); | ||
codes = sgr.flags.map(function(format){ return sgr.codes[format]; }); | ||
out.write(util.format("\x1b[%sm", codes.join(";"))); | ||
out.write(otherFlags.reduce(function(prev, curr){ | ||
return prev + "\x1b[" + otherCodes[curr]; | ||
}, "")); | ||
// out.write(otherFlags.reduce(function(prev, curr){ | ||
// return prev + "\x1b[" + otherCodes[curr]; | ||
// }, "")); | ||
log.apply(this, arguments); | ||
out.write("\x1b[0m"); | ||
sgrFlags = []; | ||
sgr.flags = []; | ||
return console; | ||
@@ -82,3 +95,3 @@ }; | ||
var args = wodge.array(arguments), | ||
codes = sgrFlags.map(function(format){ return sgr[format]; }); | ||
codes = sgr.flags.map(function(format){ return sgr.codes[format]; }); | ||
@@ -88,3 +101,3 @@ process.stderr.write(util.format("\x1b[%sm", codes.join(";"))); | ||
process.stderr.write("\x1b[0m"); | ||
sgrFlags = []; | ||
sgr.flags = []; | ||
return console; | ||
@@ -94,8 +107,8 @@ }; | ||
console.write = function(txt){ | ||
var codes = sgrFlags.map(function(format){ return sgr[format]; }); | ||
out.write(otherFlags.reduce(function(prev, curr){ | ||
return prev + "\x1b[" + otherCodes[curr]; | ||
}, "")); | ||
out.write(util.format(sgrFormat, codes.join(";"), txt)); | ||
sgrFlags = []; | ||
var codes = sgr.flags.map(function(format){ return sgr.codes[format]; }); | ||
// out.write(otherFlags.reduce(function(prev, curr){ | ||
// return prev + "\x1b[" + otherCodes[curr]; | ||
// }, "")); | ||
out.write(util.format(sgr.format, codes.join(";"), txt)); | ||
sgr.flags = []; | ||
return console; | ||
@@ -102,0 +115,0 @@ }; |
@@ -12,3 +12,4 @@ require("../"); | ||
console.bold.underline.blue.log("console.%s.underline.%s.log using format string", "bold", "blue"); | ||
console.log("console.log %u{%s} inside format string", "underline"); | ||
console.error("console.error %u{%s} inside %u{format} string", "underline"); | ||
console.log("console.log %underline{%s} inside format string", "underline"); | ||
console.error("console.error %underline{%s} inside %underline{format} string", "underline"); | ||
console.underline.log("console.underline.log with %blue{blue} inside the string"); |
{ | ||
"name": "console-dope", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "adds colouring and cursor control features to the console", | ||
@@ -5,0 +5,0 @@ "main": "console-dope.js", |
3916
119