console-toolkit 1.0.0 → 1.1.0
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/README.md +1 -0
- package/package.json +1 -1
- package/src/alphanumeric/number-formatters.js +1 -1
- package/src/output/updater.js +17 -12
- package/src/output/writer.js +7 -6
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -24,7 +24,7 @@ export const prepareTimeFormat = (values, scale = 1, useUnicode) => {
|
|
|
24
24
|
|
|
25
25
|
export const formatTime = (value, format) => {
|
|
26
26
|
let result = (value * format.scale).toFixed(format.precision);
|
|
27
|
-
if (format.precision > 0) result = result.replace(/\.0+$/, '');
|
|
27
|
+
if (format.precision > 0 && !format.keepFractionAsIs) result = result.replace(/\.0+$/, '');
|
|
28
28
|
return result + format.unit;
|
|
29
29
|
};
|
|
30
30
|
|
package/src/output/updater.js
CHANGED
|
@@ -7,11 +7,15 @@ import {cursorUp, setCommands} from '../ansi/csi.js';
|
|
|
7
7
|
const RESET = setCommands([]);
|
|
8
8
|
|
|
9
9
|
export class Updater {
|
|
10
|
-
constructor(updater, {prologue, epilogue, noLastNewLine} = {}, writer = new Writer()) {
|
|
10
|
+
constructor(updater, {prologue, epilogue, beforeFrame, afterFrame, beforeLine, afterLine, noLastNewLine} = {}, writer = new Writer()) {
|
|
11
11
|
this.updater = updater;
|
|
12
12
|
this.writer = writer;
|
|
13
13
|
this.prologue = prologue || RESET;
|
|
14
14
|
this.epilogue = epilogue || RESET;
|
|
15
|
+
this.beforeFrame = beforeFrame || '';
|
|
16
|
+
this.afterFrame = afterFrame || '';
|
|
17
|
+
this.beforeLine = beforeLine || '';
|
|
18
|
+
this.afterLine = afterLine || '';
|
|
15
19
|
this.noLastNewLine = noLastNewLine;
|
|
16
20
|
this.lastHeight = 0;
|
|
17
21
|
this.isDone = false;
|
|
@@ -34,30 +38,31 @@ export class Updater {
|
|
|
34
38
|
this.intervalHandle = null;
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
getFrame(state) {
|
|
38
|
-
if (typeof this.updater == 'function') return this.updater(state);
|
|
41
|
+
getFrame(state, ...args) {
|
|
42
|
+
if (typeof this.updater == 'function') return this.updater(state, ...args);
|
|
39
43
|
if (typeof this.updater?.getFrame == 'function') {
|
|
40
44
|
this.updater.state = state;
|
|
41
|
-
return this.updater.getFrame();
|
|
45
|
+
return this.updater.getFrame(...args);
|
|
42
46
|
}
|
|
43
47
|
throw new TypeError('Updater must be a function or implement getFrame()');
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
async writeFrame(state) {
|
|
50
|
+
async writeFrame(state, ...args) {
|
|
47
51
|
if (this.first) {
|
|
48
52
|
this.prologue && (await this.writer.writeString(this.prologue));
|
|
49
53
|
this.first = false;
|
|
50
54
|
}
|
|
51
55
|
|
|
52
|
-
const frame = toStrings(this.getFrame(state));
|
|
56
|
+
const frame = toStrings(this.getFrame(state, ...args));
|
|
53
57
|
if (!frame) return;
|
|
54
58
|
|
|
55
|
-
if (this.lastHeight) await this.writer.writeString('\r' + cursorUp(this.lastHeight));
|
|
59
|
+
if (this.lastHeight) await this.writer.writeString('\r' + cursorUp(this.lastHeight) + this.beforeFrame);
|
|
56
60
|
|
|
57
61
|
this.lastHeight = frame.length;
|
|
58
62
|
if (this.noLastNewLine) --this.lastHeight;
|
|
59
63
|
|
|
60
|
-
await this.writer.write(frame,
|
|
64
|
+
await this.writer.write(frame, {noLastNewLine: this.noLastNewLine, beforeLine: this.beforeLine, afterLine: this.afterLine});
|
|
65
|
+
this.afterFrame && (await this.writer.writeString(this.afterFrame));
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
async done() {
|
|
@@ -67,14 +72,14 @@ export class Updater {
|
|
|
67
72
|
this.epilogue && (await this.writer.writeString(this.epilogue));
|
|
68
73
|
}
|
|
69
74
|
|
|
70
|
-
async update(state = 'active') {
|
|
75
|
+
async update(state = 'active', ...args) {
|
|
71
76
|
if (this.isDone || !this.writer.isTTY) return;
|
|
72
|
-
await this.writeFrame(state);
|
|
77
|
+
await this.writeFrame(state, ...args);
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
async final() {
|
|
80
|
+
async final(...args) {
|
|
76
81
|
if (this.isDone) return;
|
|
77
|
-
await this.writeFrame('finished');
|
|
82
|
+
await this.writeFrame('finished', ...args);
|
|
78
83
|
await this.done();
|
|
79
84
|
}
|
|
80
85
|
}
|
package/src/output/writer.js
CHANGED
|
@@ -93,12 +93,12 @@ export class Writer {
|
|
|
93
93
|
await write(this.stream, s.replace(matchCsiNoGroups, ''));
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
async write(s, sameColumn, noLastNewLine) {
|
|
96
|
+
async write(s, {sameColumn, noLastNewLine, beforeLine = '', afterLine = ''} = {}) {
|
|
97
97
|
s = toStrings(s);
|
|
98
98
|
|
|
99
99
|
if (!this.isTTY) {
|
|
100
100
|
const matcher = this.forceColorDepth ? matchCsiNoSgrNoGroups : matchCsiNoGroups;
|
|
101
|
-
let lines = Array.from(s).join('\n');
|
|
101
|
+
let lines = Array.from(s).map(line => beforeLine + line + afterLine).join('\n');
|
|
102
102
|
if (!noLastNewLine) lines += '\n';
|
|
103
103
|
matcher.lastIndex = 0;
|
|
104
104
|
lines = lines.replace(matcher, '');
|
|
@@ -108,21 +108,22 @@ export class Writer {
|
|
|
108
108
|
|
|
109
109
|
if (sameColumn === 'save') {
|
|
110
110
|
for (const line of s) {
|
|
111
|
-
await write(this.stream, CURSOR_SAVE_POS + line + CURSOR_RESTORE_POS + CURSOR_DOWN1);
|
|
111
|
+
await write(this.stream, CURSOR_SAVE_POS + beforeLine + line + afterline + CURSOR_RESTORE_POS + CURSOR_DOWN1);
|
|
112
112
|
}
|
|
113
113
|
return;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
if (sameColumn) {
|
|
117
117
|
for (const line of s) {
|
|
118
|
-
const
|
|
119
|
-
|
|
118
|
+
const fullLine = beforeLine + line + afterLine,
|
|
119
|
+
length = getLength(fullLine);
|
|
120
|
+
await write(this.stream, fullLine);
|
|
120
121
|
await this.moveCursor(-length, 1);
|
|
121
122
|
}
|
|
122
123
|
return;
|
|
123
124
|
}
|
|
124
125
|
|
|
125
|
-
let lines = Array.from(s).join('\n');
|
|
126
|
+
let lines = Array.from(s).map(line => beforeLine + line + afterLine).join('\n');
|
|
126
127
|
if (!noLastNewLine) lines += '\n';
|
|
127
128
|
await write(this.stream, lines);
|
|
128
129
|
}
|