From ce9742a20ec7a1b9ea259e0f4c24011b84bb89f9 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sat, 21 Mar 2020 21:13:46 +0100 Subject: [PATCH] Also adding prefix to browser console output --- .editorconfig | 1 + package-lock.json | 2 +- package.json | 2 +- src/consolewriter.ts | 277 ++++++++++++++++++++++--------------------- 4 files changed, 145 insertions(+), 137 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6e966e0..1c81eed 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,4 @@ +[*] charset = utf-8 indent_style = space indent_size = 3 diff --git a/package-lock.json b/package-lock.json index 73dea62..7f480f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@hibas123/logging", - "version": "2.1.4", + "version": "2.1.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 56084da..2e36c72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/logging", - "version": "2.1.4", + "version": "2.1.5", "description": "", "main": "out/index.js", "types": "out/index.d.ts", diff --git a/src/consolewriter.ts b/src/consolewriter.ts index 791a9cf..2a8f662 100644 --- a/src/consolewriter.ts +++ b/src/consolewriter.ts @@ -1,147 +1,154 @@ import { ObservableInterface } from "@hibas123/utils"; import { Colors } from "./index"; -import { Adapter, Message, FormattedLine, TerminalFormats, FormatTypes } from "./types"; +import { + Adapter, + Message, + FormattedLine, + TerminalFormats, + FormatTypes +} from "./types"; const browser = typeof window !== "undefined"; export class ConsoleAdapter implements Adapter { - init(observable: ObservableInterface) { - observable.subscribe(this.onMessage.bind(this)); - } + init(observable: ObservableInterface) { + observable.subscribe(this.onMessage.bind(this)); + } - flush() { } + flush() {} - // TODO: Check if required! - // private escape(text: string): string { - // return text - // .replace(/%s/g, "%%s") - // .replace(/%c/g, "%%c") - // } + // TODO: Check if required! + // private escape(text: string): string { + // return text + // .replace(/%s/g, "%%s") + // .replace(/%c/g, "%%c") + // } - private formatLine(line: FormattedLine): [string, string[] | undefined] { - let text = ""; - let style_formats: string[] = []; + private formatLine(line: FormattedLine): [string, string[] | undefined] { + let text = ""; + let style_formats: string[] = []; - if (!browser) { - for (let part of line) { - let formats = ""; - for (let format of part.formats) { - switch (format.type) { - case FormatTypes.BOLD: - formats += TerminalFormats.Bold; - break; - case FormatTypes.UNDERSCORE: - formats += TerminalFormats.Underscore; - break; - case FormatTypes.BLINK: - formats += TerminalFormats.Blink; - break; - case FormatTypes.COLOR: - switch (format.color) { - case Colors.RED: - formats += TerminalFormats.FgRed; - break; - case Colors.GREEN: - formats += TerminalFormats.FgGreen; - break; - case Colors.YELLOW: - formats += TerminalFormats.FgYellow; - break; - case Colors.BLUE: - formats += TerminalFormats.FgBlue; - break; - case Colors.MAGENTA: - formats += TerminalFormats.FgMagenta; - break; - case Colors.CYAN: - formats += TerminalFormats.FgCyan; - break; - case Colors.WHITE: - formats += TerminalFormats.FgWhite; - break; - } - break; - } - } - text += formats + part.text + TerminalFormats.Reset; - } - } else { - for (let part of line) { - let styles: string[] = []; - let resetStyles: string[] = []; - for (let format of part.formats) { - switch (format.type) { - case FormatTypes.BOLD: - styles.push("font-weight: bold;"); - resetStyles.push("font-weight: unset"); - break; - case FormatTypes.UNDERSCORE: - styles.push("text-decoration: underline"); - resetStyles.push("text-decoration: unset"); - break; - case FormatTypes.BLINK: - styles.push("text-decoration: blink"); - resetStyles.push("text-decoration: unset"); - break; - case FormatTypes.COLOR: - let color = ""; - switch (format.color) { - case Colors.RED: - color = "red"; - break; - case Colors.GREEN: - color = "green"; - break; - case Colors.YELLOW: - color = "gold"; - break; - case Colors.BLUE: - color = "blue"; - break; - case Colors.MAGENTA: - color = "magenta"; - break; - case Colors.CYAN: - color = "cyan"; - break; - case Colors.WHITE: - color = "white"; - break; - } - styles.push("color: " + color); - resetStyles.push("color: unset"); - break; - } - } - text += "%c" + part.text.replace(/%c/g, "%%c") + "%c"; - style_formats.push(styles.join(";"), resetStyles.join(";")); - } + if (!browser) { + for (let part of line) { + let formats = ""; + for (let format of part.formats) { + switch (format.type) { + case FormatTypes.BOLD: + formats += TerminalFormats.Bold; + break; + case FormatTypes.UNDERSCORE: + formats += TerminalFormats.Underscore; + break; + case FormatTypes.BLINK: + formats += TerminalFormats.Blink; + break; + case FormatTypes.COLOR: + switch (format.color) { + case Colors.RED: + formats += TerminalFormats.FgRed; + break; + case Colors.GREEN: + formats += TerminalFormats.FgGreen; + break; + case Colors.YELLOW: + formats += TerminalFormats.FgYellow; + break; + case Colors.BLUE: + formats += TerminalFormats.FgBlue; + break; + case Colors.MAGENTA: + formats += TerminalFormats.FgMagenta; + break; + case Colors.CYAN: + formats += TerminalFormats.FgCyan; + break; + case Colors.WHITE: + formats += TerminalFormats.FgWhite; + break; + } + break; + } + } + text += formats + part.text + TerminalFormats.Reset; } - - return [text, style_formats]; - } - - onMessage(message: Message) { - let lines = message.text.formatted; - - let prefix = ""; - if (message.name) prefix = `[${message.name}]=>`; - - if (browser) { - - let formats: string[] = []; - let text = lines.map(line => { - let [t, fmts] = this.formatLine(line); - formats.push(...fmts); - return t; - }).join("\n"); - // console.log(formats); - console.log(text, ...formats); - } else { - lines.forEach(line => { - let [text] = this.formatLine(line); - console.log(prefix + text); - }) + } else { + for (let part of line) { + let styles: string[] = []; + let resetStyles: string[] = []; + for (let format of part.formats) { + switch (format.type) { + case FormatTypes.BOLD: + styles.push("font-weight: bold;"); + resetStyles.push("font-weight: unset"); + break; + case FormatTypes.UNDERSCORE: + styles.push("text-decoration: underline"); + resetStyles.push("text-decoration: unset"); + break; + case FormatTypes.BLINK: + styles.push("text-decoration: blink"); + resetStyles.push("text-decoration: unset"); + break; + case FormatTypes.COLOR: + let color = ""; + switch (format.color) { + case Colors.RED: + color = "red"; + break; + case Colors.GREEN: + color = "green"; + break; + case Colors.YELLOW: + color = "gold"; + break; + case Colors.BLUE: + color = "blue"; + break; + case Colors.MAGENTA: + color = "magenta"; + break; + case Colors.CYAN: + color = "cyan"; + break; + case Colors.WHITE: + color = "white"; + break; + } + styles.push("color: " + color); + resetStyles.push("color: unset"); + break; + } + } + text += "%c" + part.text.replace(/%c/g, "%%c") + "%c"; + style_formats.push(styles.join(";"), resetStyles.join(";")); } - } -} \ No newline at end of file + } + + return [text, style_formats]; + } + + onMessage(message: Message) { + let lines = message.text.formatted; + + let prefix = ""; + if (message.name) prefix = `[${message.name}]=>`; + + if (browser) { + let formats: string[] = []; + let text = lines + .map(line => { + let [t, fmts] = this.formatLine(line); + formats.push(...fmts); + return prefix + t; + }) + .join("\n"); + // console.log(formats); + console.log(text, ...formats); + } else { + lines.forEach(line => { + let [text] = this.formatLine(line); + console.log(prefix + text); + }); + } + } +}