@travetto/terminal 3.0.0-rc.5 → 3.0.0-rc.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/package.json +1 -1
- package/src/iterable.ts +2 -2
- package/src/operation.ts +25 -2
- package/src/terminal.ts +27 -6
- package/src/types.ts +1 -1
package/package.json
CHANGED
package/src/iterable.ts
CHANGED
|
@@ -45,14 +45,14 @@ export class IterableUtil {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
static cycle<T>(items: T[]): StoppableIterable<T> {
|
|
48
|
-
let done =
|
|
48
|
+
let done = false;
|
|
49
49
|
async function* buildStream(): AsyncIterable<T> {
|
|
50
50
|
let i = -1;
|
|
51
51
|
while (!done) {
|
|
52
52
|
yield items[(i += 1) % items.length];
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
return { stream: buildStream(), stop: (): void => { done =
|
|
55
|
+
return { stream: buildStream(), stop: (): void => { done = true; } };
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
static async drain<T>(source: AsyncIterable<T>): Promise<T[]> {
|
package/src/operation.ts
CHANGED
|
@@ -64,11 +64,11 @@ export class TerminalOperation {
|
|
|
64
64
|
const indicator = IterableUtil.map(
|
|
65
65
|
stream,
|
|
66
66
|
IterableUtil.DELAY(config),
|
|
67
|
-
(ch, i) => i === 0 ? `${ch} ${message}` : ch
|
|
67
|
+
(ch, i) => config.end ? `${message} ${ch}` : (i === 0 ? `${ch} ${message}` : ch)
|
|
68
68
|
);
|
|
69
69
|
|
|
70
70
|
const final = this.streamToPosition(term, indicator, config.position ?? 'inline');
|
|
71
|
-
return () =>
|
|
71
|
+
return async () => { stop(); return final; };
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
@@ -92,4 +92,27 @@ export class TerminalOperation {
|
|
|
92
92
|
return `${color(l)}${r}`;
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Stream lines with a waiting indicator
|
|
98
|
+
*/
|
|
99
|
+
static async streamLinesWithWaiting(term: TermState, lines: AsyncIterable<string>, cfg: TerminalWaitingConfig = {}): Promise<void> {
|
|
100
|
+
let writer: (() => Promise<unknown>) | undefined;
|
|
101
|
+
let line: string | undefined;
|
|
102
|
+
|
|
103
|
+
const commitLine = async (): Promise<void> => {
|
|
104
|
+
await writer?.();
|
|
105
|
+
if (line) {
|
|
106
|
+
await TerminalWriter.for(term).restoreOnCommit().changePosition({ y: -1 }).changePosition({ x: 0 }).writeLine(line).commit();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
for await (const msg of lines) {
|
|
111
|
+
await commitLine();
|
|
112
|
+
line = `${String.fromCharCode(171)} ${msg}`;
|
|
113
|
+
writer = this.streamWaiting(term, line, cfg);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
await commitLine();
|
|
117
|
+
}
|
|
95
118
|
}
|
package/src/terminal.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import { TerminalOperation } from './operation';
|
|
9
9
|
import { TerminalQuerier } from './query';
|
|
10
10
|
import { TerminalWriter } from './writer';
|
|
11
|
-
import { ColorOutputUtil, TermStyleInput } from './color-output';
|
|
11
|
+
import { ColorOutputUtil, Prim, TermColorFn, TermColorPalette, TermColorPaletteInput, TermStyleInput } from './color-output';
|
|
12
12
|
|
|
13
13
|
type TerminalStreamPositionConfig = {
|
|
14
14
|
position?: TermLinePosition;
|
|
@@ -121,6 +121,22 @@ export class Terminal implements TermState {
|
|
|
121
121
|
return res.finally(TerminalOperation.streamWaiting(this, message, config));
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Stream line output, showing a waiting indicator for each line until the next one occurs
|
|
126
|
+
*
|
|
127
|
+
* @param lines
|
|
128
|
+
* @param config
|
|
129
|
+
* @returns
|
|
130
|
+
*/
|
|
131
|
+
async streamLinesWithWaiting(lines: AsyncIterable<string>, config: TerminalWaitingConfig = {}): Promise<void> {
|
|
132
|
+
if (!this.interactive) {
|
|
133
|
+
for await (const line of lines) {
|
|
134
|
+
await this.writeLines(line);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return TerminalOperation.streamLinesWithWaiting(this, lines, config);
|
|
138
|
+
}
|
|
139
|
+
|
|
124
140
|
/**
|
|
125
141
|
* Consumes a stream, of events, tied to specific list indices, and updates in place
|
|
126
142
|
*/
|
|
@@ -167,17 +183,22 @@ export class Terminal implements TermState {
|
|
|
167
183
|
return this.streamToPosition(source, async (v, i) => render(await resolve(v, i)), config);
|
|
168
184
|
}
|
|
169
185
|
|
|
170
|
-
/* eslint-disable @typescript-eslint/member-ordering */
|
|
171
186
|
/** Creates a colorer function */
|
|
172
|
-
colorer
|
|
187
|
+
colorer(style: TermStyleInput | [light: TermStyleInput, dark: TermStyleInput]): TermColorFn {
|
|
188
|
+
return ColorOutputUtil.colorer(this, style);
|
|
189
|
+
}
|
|
173
190
|
|
|
174
191
|
/** Creates a color palette based on input styles */
|
|
175
|
-
palette
|
|
192
|
+
palette<P extends TermColorPaletteInput>(input: P): TermColorPalette<P> {
|
|
193
|
+
return ColorOutputUtil.palette(this, input);
|
|
194
|
+
}
|
|
176
195
|
|
|
177
196
|
/** Convenience method to creates a color template function based on input styles */
|
|
178
|
-
templateFunction
|
|
179
|
-
|
|
197
|
+
templateFunction<P extends TermColorPaletteInput>(input: P): (key: keyof P, val: Prim) => string {
|
|
198
|
+
return ColorOutputUtil.templateFunction(this, input);
|
|
199
|
+
}
|
|
180
200
|
}
|
|
201
|
+
|
|
181
202
|
export const GlobalTerminal = new Terminal({ output: process.stdout });
|
|
182
203
|
|
|
183
204
|
// Trigger
|
package/src/types.ts
CHANGED
|
@@ -13,7 +13,7 @@ export type TerminalTableEvent = { idx: number, text: string, done?: boolean };
|
|
|
13
13
|
export type TerminalTableConfig = { header?: string[], forceNonInteractiveOrder?: boolean };
|
|
14
14
|
export type TerminalProgressEvent = { idx: number, total?: number, text?: string };
|
|
15
15
|
export type TerminalProgressRender = (ev: TerminalProgressEvent) => string;
|
|
16
|
-
export type TerminalWaitingConfig = { position?: TermLinePosition } & DelayedConfig;
|
|
16
|
+
export type TerminalWaitingConfig = { position?: TermLinePosition, end?: boolean } & DelayedConfig;
|
|
17
17
|
|
|
18
18
|
export type TermColorLevel = 0 | 1 | 2 | 3;
|
|
19
19
|
export type TermColorScheme = 'dark' | 'light';
|