@travetto/terminal 3.0.0-rc.4 → 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/color-output.ts +1 -1
- package/src/iterable.ts +2 -2
- package/src/operation.ts +25 -2
- package/src/terminal.ts +31 -7
- package/src/types.ts +1 -1
package/package.json
CHANGED
package/src/color-output.ts
CHANGED
|
@@ -46,7 +46,7 @@ export class ColorOutputUtil {
|
|
|
46
46
|
): Promise<TermColorScheme | undefined> {
|
|
47
47
|
let color = await query();
|
|
48
48
|
if (!color && env) {
|
|
49
|
-
const [bg] = env.split(';');
|
|
49
|
+
const [, bg] = env.split(';');
|
|
50
50
|
color = ColorDefineUtil.rgbFromAnsi256(+bg);
|
|
51
51
|
}
|
|
52
52
|
if (color) {
|
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;
|
|
@@ -99,7 +99,10 @@ export class Terminal implements TermState {
|
|
|
99
99
|
|
|
100
100
|
async reset(): Promise<void> {
|
|
101
101
|
await this.#query.close();
|
|
102
|
-
|
|
102
|
+
if (this.interactive) {
|
|
103
|
+
await this.writer().reset().commit();
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
103
106
|
}
|
|
104
107
|
|
|
105
108
|
getCursorPosition(): Promise<TermCoord> {
|
|
@@ -118,6 +121,22 @@ export class Terminal implements TermState {
|
|
|
118
121
|
return res.finally(TerminalOperation.streamWaiting(this, message, config));
|
|
119
122
|
}
|
|
120
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
|
+
|
|
121
140
|
/**
|
|
122
141
|
* Consumes a stream, of events, tied to specific list indices, and updates in place
|
|
123
142
|
*/
|
|
@@ -164,17 +183,22 @@ export class Terminal implements TermState {
|
|
|
164
183
|
return this.streamToPosition(source, async (v, i) => render(await resolve(v, i)), config);
|
|
165
184
|
}
|
|
166
185
|
|
|
167
|
-
/* eslint-disable @typescript-eslint/member-ordering */
|
|
168
186
|
/** Creates a colorer function */
|
|
169
|
-
colorer
|
|
187
|
+
colorer(style: TermStyleInput | [light: TermStyleInput, dark: TermStyleInput]): TermColorFn {
|
|
188
|
+
return ColorOutputUtil.colorer(this, style);
|
|
189
|
+
}
|
|
170
190
|
|
|
171
191
|
/** Creates a color palette based on input styles */
|
|
172
|
-
palette
|
|
192
|
+
palette<P extends TermColorPaletteInput>(input: P): TermColorPalette<P> {
|
|
193
|
+
return ColorOutputUtil.palette(this, input);
|
|
194
|
+
}
|
|
173
195
|
|
|
174
196
|
/** Convenience method to creates a color template function based on input styles */
|
|
175
|
-
templateFunction
|
|
176
|
-
|
|
197
|
+
templateFunction<P extends TermColorPaletteInput>(input: P): (key: keyof P, val: Prim) => string {
|
|
198
|
+
return ColorOutputUtil.templateFunction(this, input);
|
|
199
|
+
}
|
|
177
200
|
}
|
|
201
|
+
|
|
178
202
|
export const GlobalTerminal = new Terminal({ output: process.stdout });
|
|
179
203
|
|
|
180
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';
|