Răsfoiți Sursa

Create TimestampTransformer and use it in EwtConsole (#501)

Peter Zich 1 an în urmă
părinte
comite
9261c2c24e
2 a modificat fișierele cu 14 adăugiri și 0 ștergeri
  1. 2 0
      src/components/ewt-console.ts
  2. 12 0
      src/util/timestamp-transformer.ts

+ 2 - 0
src/components/ewt-console.ts

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

+ 12 - 0
src/util/timestamp-transformer.ts

@@ -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}`);
+  }
+}