@alloy-js/core 0.21.0-dev.1 → 0.21.0-dev.2

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,47 @@
1
+ import { expect, it } from "vitest";
2
+ import { pret, PrettyString } from "./pretty-string.js";
3
+
4
+ function expectRender(
5
+ pretty: PrettyString,
6
+ expected: {
7
+ toString: string;
8
+ toAnsi: string;
9
+ },
10
+ ) {
11
+ expect(pretty.toString()).toBe(expected.toString);
12
+ expect(pretty.toAnsi()).toBe(expected.toAnsi);
13
+ }
14
+
15
+ it("interpolate basic string", () => {
16
+ const result = pret`foo ${"bar"} baz`;
17
+ expectRender(result, {
18
+ toString: "foo bar baz",
19
+ toAnsi: "foo bar baz",
20
+ });
21
+ });
22
+
23
+ // cspell:ignore mbar
24
+ it("interpolate pretty segment", () => {
25
+ const result = pret`foo ${pret.red("bar")} baz`;
26
+ expectRender(result, {
27
+ toString: "foo bar baz",
28
+ toAnsi: "foo \x1b[31mbar\x1b[39m baz",
29
+ });
30
+ });
31
+
32
+ it("interpolate another pretty string with more formatting", () => {
33
+ const a = pret`foo ${pret.red("bar")} baz`;
34
+ const result = pret`Hi, ${pret.bgGreen(a)}`;
35
+ expectRender(result, {
36
+ toString: "Hi, foo bar baz",
37
+ toAnsi: "Hi, \x1b[42mfoo \x1b[31mbar\x1b[39m baz\x1b[49m",
38
+ });
39
+ });
40
+
41
+ it("use full rgb", () => {
42
+ const result = pret`foo ${pret.rgb(123, 45, 67, "bar")} baz`;
43
+ expectRender(result, {
44
+ toString: "foo bar baz",
45
+ toAnsi: "foo \x1b[38;2;123;45;67mbar\x1b[39m baz",
46
+ });
47
+ });
@@ -0,0 +1,130 @@
1
+ import { inspect } from "../inspect.js";
2
+
3
+ export function pret(
4
+ strings: TemplateStringsArray,
5
+ ...keys: PrettyStringSegment[]
6
+ ): PrettyString {
7
+ const result: PrettyStringSegment[] = [strings[0]];
8
+ keys.forEach((key, i) => {
9
+ result.push(key);
10
+ result.push(strings[i + 1]);
11
+ });
12
+
13
+ return new PrettyString(result);
14
+ }
15
+
16
+ pret.black = (x: PrettyStringSegment) => new PrettyStringColored(x, "black");
17
+ pret.red = (x: PrettyStringSegment) => new PrettyStringColored(x, "red");
18
+ pret.green = (x: PrettyStringSegment) => new PrettyStringColored(x, "green");
19
+ pret.yellow = (x: PrettyStringSegment) => new PrettyStringColored(x, "yellow");
20
+ pret.blue = (x: PrettyStringSegment) => new PrettyStringColored(x, "blue");
21
+ pret.magenta = (x: PrettyStringSegment) =>
22
+ new PrettyStringColored(x, "magenta");
23
+ pret.cyan = (x: PrettyStringSegment) => new PrettyStringColored(x, "cyan");
24
+ pret.white = (x: PrettyStringSegment) => new PrettyStringColored(x, "white");
25
+ pret.gray = (x: PrettyStringSegment) => new PrettyStringColored(x, "gray");
26
+
27
+ pret.bgBlack = (x: PrettyStringSegment) =>
28
+ new PrettyStringColored(x, "bgBlack");
29
+ pret.bgRed = (x: PrettyStringSegment) => new PrettyStringColored(x, "bgRed");
30
+ pret.bgGreen = (x: PrettyStringSegment) =>
31
+ new PrettyStringColored(x, "bgGreen");
32
+ pret.bgYellow = (x: PrettyStringSegment) =>
33
+ new PrettyStringColored(x, "bgYellow");
34
+ pret.bgBlue = (x: PrettyStringSegment) => new PrettyStringColored(x, "bgBlue");
35
+ pret.bgMagenta = (x: PrettyStringSegment) =>
36
+ new PrettyStringColored(x, "bgMagenta");
37
+ pret.bgCyan = (x: PrettyStringSegment) => new PrettyStringColored(x, "bgCyan");
38
+ pret.bgWhite = (x: PrettyStringSegment) =>
39
+ new PrettyStringColored(x, "bgWhite");
40
+
41
+ pret.rgb = (r: number, g: number, b: number, x: PrettyStringSegment) =>
42
+ new PrettyStringColored(x, { kind: "rgb", r, g, b });
43
+
44
+ const ansiColors = {
45
+ reset: ["\x1b[0m", "\x1b[0m"],
46
+ bold: ["\x1b[1m", "\x1b[22m"],
47
+ dim: ["\x1b[2m", "\x1b[22m"],
48
+ italic: ["\x1b[3m", "\x1b[23m"],
49
+ underline: ["\x1b[4m", "\x1b[24m"],
50
+ inverse: ["\x1b[7m", "\x1b[27m"],
51
+ hidden: ["\x1b[8m", "\x1b[28m"],
52
+ strikethrough: ["\x1b[9m", "\x1b[29m"],
53
+ black: ["\x1b[30m", "\x1b[39m"],
54
+ red: ["\x1b[31m", "\x1b[39m"],
55
+ green: ["\x1b[32m", "\x1b[39m"],
56
+ yellow: ["\x1b[33m", "\x1b[39m"],
57
+ blue: ["\x1b[34m", "\x1b[39m"],
58
+ magenta: ["\x1b[35m", "\x1b[39m"],
59
+ cyan: ["\x1b[36m", "\x1b[39m"],
60
+ white: ["\x1b[37m", "\x1b[39m"],
61
+ gray: ["\x1b[90m", "\x1b[39m"],
62
+ bgBlack: ["\x1b[40m", "\x1b[49m"],
63
+ bgRed: ["\x1b[41m", "\x1b[49m"],
64
+ bgGreen: ["\x1b[42m", "\x1b[49m"],
65
+ bgYellow: ["\x1b[43m", "\x1b[49m"],
66
+ bgBlue: ["\x1b[44m", "\x1b[49m"],
67
+ bgMagenta: ["\x1b[45m", "\x1b[49m"],
68
+ bgCyan: ["\x1b[46m", "\x1b[49m"],
69
+ bgWhite: ["\x1b[47m", "\x1b[49m"],
70
+ } as Record<string, [string, string]>;
71
+
72
+ export type PrettyStringSegment = string | PrettyStringColored | PrettyString;
73
+
74
+ export interface RgbColor {
75
+ kind: "rgb";
76
+ r: number;
77
+ g: number;
78
+ b: number;
79
+ }
80
+
81
+ export type ColorCodes = keyof typeof ansiColors;
82
+ export type Color = ColorCodes | RgbColor;
83
+ export class PrettyStringColored {
84
+ #value: PrettyStringSegment;
85
+ #color: Color;
86
+
87
+ constructor(value: PrettyStringSegment, color: Color) {
88
+ this.#value = value;
89
+ this.#color = color;
90
+ }
91
+
92
+ toString(): string {
93
+ return this.#value.toString();
94
+ }
95
+
96
+ toAnsi(): string {
97
+ if (typeof this.#color === "string") {
98
+ const [start, end] = ansiColors[this.#color];
99
+ return `${start}${typeof this.#value === "string" ? this.#value : this.#value.toAnsi()}${end}`;
100
+ }
101
+ const { r, g, b } = this.#color;
102
+ const start = `\x1b[38;2;${r};${g};${b}m`;
103
+ const end = `\x1b[39m`;
104
+ return `${start}${typeof this.#value === "string" ? this.#value : this.#value.toAnsi()}${end}`;
105
+ }
106
+ }
107
+
108
+ export class PrettyString {
109
+ #segments: PrettyStringSegment[];
110
+
111
+ constructor(segments: PrettyStringSegment[]) {
112
+ this.#segments = segments;
113
+ }
114
+
115
+ toString(): string {
116
+ return this.#segments
117
+ .map((s) => (typeof s === "string" ? s : s.toString()))
118
+ .join("");
119
+ }
120
+
121
+ toAnsi(): string {
122
+ return this.#segments
123
+ .map((s) => (typeof s === "string" ? s : s.toAnsi()))
124
+ .join("");
125
+ }
126
+
127
+ [inspect.custom]() {
128
+ return this.toAnsi();
129
+ }
130
+ }
package/src/refkey.ts CHANGED
@@ -192,3 +192,16 @@ export function memberRefkey(
192
192
  [RefkeySym]: true,
193
193
  };
194
194
  }
195
+
196
+ export function inspectRefkey(refkey: Refkey): string {
197
+ const text =
198
+ isMemberRefkey(refkey) ?
199
+ `memberRefkey[${inspectRefkey(refkey.base)} -> ${inspectRefkey(refkey.member)}]`
200
+ : `refkey[${refkey.key}]`;
201
+
202
+ return text;
203
+ }
204
+
205
+ export function unresolvedRefkey(refkey: Refkey): string {
206
+ return `<Unresolved Symbol: ${inspectRefkey(refkey)}>`;
207
+ }
package/src/tracer.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { effect, ReactiveEffectRunner } from "@vue/reactivity";
2
2
  import { untrack } from "./reactivity.js";
3
- import { isMemberRefkey, type Refkey } from "./refkey.js";
3
+ import { inspectRefkey, type Refkey } from "./refkey.js";
4
4
  import { scheduler } from "./scheduler.js";
5
5
  import { type OutputScope } from "./symbols/output-scope.js";
6
6
  import type {
@@ -429,10 +429,7 @@ export function formatRefkeys(refkeys: Refkey[] | Refkey | undefined) {
429
429
  }
430
430
 
431
431
  function formatRefkey(refkey: Refkey): string {
432
- const text =
433
- isMemberRefkey(refkey) ?
434
- `memberRefkey[${formatRefkey(refkey.base)} -> ${formatRefkey(refkey.member)}]`
435
- : `refkey[${refkey.key}]`;
432
+ const text = inspectRefkey(refkey);
436
433
 
437
434
  return colorText(text, {
438
435
  fg: {
package/temp/api.json CHANGED
@@ -7668,6 +7668,52 @@
7668
7668
  ],
7669
7669
  "extendsTokenRanges": []
7670
7670
  },
7671
+ {
7672
+ "kind": "Function",
7673
+ "canonicalReference": "@alloy-js/core!inspectRefkey:function(1)",
7674
+ "docComment": "",
7675
+ "excerptTokens": [
7676
+ {
7677
+ "kind": "Content",
7678
+ "text": "export declare function inspectRefkey(refkey: "
7679
+ },
7680
+ {
7681
+ "kind": "Reference",
7682
+ "text": "Refkey",
7683
+ "canonicalReference": "@alloy-js/core!Refkey:type"
7684
+ },
7685
+ {
7686
+ "kind": "Content",
7687
+ "text": "): "
7688
+ },
7689
+ {
7690
+ "kind": "Content",
7691
+ "text": "string"
7692
+ },
7693
+ {
7694
+ "kind": "Content",
7695
+ "text": ";"
7696
+ }
7697
+ ],
7698
+ "fileUrlPath": "src/refkey.ts",
7699
+ "returnTypeTokenRange": {
7700
+ "startIndex": 3,
7701
+ "endIndex": 4
7702
+ },
7703
+ "releaseTag": "Public",
7704
+ "overloadIndex": 1,
7705
+ "parameters": [
7706
+ {
7707
+ "parameterName": "refkey",
7708
+ "parameterTypeTokenRange": {
7709
+ "startIndex": 1,
7710
+ "endIndex": 2
7711
+ },
7712
+ "isOptional": false
7713
+ }
7714
+ ],
7715
+ "name": "inspectRefkey"
7716
+ },
7671
7717
  {
7672
7718
  "kind": "Function",
7673
7719
  "canonicalReference": "@alloy-js/core!instantiateTakenMembersTo:function(1)",
@@ -23233,6 +23279,52 @@
23233
23279
  ],
23234
23280
  "name": "traverseOutput"
23235
23281
  },
23282
+ {
23283
+ "kind": "Function",
23284
+ "canonicalReference": "@alloy-js/core!unresolvedRefkey:function(1)",
23285
+ "docComment": "",
23286
+ "excerptTokens": [
23287
+ {
23288
+ "kind": "Content",
23289
+ "text": "export declare function unresolvedRefkey(refkey: "
23290
+ },
23291
+ {
23292
+ "kind": "Reference",
23293
+ "text": "Refkey",
23294
+ "canonicalReference": "@alloy-js/core!Refkey:type"
23295
+ },
23296
+ {
23297
+ "kind": "Content",
23298
+ "text": "): "
23299
+ },
23300
+ {
23301
+ "kind": "Content",
23302
+ "text": "string"
23303
+ },
23304
+ {
23305
+ "kind": "Content",
23306
+ "text": ";"
23307
+ }
23308
+ ],
23309
+ "fileUrlPath": "src/refkey.ts",
23310
+ "returnTypeTokenRange": {
23311
+ "startIndex": 3,
23312
+ "endIndex": 4
23313
+ },
23314
+ "releaseTag": "Public",
23315
+ "overloadIndex": 1,
23316
+ "parameters": [
23317
+ {
23318
+ "parameterName": "refkey",
23319
+ "parameterTypeTokenRange": {
23320
+ "startIndex": 1,
23321
+ "endIndex": 2
23322
+ },
23323
+ "isOptional": false
23324
+ }
23325
+ ],
23326
+ "name": "unresolvedRefkey"
23327
+ },
23236
23328
  {
23237
23329
  "kind": "Function",
23238
23330
  "canonicalReference": "@alloy-js/core!untrack:function(1)",