@trenskow/print 0.1.6 → 0.1.8
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 +35 -4
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
// See license in LICENSE.
|
|
7
7
|
//
|
|
8
8
|
|
|
9
|
+
import { PassThrough } from 'stream';
|
|
10
|
+
|
|
9
11
|
import NullStream from '@trenskow/null-stream';
|
|
10
12
|
import keyd from 'keyd';
|
|
11
13
|
|
|
@@ -143,21 +145,34 @@ const printer = (stream) => {
|
|
|
143
145
|
|
|
144
146
|
};
|
|
145
147
|
|
|
146
|
-
print.table = ({ columns, data, options: {
|
|
148
|
+
print.table = ({ columns, data, options: { paddingLeft = 0 } = {} }) => {
|
|
147
149
|
|
|
148
150
|
if (!Array.isArray(data)) data = [data];
|
|
149
151
|
|
|
152
|
+
let header = true;
|
|
153
|
+
|
|
154
|
+
if (Array.isArray(columns)) {
|
|
155
|
+
|
|
156
|
+
header = false;
|
|
157
|
+
|
|
158
|
+
columns = Object.fromEntries(
|
|
159
|
+
columns.map((column) => [column, column]));
|
|
160
|
+
|
|
161
|
+
}
|
|
162
|
+
|
|
150
163
|
data = data.map((row) => Object.keys(columns)
|
|
151
164
|
.map((column) => keyd(row).get(column) || '')
|
|
152
165
|
.map((column) => typeof column === 'function' ? column() : column)
|
|
153
|
-
.map((column) => `${column || ''}`));
|
|
166
|
+
.map((column) => `${''.padStart(paddingLeft, ' ')}${column || ''}`));
|
|
154
167
|
|
|
155
168
|
if (!stream.isTTY) {
|
|
156
169
|
print(data.map((row) => row.join('\t')).join('\n'));
|
|
157
170
|
return;
|
|
158
171
|
}
|
|
159
172
|
|
|
160
|
-
|
|
173
|
+
if (header) {
|
|
174
|
+
data = [Object.values(columns), ...data];
|
|
175
|
+
}
|
|
161
176
|
|
|
162
177
|
const columnWidth = Object.keys(columns)
|
|
163
178
|
.map((_, idx) => {
|
|
@@ -166,7 +181,7 @@ const printer = (stream) => {
|
|
|
166
181
|
|
|
167
182
|
data.forEach((row, idx) => {
|
|
168
183
|
|
|
169
|
-
print.nn(idx === 0 && header ? '\x1b[1m
|
|
184
|
+
print.tty.nn(idx === 0 && header ? `${''.padStart(paddingLeft, ' ')}\x1b[1m` : '\x1b[0m');
|
|
170
185
|
|
|
171
186
|
print(
|
|
172
187
|
row
|
|
@@ -193,6 +208,22 @@ print.out = print;
|
|
|
193
208
|
print.err = printer(process.stderr);
|
|
194
209
|
print.stream = printer;
|
|
195
210
|
|
|
211
|
+
print.toString = (writer) => {
|
|
212
|
+
|
|
213
|
+
const stream = new PassThrough();
|
|
214
|
+
|
|
215
|
+
let string = '';
|
|
216
|
+
|
|
217
|
+
stream.on('data', (chunk) => string += chunk.toString());
|
|
218
|
+
|
|
219
|
+
writer(printer(stream));
|
|
220
|
+
|
|
221
|
+
stream.end();
|
|
222
|
+
|
|
223
|
+
return string;
|
|
224
|
+
|
|
225
|
+
};
|
|
226
|
+
|
|
196
227
|
const parallelProgress = (options = {}) => {
|
|
197
228
|
|
|
198
229
|
options.simpleOutput = !process.stdout.isTTY || options.simpleOutput;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/print",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
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
|
}
|