/
index.js
56 lines (48 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var map = require('es5-ext/lib/Object/plain/map').call
, toString, codes, properties, init;
toString = function (code, str) {
return '\x1b[' + code[0] + 'm' + (str || "") + '\x1b[' + code[1] + 'm';
};
codes = {
// styles
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'strike' : [9, 29]
};
['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'].forEach(
function (color, index) {
// foreground
codes[color] = [30 + index, 39];
// background
codes['bg' + color[0].toUpperCase() + color.slice(1)] = [40 + index, 49];
}
);
properties = map(codes, function (code) {
return {
get: function () {
this.style.push(code);
return this;
},
enumerable: true
};
});
init = function () {
var o = Object.defineProperties(function self (msg) {
return self.style.reduce(function (msg, code) {
return toString(code, msg);
}, msg);
}, properties);
o.style = [];
return o[this];
};
module.exports = Object.defineProperties(function (msg) {
return msg;
}, map(codes, function (code, name) {
return {
get: init.bind(name),
enumerable: true
};
}));