@trenskow/print 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +71 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -47,6 +47,76 @@ const printer = (stream) => {
|
|
|
47
47
|
print(content, Object.assign({ newLine: false }, options));
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
+
Object.entries({
|
|
51
|
+
1: [['bold', 'bright'], ['dim', 'dimmed'], undefined, ['underscore', 'underline', 'underlined'], 'blink', ['reverse', 'reversed'], ['hidden', 'invisible']],
|
|
52
|
+
30: ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
|
|
53
|
+
}).forEach(([offset, modifiers]) => {
|
|
54
|
+
|
|
55
|
+
offset = parseInt(offset);
|
|
56
|
+
|
|
57
|
+
modifiers.forEach((modifier, idx) => {
|
|
58
|
+
|
|
59
|
+
if (typeof modifier === 'undefined') return;
|
|
60
|
+
|
|
61
|
+
if (!Array.isArray(modifier)) modifier = [modifier];
|
|
62
|
+
|
|
63
|
+
modifier.forEach((modifier) => {
|
|
64
|
+
|
|
65
|
+
print[modifier] = (content) => {
|
|
66
|
+
print[modifier].nn(content);
|
|
67
|
+
print();
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
print[modifier].nn = (content) => {
|
|
71
|
+
if (stream.isTTY) print.nn(`\x1b[${offset + idx}m`);
|
|
72
|
+
print.nn(content);
|
|
73
|
+
if (stream.isTTY) print.nn('\x1b[0m');
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
print.color = (colors) => {
|
|
84
|
+
return (content) => {
|
|
85
|
+
print.color.nn(colors)(content);
|
|
86
|
+
print();
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
print.color.nn = (colors) => {
|
|
91
|
+
|
|
92
|
+
if (!Array.isArray(colors)) colors = [colors];
|
|
93
|
+
|
|
94
|
+
while (colors.length < 3) colors.push(0);
|
|
95
|
+
|
|
96
|
+
if (colors.some((color) => color > 1)) {
|
|
97
|
+
colors = colors.map((color) => color / 255);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (process.env.COLORTERM !== 'truecolor') {
|
|
101
|
+
return print;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return (content) => {
|
|
105
|
+
|
|
106
|
+
if (stream.isTTY) {
|
|
107
|
+
print.nn(`\x1b[38;2;${colors.map((color) => Math.round(color * 255)).join(';')}m`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
print(content);
|
|
111
|
+
|
|
112
|
+
if (stream.isTTY) {
|
|
113
|
+
print.nn('\x1b[0m');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
};
|
|
119
|
+
|
|
50
120
|
print.sentence = (sentence) => {
|
|
51
121
|
|
|
52
122
|
if (!stream.isTTY) return print(sentence);
|
|
@@ -109,7 +179,7 @@ const printer = (stream) => {
|
|
|
109
179
|
|
|
110
180
|
Object.defineProperty(print, 'tty', {
|
|
111
181
|
get: () => {
|
|
112
|
-
return stream.isTTY ?
|
|
182
|
+
return stream.isTTY ? printer(stream) : printer(new NullStream());
|
|
113
183
|
}
|
|
114
184
|
});
|
|
115
185
|
|