@trenskow/print 0.1.7 → 0.1.9
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 +14 -10
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -12,9 +12,13 @@ import NullStream from '@trenskow/null-stream';
|
|
|
12
12
|
import keyd from 'keyd';
|
|
13
13
|
|
|
14
14
|
let ttyColumn = 0;
|
|
15
|
+
let forceTTY = ['1', 'true'].includes(process.env.FORCE_TTY);
|
|
16
|
+
let ttyWidth = parseInt(process.env.TTY_WIDTH) || process.stdout.columns || 80;
|
|
15
17
|
|
|
16
18
|
const printer = (stream) => {
|
|
17
19
|
|
|
20
|
+
const isTTY = forceTTY || stream.isTTY;
|
|
21
|
+
|
|
18
22
|
const print = (content = '', options = {}) => {
|
|
19
23
|
|
|
20
24
|
if (!Array.isArray(content)) content = [content];
|
|
@@ -31,13 +35,13 @@ const printer = (stream) => {
|
|
|
31
35
|
|
|
32
36
|
stream.write(output);
|
|
33
37
|
|
|
34
|
-
if (
|
|
38
|
+
if (isTTY) {
|
|
35
39
|
|
|
36
40
|
const lines = output.split('\n');
|
|
37
41
|
|
|
38
42
|
if (lines.length > 1) ttyColumn = 0;
|
|
39
43
|
|
|
40
|
-
ttyColumn = (ttyColumn + lines[lines.length - 1].length) % stream.columns;
|
|
44
|
+
ttyColumn = (ttyColumn + lines[lines.length - 1].length) % (stream.columns || ttyWidth);
|
|
41
45
|
|
|
42
46
|
}
|
|
43
47
|
|
|
@@ -70,9 +74,9 @@ const printer = (stream) => {
|
|
|
70
74
|
};
|
|
71
75
|
|
|
72
76
|
print[modifier].nn = (content) => {
|
|
73
|
-
if (
|
|
77
|
+
if (isTTY) print.nn(`\x1b[${offset + idx}m`);
|
|
74
78
|
print.nn(content);
|
|
75
|
-
if (
|
|
79
|
+
if (isTTY) print.nn('\x1b[0m');
|
|
76
80
|
};
|
|
77
81
|
|
|
78
82
|
});
|
|
@@ -105,13 +109,13 @@ const printer = (stream) => {
|
|
|
105
109
|
|
|
106
110
|
return (content) => {
|
|
107
111
|
|
|
108
|
-
if (
|
|
112
|
+
if (isTTY) {
|
|
109
113
|
print.nn(`\x1b[38;2;${colors.map((color) => Math.round(color * 255)).join(';')}m`);
|
|
110
114
|
}
|
|
111
115
|
|
|
112
116
|
print(content);
|
|
113
117
|
|
|
114
|
-
if (
|
|
118
|
+
if (isTTY) {
|
|
115
119
|
print.nn('\x1b[0m');
|
|
116
120
|
}
|
|
117
121
|
|
|
@@ -121,7 +125,7 @@ const printer = (stream) => {
|
|
|
121
125
|
|
|
122
126
|
print.sentence = (sentence) => {
|
|
123
127
|
|
|
124
|
-
if (!
|
|
128
|
+
if (!isTTY) return print(sentence);
|
|
125
129
|
|
|
126
130
|
const offset = ttyColumn;
|
|
127
131
|
|
|
@@ -130,7 +134,7 @@ const printer = (stream) => {
|
|
|
130
134
|
words
|
|
131
135
|
.forEach((word) => {
|
|
132
136
|
|
|
133
|
-
if (ttyColumn + word.length >= stream.columns) {
|
|
137
|
+
if (ttyColumn + word.length >= (stream.columns || ttyWidth)) {
|
|
134
138
|
print();
|
|
135
139
|
print.nn((new Array(offset).fill(' ').join('')));
|
|
136
140
|
}
|
|
@@ -165,7 +169,7 @@ const printer = (stream) => {
|
|
|
165
169
|
.map((column) => typeof column === 'function' ? column() : column)
|
|
166
170
|
.map((column) => `${''.padStart(paddingLeft, ' ')}${column || ''}`));
|
|
167
171
|
|
|
168
|
-
if (!
|
|
172
|
+
if (!isTTY) {
|
|
169
173
|
print(data.map((row) => row.join('\t')).join('\n'));
|
|
170
174
|
return;
|
|
171
175
|
}
|
|
@@ -194,7 +198,7 @@ const printer = (stream) => {
|
|
|
194
198
|
|
|
195
199
|
Object.defineProperty(print, 'tty', {
|
|
196
200
|
get: () => {
|
|
197
|
-
return
|
|
201
|
+
return isTTY ? printer(stream) : printer(new NullStream());
|
|
198
202
|
}
|
|
199
203
|
});
|
|
200
204
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/print",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "A simple package for printing to the console (or other streams).",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -31,6 +31,6 @@
|
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@trenskow/null-stream": "^0.1.1",
|
|
34
|
-
"keyd": "^2.1.
|
|
34
|
+
"keyd": "^2.1.18"
|
|
35
35
|
}
|
|
36
36
|
}
|