Create TimestampTransformer and use it in EwtConsole (#501)

This commit is contained in:
Peter Zich 2024-04-17 19:43:27 -07:00 committed by GitHub
parent ec35b6aa69
commit 9261c2c24e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import { ColoredConsole, coloredConsoleStyles } from "../util/console-color"; import { ColoredConsole, coloredConsoleStyles } from "../util/console-color";
import { sleep } from "../util/sleep"; import { sleep } from "../util/sleep";
import { LineBreakTransformer } from "../util/line-break-transformer"; import { LineBreakTransformer } from "../util/line-break-transformer";
import { TimestampTransformer } from "../util/timestamp-transformer";
import { Logger } from "../const"; import { Logger } from "../const";
export class EwtConsole extends HTMLElement { export class EwtConsole extends HTMLElement {
@ -95,6 +96,7 @@ export class EwtConsole extends HTMLElement {
signal: abortSignal, signal: abortSignal,
}) })
.pipeThrough(new TransformStream(new LineBreakTransformer())) .pipeThrough(new TransformStream(new LineBreakTransformer()))
.pipeThrough(new TransformStream(new TimestampTransformer()))
.pipeTo( .pipeTo(
new WritableStream({ new WritableStream({
write: (chunk) => { write: (chunk) => {

View File

@ -0,0 +1,12 @@
export class TimestampTransformer implements Transformer<string, string> {
transform(
chunk: string,
controller: TransformStreamDefaultController<string>,
) {
const date = new Date();
const h = date.getHours().toString().padStart(2, "0");
const m = date.getMinutes().toString().padStart(2, "0");
const s = date.getSeconds().toString().padStart(2, "0");
controller.enqueue(`[${h}:${m}:${s}]${chunk}`);
}
}