console-dope
Advanced tools
Comparing version 0.2.0 to 0.2.1
"use strict"; | ||
var wodge = require("wodge"), | ||
util = require("util"); | ||
var util = require("util"); | ||
@@ -11,2 +10,5 @@ var log = console.log, | ||
/* Control Sequence Initiator */ | ||
var csi = "\x1b["; | ||
/* | ||
@@ -31,6 +33,11 @@ Select Graphic Rendition | ||
}, | ||
flags: [], | ||
format: "\x1b[%sm%s\x1b[0m", | ||
seq: function(code){ | ||
return format("\x1b[%sm", this.codes[code]); | ||
return format("%s%sm", csi, this.codes[code]); | ||
}, | ||
activeFlags: [], | ||
activeSeq: function(){ | ||
var codes = this.activeFlags.map(function(format){ | ||
return sgr.codes[format]; | ||
}); | ||
return format("%s%sm", csi, codes.join(";")); | ||
} | ||
@@ -44,7 +51,15 @@ }; | ||
/* | ||
Override util.format to additionally replace `%blue{blah}` style tokens | ||
*/ | ||
util.format = function(){ | ||
var output = format.apply(null, arguments); | ||
Object.keys(sgr.codes).forEach(function(code){ | ||
var re = new RegExp("%" + code + "{(.*?)}", "g"); | ||
output = output.replace(re, sgr.seq(code) + "$1"); | ||
var token = new RegExp("%" + code + "{(.*?)}", "g"); | ||
if (token.test(output)){ | ||
output = output.replace( | ||
token, | ||
format("%s%s%s%s", sgr.seq(code), "$1", sgr.seq("reset"), sgr.activeSeq()) | ||
); | ||
} | ||
}); | ||
@@ -54,7 +69,10 @@ return output; | ||
/* | ||
Define console properties, `console.red` etc. | ||
*/ | ||
function addSgrProperty(flag){ | ||
Object.defineProperty(console, flag, { | ||
enumerable: true, | ||
Object.defineProperty(console, flag, { | ||
enumerable: true, | ||
get: function(){ | ||
sgr.flags.push(flag); | ||
sgr.activeFlags.push(flag); | ||
return console; | ||
@@ -67,4 +85,4 @@ } | ||
function addOtherProperty(flag){ | ||
Object.defineProperty(console, flag, { | ||
enumerable: true, | ||
Object.defineProperty(console, flag, { | ||
enumerable: true, | ||
get: function(){ | ||
@@ -80,6 +98,3 @@ otherFlags.push(flag); | ||
console.log = function(){ | ||
var args = wodge.array(arguments), | ||
codes = sgr.flags.map(function(format){ return sgr.codes[format]; }); | ||
out.write(util.format("\x1b[%sm", codes.join(";"))); | ||
out.write(sgr.activeSeq()); | ||
// out.write(otherFlags.reduce(function(prev, curr){ | ||
@@ -89,4 +104,4 @@ // return prev + "\x1b[" + otherCodes[curr]; | ||
log.apply(this, arguments); | ||
out.write("\x1b[0m"); | ||
sgr.flags = []; | ||
out.write(sgr.seq("reset")); | ||
sgr.activeFlags = []; | ||
return console; | ||
@@ -96,9 +111,6 @@ }; | ||
console.error = function(){ | ||
var args = wodge.array(arguments), | ||
codes = sgr.flags.map(function(format){ return sgr.codes[format]; }); | ||
process.stderr.write(util.format("\x1b[%sm", codes.join(";"))); | ||
process.stderr.write(sgr.activeSeq()); | ||
err.apply(this, arguments); | ||
process.stderr.write("\x1b[0m"); | ||
sgr.flags = []; | ||
process.stderr.write(sgr.seq("reset")); | ||
sgr.activeFlags = []; | ||
return console; | ||
@@ -108,8 +120,9 @@ }; | ||
console.write = function(txt){ | ||
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 = []; | ||
out.write(sgr.activeSeq()); | ||
out.write(txt); | ||
out.write(sgr.seq("reset")); | ||
sgr.activeFlags = []; | ||
return console; | ||
@@ -116,0 +129,0 @@ }; |
require("../"); | ||
console.log("console.log"); | ||
console.write("console.write\n"); | ||
console.error("console.error"); | ||
console.red.log("console.red.log"); | ||
console.red.write("console.red.write\n"); | ||
console.red.error("console.red.error"); | ||
console.red.underline.log("console.red.underline.log"); | ||
console.red.underline.write("console.red.underline.write\n"); | ||
console.red.underline.error("console.red.underline.error"); | ||
console.green.write("console.green.write"); | ||
console.write("\n"); | ||
console.bold.log("console.bold.log"); | ||
console.bold.magenta.log("console.bold.magenta.log", "with", { multiple: "ARGS" }); | ||
console.bold.underline.blue.log("console.%s.underline.%s.log using format string", "bold", "blue"); | ||
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.2.0", | ||
"version": "0.2.1", | ||
"description": "adds colouring and cursor control features to the console", | ||
"main": "console-dope.js", | ||
"author": "Lloyd Brookes", | ||
"dependencies": { | ||
"wodge": "~0.1.5" | ||
} | ||
"author": "Lloyd Brookes" | ||
} |
4546
0
4
136
- Removedwodge@~0.1.5
- Removedwodge@0.1.5(transitive)