console-dope
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -10,2 +10,4 @@ "use strict"; | ||
var dope = module.exports = {}; | ||
/* Control Sequence Initiator */ | ||
@@ -49,2 +51,24 @@ var csi = "\x1b["; | ||
var EL = { | ||
codes: { | ||
clearLineToEnd: 0, | ||
clearLineToBeginnng: 1, | ||
clearLine: 2 | ||
}, | ||
seq: function(code){ | ||
return format("%s%sK", csi, this.codes[code]); | ||
}, | ||
activeFlags: [], | ||
activeSeq: function(){ | ||
if (this.activeFlags.length){ | ||
var codes = this.activeFlags.map(function(format){ | ||
return sgr.codes[format]; | ||
}); | ||
return format("%s%sK", csi, codes.join(";")); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
} | ||
var otherCodes = { | ||
@@ -69,14 +93,21 @@ hide: "?25l", | ||
}); | ||
Object.keys(EL.codes).forEach(function(code){ | ||
var token = new RegExp("%" + code + "{(.*?)}", "g"); | ||
if (token.test(output)){ | ||
output = output.replace( | ||
token, | ||
format("%s%s%s%s", EL.seq(code), "$1", EL.seq("reset"), EL.activeSeq()) | ||
); | ||
} | ||
}); | ||
return output; | ||
}; | ||
/* | ||
Define console properties, `console.red` etc. | ||
*/ | ||
/* Define SGR properties, `dope.red` etc. */ | ||
function addSgrProperty(flag){ | ||
Object.defineProperty(console, flag, { | ||
Object.defineProperty(dope, flag, { | ||
enumerable: true, | ||
get: function(){ | ||
sgr.activeFlags.push(flag); | ||
return console; | ||
return dope; | ||
} | ||
@@ -87,56 +118,51 @@ }); | ||
function addOtherProperty(flag){ | ||
Object.defineProperty(console, flag, { | ||
/* Define EL properties, `dope.clearLine` etc. */ | ||
function addELProperty(flag){ | ||
Object.defineProperty(dope, flag, { | ||
enumerable: true, | ||
get: function(){ | ||
otherFlags.push(flag); | ||
return console; | ||
EL.activeFlags.push(flag); | ||
return dope; | ||
} | ||
}); | ||
} | ||
// Object.keys(otherCodes).forEach(addOtherProperty); | ||
Object.keys(EL.codes).forEach(addELProperty); | ||
console.log = function(){ | ||
dope.log = function(){ | ||
out.write(sgr.activeSeq()); | ||
// out.write(otherFlags.reduce(function(prev, curr){ | ||
// return prev + "\x1b[" + otherCodes[curr]; | ||
// }, "")); | ||
out.write(EL.activeSeq()); | ||
log.apply(this, arguments); | ||
out.write(sgr.seq("reset")); | ||
sgr.activeFlags = []; | ||
return console; | ||
return this; | ||
}; | ||
console.error = function(){ | ||
dope.error = function(){ | ||
process.stderr.write(sgr.activeSeq()); | ||
process.stderr.write(EL.activeSeq()); | ||
err.apply(this, arguments); | ||
process.stderr.write(sgr.seq("reset")); | ||
sgr.activeFlags = []; | ||
return console; | ||
return this; | ||
}; | ||
console.write = function(txt){ | ||
// out.write(otherFlags.reduce(function(prev, curr){ | ||
// return prev + "\x1b[" + otherCodes[curr]; | ||
// }, "")); | ||
dope.write = function(txt){ | ||
out.write(sgr.activeSeq()); | ||
out.write(EL.activeSeq()); | ||
out.write(String(txt)); | ||
out.write(sgr.seq("reset")); | ||
sgr.activeFlags = []; | ||
return console; | ||
return this; | ||
}; | ||
console.hideCursor = function(){ | ||
dope.hideCursor = function(){ | ||
out.write("\x1b[" + otherCodes.hide); | ||
}; | ||
console.showCursor = function(){ | ||
dope.showCursor = function(){ | ||
out.write("\x1b[" + otherCodes.show); | ||
}; | ||
console.column = function(col){ | ||
dope.column = function(col){ | ||
out.write(util.format("\x1b[%dG", col)); | ||
return console; | ||
return this; | ||
}; | ||
exports = console; |
@@ -1,33 +0,36 @@ | ||
require("../"); | ||
var dope = require("../"); | ||
console.hideCursor(); | ||
dope.hideCursor(); | ||
console.log("console.log"); | ||
console.write("console.write\n"); | ||
console.error("console.error"); | ||
dope.log("dope.log"); | ||
dope.write("dope.write\n"); | ||
dope.error("dope.error"); | ||
console.red.log("console.red.log"); | ||
console.red.write("console.red.write\n"); | ||
console.red.error("console.red.error"); | ||
dope.red.log("dope.red.log"); | ||
dope.red.write("dope.red.write\n"); | ||
dope.red.error("dope.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"); | ||
dope.red.underline.log("dope.red.underline.log"); | ||
dope.red.underline.write("dope.red.underline.write\n"); | ||
dope.red.underline.error("dope.red.underline.error"); | ||
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"); | ||
dope.bold.log("dope.bold.log"); | ||
dope.bold.magenta.log("dope.bold.magenta.log", "with", { multiple: "ARGS" }); | ||
dope.bold.underline.blue.log("dope.%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"); | ||
dope.log("dope.log %underline{%s} inside format string", "underline"); | ||
dope.error("dope.error %underline{%s} inside %underline{format} string", "underline"); | ||
dope.underline.log("dope.underline.log with %blue{blue} inside the string"); | ||
console.column(10).log("console.column(10).log"); | ||
console.column(10).write("console.column(10).write\n"); | ||
console.column(10).error("console.column(10).error"); | ||
dope.column(10).log("dope.column(10).log"); | ||
dope.column(10).write("dope.column(10).write\n"); | ||
dope.column(10).error("dope.column(10).error"); | ||
console.log(1); | ||
console.write(2); | ||
console.error(3); | ||
dope.log(1); | ||
dope.write(2); | ||
dope.error(3); | ||
console.showCursor(); | ||
dope.write("this is a line innit"); | ||
dope.clearLine.column(1).log("dope.clearLine.column(1).log"); | ||
dope.showCursor(); |
{ | ||
"name": "console-dope", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "adds colouring and cursor control features to the console", | ||
@@ -5,0 +5,0 @@ "main": "console-dope.js", |
5610
176