-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module.exports = { | ||
// modifiers | ||
reset: [0, 0], | ||
bold: [1, 22], | ||
dim: [2, 22], | ||
italic: [3, 23], | ||
underline: [4, 24], | ||
inverse: [7, 27], | ||
hidden: [8, 28], | ||
strikethrough: [9, 29], | ||
|
||
// colors | ||
black: [30, 39], | ||
red: [31, 39], | ||
green: [32, 39], | ||
yellow: [33, 39], | ||
blue: [34, 39], | ||
magenta: [35, 39], | ||
cyan: [36, 39], | ||
white: [37, 39], | ||
gray: [90, 39], | ||
|
||
// background colors | ||
bgBlack: [40, 49], | ||
bgRed: [41, 49], | ||
bgGreen: [42, 49], | ||
bgYellow: [43, 49], | ||
bgBlue: [44, 49], | ||
bgMagenta: [45, 49], | ||
bgCyan: [46, 49], | ||
bgWhite: [47, 49], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,44 @@ | ||
module.exports = {} | ||
const { format } = require('util'); | ||
const CODES = require('./codes'); | ||
|
||
const $ = { enabled:true }; | ||
const RGX = /\x1b\[[0-9]+m/ig; | ||
const toANSI = x => `\x1b[${x}m`; | ||
|
||
function print() { | ||
let out = format.apply(null, arguments); | ||
if (!$.enabled || out.trim().length == 0) return out; | ||
|
||
let i=0, tmp, arr=this.keys, isMulti=!!~out.indexOf('\n'); | ||
for (; i < arr.length; i++) { | ||
tmp = CODES[arr[i]]; // [x1, x2, rgx] | ||
if (out.indexOf(tmp[0]) !== 0 || out.slice(-tmp[1].length) !== tmp[1]) { | ||
out = tmp[0] + out.replace(tmp[2], tmp[0]) + tmp[1]; | ||
} | ||
isMulti && (out = out.replace(/(\r?\n)/g, `${tmp[1]}$1${tmp[0]}`)); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
function wrap(keys) { | ||
let fn = (...args) => print.apply(fn, args); | ||
Object.setPrototypeOf(fn, $); | ||
fn.keys = keys; | ||
return fn; | ||
} | ||
|
||
for (let k in CODES) { | ||
CODES[k] = CODES[k].map(toANSI).concat(new RegExp(`\\x1b\\[${CODES[k][1]}m`, 'g')); | ||
Object.defineProperty($, k, { | ||
get() { | ||
return this.keys !== void 0 ? (this.keys.push(k),this) : wrap([k]); | ||
} | ||
}); | ||
} | ||
|
||
$.clear = str => { | ||
return str && typeof str === 'string' ? str.replace(RGX, '') : str; | ||
} | ||
|
||
module.exports = $; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,6 @@ | |
"url": "lukeed.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
} | ||
} |