@holz/ansi-terminal-backend 0.1.0 → 0.4.0

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.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c=require("@holz/core");function n(l){return`\x1B[${l}`}const t=n("0m"),a=n("2m"),d=n("31m"),g=n("32m"),$=n("33m"),p=n("34m");class u{constructor(e={}){this.console=e.console??console}processLog(e){const r=this.getTimestamp(new Date),s=[{include:!0,command:"%s",content:`${t}${a}${r}${t}`},{include:!0,command:"%s",content:L[e.level]},{include:!0,command:"%s",content:e.message},{include:e.origin.length>0,command:"%s",content:`${a}${e.origin.join(":")}${t}`},{include:Object.keys(e.context).length>0,command:"%O",content:e.context}].filter(o=>o.include),i=s.map(o=>o.command).join(" "),m=s.map(o=>o.content);this.console.error(i,...m)}getTimestamp(e){const r=e.getHours().toString().padStart(2,"0"),s=e.getMinutes().toString().padStart(2,"0"),i=e.getSeconds().toString().padStart(2,"0"),m=e.getMilliseconds().toString().padStart(3,"0");return`[${r}:${s}:${i}.${m}]`}}const L={[c.LogLevel.Debug]:`${p}DEBUG${t}`,[c.LogLevel.Info]:`${g}INFO${t} `,[c.LogLevel.Warn]:`${$}WARN${t} `,[c.LogLevel.Error]:`${d}ERROR${t}`};exports.AnsiTerminalBackend=u;exports.default=u;
@@ -1,68 +1,55 @@
1
- var s = /* @__PURE__ */ ((o) => (o.Error = "error", o.Warn = "warn", o.Info = "info", o.Debug = "debug", o))(s || {});
2
- function t(o) {
3
- return `\x1B[${o}`;
1
+ import { LogLevel as c } from "@holz/core";
2
+ function e(a) {
3
+ return `\x1B[${a}`;
4
4
  }
5
- const m = {
6
- black: t("30m"),
7
- red: t("31m"),
8
- green: t("32m"),
9
- yellow: t("33m"),
10
- blue: t("34m"),
11
- magenta: t("35m"),
12
- cyan: t("36m"),
13
- white: t("37m")
14
- }, n = {
15
- reset: t("0m"),
16
- bold: t("1m"),
17
- dim: t("2m")
18
- };
19
- class u {
20
- constructor(e = {}) {
21
- this.console = e.console ?? console;
5
+ const n = e("0m"), l = e("2m"), d = e("31m"), u = e("32m"), $ = e("33m"), g = e("34m");
6
+ class f {
7
+ constructor(t = {}) {
8
+ this.console = t.console ?? console;
22
9
  }
23
- processLog(e) {
24
- const i = this.getTimestamp(new Date()), c = [
10
+ processLog(t) {
11
+ const r = this.getTimestamp(new Date()), s = [
25
12
  {
26
13
  include: !0,
27
14
  command: "%s",
28
- content: `${n.reset}${n.dim}${i}${n.reset}`
15
+ content: `${n}${l}${r}${n}`
29
16
  },
30
17
  {
31
18
  include: !0,
32
19
  command: "%s",
33
- content: d[e.level]
20
+ content: p[t.level]
34
21
  },
35
22
  {
36
23
  include: !0,
37
24
  command: "%s",
38
- content: e.message
25
+ content: t.message
39
26
  },
40
27
  {
41
- include: e.origin.length > 0,
28
+ include: t.origin.length > 0,
42
29
  command: "%s",
43
- content: `${n.dim}${e.origin.join(":")}${n.reset}`
30
+ content: `${l}${t.origin.join(":")}${n}`
44
31
  },
45
32
  {
46
- include: Object.keys(e.context).length > 0,
33
+ include: Object.keys(t.context).length > 0,
47
34
  command: "%O",
48
- content: e.context
35
+ content: t.context
49
36
  }
50
- ].filter((r) => r.include), a = c.map((r) => r.command).join(" "), l = c.map((r) => r.content);
51
- this.console.error(a, ...l);
37
+ ].filter((o) => o.include), i = s.map((o) => o.command).join(" "), m = s.map((o) => o.content);
38
+ this.console.error(i, ...m);
52
39
  }
53
40
  // ISO-8601 timestamp with milliseconds.
54
- getTimestamp(e) {
55
- const i = e.getHours().toString().padStart(2, "0"), c = e.getMinutes().toString().padStart(2, "0"), a = e.getSeconds().toString().padStart(2, "0"), l = e.getMilliseconds().toString().padStart(3, "0");
56
- return `[${i}:${c}:${a}.${l}]`;
41
+ getTimestamp(t) {
42
+ const r = t.getHours().toString().padStart(2, "0"), s = t.getMinutes().toString().padStart(2, "0"), i = t.getSeconds().toString().padStart(2, "0"), m = t.getMilliseconds().toString().padStart(3, "0");
43
+ return `[${r}:${s}:${i}.${m}]`;
57
44
  }
58
45
  }
59
- const d = {
60
- [s.Debug]: `${m.blue}DEBUG${n.reset}`,
61
- [s.Info]: `${m.green}INFO${n.reset} `,
62
- [s.Warn]: `${m.yellow}WARN${n.reset} `,
63
- [s.Error]: `${m.red}ERROR${n.reset}`
46
+ const p = {
47
+ [c.Debug]: `${g}DEBUG${n}`,
48
+ [c.Info]: `${u}INFO${n} `,
49
+ [c.Warn]: `${$}WARN${n} `,
50
+ [c.Error]: `${d}ERROR${n}`
64
51
  };
65
52
  export {
66
- u as AnsiTerminalBackend,
67
- u as default
53
+ f as AnsiTerminalBackend,
54
+ f as default
68
55
  };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@holz/ansi-terminal-backend",
3
- "version": "0.1.0",
3
+ "version": "0.4.0",
4
4
  "description": "An ANSI terminal backend for Holz",
5
5
  "type": "module",
6
- "main": "./dist/holz-ansi-terminal-backend.umd.cjs",
6
+ "main": "./dist/holz-ansi-terminal-backend.cjs",
7
7
  "module": "./dist/holz-ansi-terminal-backend.js",
8
8
  "types": "./src/index.ts",
9
9
  "repository": {
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "exports": {
15
15
  ".": {
16
- "require": "./dist/holz-ansi-terminal-backend.umd.cjs",
16
+ "require": "./dist/holz-ansi-terminal-backend.cjs",
17
17
  "import": "./dist/holz-ansi-terminal-backend.js"
18
18
  }
19
19
  },
@@ -33,12 +33,15 @@
33
33
  "terminal"
34
34
  ],
35
35
  "scripts": {
36
- "prepare": "vite build",
36
+ "prepack": "vite build",
37
37
  "test:unit": "vitest --color --passWithNoTests",
38
38
  "test:types": "tsc"
39
39
  },
40
+ "peerDependencies": {
41
+ "@holz/core": "0.2.0"
42
+ },
40
43
  "devDependencies": {
41
- "@holz/core": "0.1.0",
44
+ "@holz/core": "0.2.0",
42
45
  "@types/node": "^18.14.0",
43
46
  "@vitest/coverage-c8": "0.28.5",
44
47
  "typescript": "4.9.5",
package/src/ansi-codes.ts CHANGED
@@ -3,25 +3,19 @@ function ansiCode(code: string) {
3
3
  }
4
4
 
5
5
  /**
6
- * Unix only. This will break for win32 terminals and may spew garbage on
7
- * terminals without 4-bit color support.
8
- *
9
- * TODO: Strip colors on non-interactive TTYs.
6
+ * Assumes 3-bit color support for an ANSI terminal. This should work on most
7
+ * modern terminals, including on Windows.
10
8
  */
11
9
 
12
- export const color = {
13
- black: ansiCode('30m'),
14
- red: ansiCode('31m'),
15
- green: ansiCode('32m'),
16
- yellow: ansiCode('33m'),
17
- blue: ansiCode('34m'),
18
- magenta: ansiCode('35m'),
19
- cyan: ansiCode('36m'),
20
- white: ansiCode('37m'),
21
- };
10
+ export const reset = ansiCode('0m');
11
+ export const bold = ansiCode('1m');
12
+ export const dim = ansiCode('2m');
22
13
 
23
- export const code = {
24
- reset: ansiCode('0m'),
25
- bold: ansiCode('1m'),
26
- dim: ansiCode('2m'),
27
- };
14
+ export const black = ansiCode('30m');
15
+ export const red = ansiCode('31m');
16
+ export const green = ansiCode('32m');
17
+ export const yellow = ansiCode('33m');
18
+ export const blue = ansiCode('34m');
19
+ export const magenta = ansiCode('35m');
20
+ export const cyan = ansiCode('36m');
21
+ export const white = ansiCode('37m');
@@ -1,6 +1,6 @@
1
1
  import type { Log, LogProcessor } from '@holz/core';
2
2
  import { LogLevel } from '@holz/core';
3
- import { color, code } from './ansi-codes';
3
+ import * as ansi from './ansi-codes';
4
4
 
5
5
  /**
6
6
  * A backend that prints logs to a 3-bit ansi terminal. This should work on
@@ -24,7 +24,7 @@ export default class AnsiTerminalBackend implements LogProcessor {
24
24
  {
25
25
  include: true,
26
26
  command: '%s',
27
- content: `${code.reset}${code.dim}${timestamp}${code.reset}`,
27
+ content: `${ansi.reset}${ansi.dim}${timestamp}${ansi.reset}`,
28
28
  },
29
29
  {
30
30
  include: true,
@@ -39,7 +39,7 @@ export default class AnsiTerminalBackend implements LogProcessor {
39
39
  {
40
40
  include: log.origin.length > 0,
41
41
  command: '%s',
42
- content: `${code.dim}${log.origin.join(':')}${code.reset}`,
42
+ content: `${ansi.dim}${log.origin.join(':')}${ansi.reset}`,
43
43
  },
44
44
  {
45
45
  include: Object.keys(log.context).length > 0,
@@ -68,10 +68,10 @@ export default class AnsiTerminalBackend implements LogProcessor {
68
68
 
69
69
  // Trailing whitespace is important for alignment.
70
70
  const logLevelLabel: Record<LogLevel, string> = {
71
- [LogLevel.Debug]: `${color.blue}DEBUG${code.reset}`,
72
- [LogLevel.Info]: `${color.green}INFO${code.reset} `,
73
- [LogLevel.Warn]: `${color.yellow}WARN${code.reset} `,
74
- [LogLevel.Error]: `${color.red}ERROR${code.reset}`,
71
+ [LogLevel.Debug]: `${ansi.blue}DEBUG${ansi.reset}`,
72
+ [LogLevel.Info]: `${ansi.green}INFO${ansi.reset} `,
73
+ [LogLevel.Warn]: `${ansi.yellow}WARN${ansi.reset} `,
74
+ [LogLevel.Error]: `${ansi.red}ERROR${ansi.reset}`,
75
75
  };
76
76
 
77
77
  interface Options {
@@ -1 +0,0 @@
1
- (function(r,t){typeof exports=="object"&&typeof module<"u"?t(exports):typeof define=="function"&&define.amd?define(["exports"],t):(r=typeof globalThis<"u"?globalThis:r||self,t(r["holz-ansi-terminal-backend"]={}))})(this,function(r){"use strict";var t=(s=>(s.Error="error",s.Warn="warn",s.Info="info",s.Debug="debug",s))(t||{});function n(s){return`\x1B[${s}`}const c={black:n("30m"),red:n("31m"),green:n("32m"),yellow:n("33m"),blue:n("34m"),magenta:n("35m"),cyan:n("36m"),white:n("37m")},o={reset:n("0m"),bold:n("1m"),dim:n("2m")};class u{constructor(e={}){this.console=e.console??console}processLog(e){const d=this.getTimestamp(new Date),m=[{include:!0,command:"%s",content:`${o.reset}${o.dim}${d}${o.reset}`},{include:!0,command:"%s",content:g[e.level]},{include:!0,command:"%s",content:e.message},{include:e.origin.length>0,command:"%s",content:`${o.dim}${e.origin.join(":")}${o.reset}`},{include:Object.keys(e.context).length>0,command:"%O",content:e.context}].filter(i=>i.include),a=m.map(i=>i.command).join(" "),l=m.map(i=>i.content);this.console.error(a,...l)}getTimestamp(e){const d=e.getHours().toString().padStart(2,"0"),m=e.getMinutes().toString().padStart(2,"0"),a=e.getSeconds().toString().padStart(2,"0"),l=e.getMilliseconds().toString().padStart(3,"0");return`[${d}:${m}:${a}.${l}]`}}const g={[t.Debug]:`${c.blue}DEBUG${o.reset}`,[t.Info]:`${c.green}INFO${o.reset} `,[t.Warn]:`${c.yellow}WARN${o.reset} `,[t.Error]:`${c.red}ERROR${o.reset}`};r.AnsiTerminalBackend=u,r.default=u,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});