@narumitw/pi-starship 0.55.1 → 0.56.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.
- package/README.md +2 -0
- package/dist/chunks/{chunk-UX6PBXVK.js → chunk-SD7TUSSU.js} +113 -113
- package/dist/chunks/chunk-SD7TUSSU.js.map +7 -0
- package/dist/chunks/{chunk-P4XMFSQW.js → chunk-YWY5RIDQ.js} +3 -7
- package/dist/chunks/chunk-YWY5RIDQ.js.map +7 -0
- package/dist/chunks/{chunk-EOGXAMTA.js → chunk-ZQZSXXPI.js} +1 -1
- package/dist/chunks/chunk-ZQZSXXPI.js.map +7 -0
- package/dist/chunks/{cloud-BKHYWCTA.js → cloud-C4VU3TX3.js} +7 -14
- package/dist/chunks/cloud-C4VU3TX3.js.map +7 -0
- package/dist/chunks/{commands-53ZO2JUK.js → commands-MJCAK6JL.js} +18 -42
- package/dist/chunks/commands-MJCAK6JL.js.map +7 -0
- package/dist/chunks/{deployment-RKKOEQQ2.js → deployment-FMNTYRBW.js} +6 -21
- package/dist/chunks/deployment-FMNTYRBW.js.map +7 -0
- package/dist/chunks/{development-BERD42HW.js → development-OUYXDPBS.js} +4 -8
- package/dist/chunks/development-OUYXDPBS.js.map +7 -0
- package/dist/chunks/{execution-42JTEY3N.js → execution-HJOGLMWD.js} +3 -3
- package/dist/chunks/execution-HJOGLMWD.js.map +7 -0
- package/dist/chunks/{languages-RMP6DW7H.js → languages-WXMF4GOF.js} +6 -22
- package/dist/chunks/languages-WXMF4GOF.js.map +7 -0
- package/dist/chunks/{package-LG5H7BBM.js → package-YXRZQAOV.js} +5 -14
- package/dist/chunks/package-YXRZQAOV.js.map +7 -0
- package/dist/index.ts +20 -55
- package/dist/index.ts.map +2 -2
- package/package.json +60 -61
- package/skills/configuring-pi-starship/SKILL.md +18 -4
- package/skills/configuring-pi-starship/references/configuration.md +70 -2
- package/skills/configuring-pi-starship/references/module-catalog.md +21 -0
- package/skills/configuring-pi-starship/references/modules.md +2 -0
- package/skills/configuring-pi-starship/scripts/apply.mjs +48 -47
- package/skills/configuring-pi-starship/scripts/backup.mjs +118 -0
- package/skills/configuring-pi-starship/scripts/config-path.mjs +3 -3
- package/skills/configuring-pi-starship/scripts/script-support.mjs +6 -6
- package/skills/configuring-pi-starship/scripts/validate.mjs +18 -18
- package/src/command-configuration.ts +267 -286
- package/src/command-contract.ts +6 -6
- package/src/command-inspector.ts +27 -27
- package/src/command-preset-picker.ts +45 -50
- package/src/command-preview.ts +153 -166
- package/src/commands.ts +590 -618
- package/src/config.ts +635 -660
- package/src/effective-config.ts +49 -48
- package/src/format/formatter.ts +211 -227
- package/src/format/style.ts +205 -216
- package/src/modules/activity.ts +74 -74
- package/src/modules/brand.ts +9 -9
- package/src/modules/cache.ts +18 -18
- package/src/modules/catalog.ts +79 -83
- package/src/modules/cloud.ts +54 -54
- package/src/modules/context.ts +27 -27
- package/src/modules/cost.ts +23 -23
- package/src/modules/deployment.ts +45 -45
- package/src/modules/development.ts +80 -81
- package/src/modules/directory.ts +89 -113
- package/src/modules/display.ts +9 -9
- package/src/modules/execution.ts +62 -69
- package/src/modules/extension-status.ts +59 -64
- package/src/modules/fill.ts +10 -10
- package/src/modules/git/branch.ts +26 -26
- package/src/modules/git/commit.ts +21 -23
- package/src/modules/git/metrics.ts +22 -22
- package/src/modules/git/runtime.ts +299 -334
- package/src/modules/git/state.ts +17 -17
- package/src/modules/git/status.ts +77 -79
- package/src/modules/git/worktree.ts +10 -12
- package/src/modules/github-pr.ts +20 -20
- package/src/modules/helpers.ts +3 -3
- package/src/modules/index.ts +21 -21
- package/src/modules/inspection.ts +105 -108
- package/src/modules/languages.ts +52 -59
- package/src/modules/model.ts +61 -67
- package/src/modules/package.ts +12 -12
- package/src/modules/provider.ts +25 -24
- package/src/modules/render.ts +131 -141
- package/src/modules/style-rules.ts +21 -0
- package/src/modules/thinking.ts +31 -26
- package/src/modules/time.ts +10 -13
- package/src/modules/tokens.ts +13 -13
- package/src/modules/truncation.ts +14 -17
- package/src/modules/turn.ts +9 -9
- package/src/modules/types.ts +133 -132
- package/src/modules/workspace-helpers.ts +4 -7
- package/src/pi-starship.ts +567 -603
- package/src/presets/catalog.ts +113 -113
- package/src/runtime/cloud.ts +122 -150
- package/src/runtime/command.ts +106 -108
- package/src/runtime/deployment.ts +150 -179
- package/src/runtime/development.ts +107 -124
- package/src/runtime/execution.ts +110 -124
- package/src/runtime/github-pr.ts +223 -252
- package/src/runtime/helpers.ts +186 -199
- package/src/runtime/languages.ts +149 -180
- package/src/runtime/package.ts +74 -87
- package/src/runtime/refresh-controller.ts +82 -86
- package/src/runtime/types.ts +38 -38
- package/src/runtime/workspace.ts +108 -116
- package/src/usage.ts +40 -40
- package/dist/chunks/chunk-EOGXAMTA.js.map +0 -7
- package/dist/chunks/chunk-P4XMFSQW.js.map +0 -7
- package/dist/chunks/chunk-UX6PBXVK.js.map +0 -7
- package/dist/chunks/cloud-BKHYWCTA.js.map +0 -7
- package/dist/chunks/commands-53ZO2JUK.js.map +0 -7
- package/dist/chunks/deployment-RKKOEQQ2.js.map +0 -7
- package/dist/chunks/development-BERD42HW.js.map +0 -7
- package/dist/chunks/execution-42JTEY3N.js.map +0 -7
- package/dist/chunks/languages-RMP6DW7H.js.map +0 -7
- package/dist/chunks/package-LG5H7BBM.js.map +0 -7
package/src/effective-config.ts
CHANGED
|
@@ -4,63 +4,64 @@ import { MODULE_DEFINITIONS } from "./modules/catalog.js";
|
|
|
4
4
|
import type { ModuleOptionValue } from "./modules/types.js";
|
|
5
5
|
|
|
6
6
|
export function projectEffectiveConfig(config: StarshipConfig): TomlTable {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
7
|
+
const projected: TomlTable = {
|
|
8
|
+
format: config.format,
|
|
9
|
+
};
|
|
10
|
+
if (config.palette !== undefined) projected.palette = config.palette;
|
|
11
|
+
projected.palettes = Object.fromEntries(
|
|
12
|
+
sortedEntries(config.palettes).map(([name, colors]) => [name, sortedRecord(colors)]),
|
|
13
|
+
);
|
|
14
|
+
for (const definition of MODULE_DEFINITIONS) {
|
|
15
|
+
const module = config.modules[definition.name];
|
|
16
|
+
const table: TomlTable = {
|
|
17
|
+
format: module.format,
|
|
18
|
+
symbol: module.symbol,
|
|
19
|
+
};
|
|
20
|
+
if (definition.styleDefaults) {
|
|
21
|
+
if (definition.fallbackStyle) table.style = module.style;
|
|
22
|
+
for (const field of Object.keys(definition.styleDefaults)) {
|
|
23
|
+
table[field] = module.styles[field] ?? "";
|
|
24
|
+
}
|
|
25
|
+
} else if (!definition.displayDefaults) {
|
|
26
|
+
table.style = module.style;
|
|
27
|
+
}
|
|
28
|
+
if (definition.displayDefaults) {
|
|
29
|
+
table.display = module.display.map((entry) => ({ ...entry }));
|
|
30
|
+
}
|
|
31
|
+
if (definition.styleRuleSelectors && module.styleRules.length > 0) {
|
|
32
|
+
table.style_rules = module.styleRules.map((rule) => ({ ...rule.selectors, style: rule.style }));
|
|
33
|
+
}
|
|
34
|
+
table.disabled = module.disabled;
|
|
35
|
+
for (const key of Object.keys(definition.options ?? {})) {
|
|
36
|
+
table[key] = cloneOptionValue(module.options[key]);
|
|
37
|
+
}
|
|
38
|
+
if (definition.name === "extension_status") {
|
|
39
|
+
table.separator = config.extensionStatus.separator;
|
|
40
|
+
table.max_statuses = config.extensionStatus.maxStatuses;
|
|
41
|
+
table.icons = sortedRecord(config.extensionStatus.icons);
|
|
42
|
+
}
|
|
43
|
+
projected[definition.name] = table;
|
|
44
|
+
}
|
|
45
|
+
return projected;
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
export function serializeEffectiveConfig(config: StarshipConfig): string {
|
|
46
|
-
|
|
49
|
+
return stringify(projectEffectiveConfig(config));
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
function cloneOptionValue(value: ModuleOptionValue | undefined): TomlValue {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
if (value === undefined) throw new Error("Missing normalized module option");
|
|
54
|
+
if (Array.isArray(value)) return [...(value as readonly string[])];
|
|
55
|
+
if (typeof value === "object") {
|
|
56
|
+
return sortedRecord(value as Readonly<Record<string, string>>);
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
function sortedRecord(values: Readonly<Record<string, string>>): Record<string, string> {
|
|
59
|
-
|
|
62
|
+
return Object.fromEntries(sortedEntries(values));
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
function sortedEntries<Value>(values: Readonly<Record<string, Value>>):
|
|
63
|
-
|
|
64
|
-
left < right ? -1 : left > right ? 1 : 0,
|
|
65
|
-
);
|
|
65
|
+
function sortedEntries<Value>(values: Readonly<Record<string, Value>>): [string, Value][] {
|
|
66
|
+
return Object.entries(values).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0));
|
|
66
67
|
}
|
package/src/format/formatter.ts
CHANGED
|
@@ -1,275 +1,259 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ColorPalette,
|
|
3
|
-
isFillChunk,
|
|
4
|
-
type LayoutChunk,
|
|
5
|
-
parseStyle,
|
|
6
|
-
type TextStyle,
|
|
7
|
-
} from "./style.js";
|
|
1
|
+
import { type ColorPalette, isFillChunk, type LayoutChunk, parseStyle, type TextStyle } from "./style.js";
|
|
8
2
|
|
|
9
3
|
export type FormatNode =
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
| { type: "text"; value: string }
|
|
5
|
+
| { type: "variable"; name: string }
|
|
6
|
+
| { type: "group"; children: FormatNode[]; style: StyleNode[] }
|
|
7
|
+
| { type: "conditional"; children: FormatNode[] };
|
|
14
8
|
|
|
15
9
|
export type StyleNode = { type: "text"; value: string } | { type: "variable"; name: string };
|
|
16
10
|
export type FormatValue = string | readonly LayoutChunk[] | undefined;
|
|
17
11
|
|
|
18
12
|
export interface RenderFormatOptions {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
variables: Readonly<Record<string, FormatValue>>;
|
|
14
|
+
styleVariables?: Readonly<Record<string, string | undefined>>;
|
|
15
|
+
palette?: ColorPalette;
|
|
22
16
|
}
|
|
23
17
|
|
|
24
18
|
export class FormatSyntaxError extends Error {
|
|
25
|
-
|
|
19
|
+
readonly offset: number;
|
|
26
20
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
constructor(message: string, offset: number) {
|
|
22
|
+
super(`${message} at offset ${offset}`);
|
|
23
|
+
this.name = "FormatSyntaxError";
|
|
24
|
+
this.offset = offset;
|
|
25
|
+
}
|
|
32
26
|
}
|
|
33
27
|
|
|
34
28
|
const FUNCTIONAL = new Set(["[", "]", "(", ")", "\\", "$"]);
|
|
35
29
|
|
|
36
30
|
export function parseFormat(format: string): FormatNode[] {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
const parser = new FormatParser(format);
|
|
32
|
+
const nodes = parser.parseNodes();
|
|
33
|
+
if (!parser.done()) throw new FormatSyntaxError("Unexpected character", parser.offset());
|
|
34
|
+
return nodes;
|
|
41
35
|
}
|
|
42
36
|
|
|
43
37
|
class FormatParser {
|
|
44
|
-
|
|
38
|
+
private index = 0;
|
|
45
39
|
|
|
46
|
-
|
|
40
|
+
constructor(private readonly input: string) {}
|
|
47
41
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
offset() {
|
|
43
|
+
return this.index;
|
|
44
|
+
}
|
|
51
45
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
done() {
|
|
47
|
+
return this.index === this.input.length;
|
|
48
|
+
}
|
|
55
49
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
parseNodes(end?: "]" | ")"): FormatNode[] {
|
|
51
|
+
const nodes: FormatNode[] = [];
|
|
52
|
+
let text = "";
|
|
53
|
+
const flushText = () => {
|
|
54
|
+
if (!text) return;
|
|
55
|
+
nodes.push({ type: "text", value: text });
|
|
56
|
+
text = "";
|
|
57
|
+
};
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
59
|
+
while (!this.done()) {
|
|
60
|
+
const current = this.input[this.index];
|
|
61
|
+
if (current === end) {
|
|
62
|
+
flushText();
|
|
63
|
+
this.index += 1;
|
|
64
|
+
return nodes;
|
|
65
|
+
}
|
|
66
|
+
if (current === "\\") {
|
|
67
|
+
const escaped = this.input[this.index + 1];
|
|
68
|
+
if (!escaped || !FUNCTIONAL.has(escaped)) {
|
|
69
|
+
throw new FormatSyntaxError("Invalid escape", this.index);
|
|
70
|
+
}
|
|
71
|
+
text += escaped;
|
|
72
|
+
this.index += 2;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (current === "$") {
|
|
76
|
+
flushText();
|
|
77
|
+
nodes.push({ type: "variable", name: this.parseVariable() });
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (current === "[") {
|
|
81
|
+
flushText();
|
|
82
|
+
this.index += 1;
|
|
83
|
+
const children = this.parseNodes("]");
|
|
84
|
+
if (this.input[this.index] !== "(") {
|
|
85
|
+
throw new FormatSyntaxError("Text group requires a style", this.index);
|
|
86
|
+
}
|
|
87
|
+
this.index += 1;
|
|
88
|
+
nodes.push({ type: "group", children, style: this.parseStyleNodes() });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (current === "(") {
|
|
92
|
+
flushText();
|
|
93
|
+
this.index += 1;
|
|
94
|
+
nodes.push({ type: "conditional", children: this.parseNodes(")") });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (current === "]" || current === ")") {
|
|
98
|
+
throw new FormatSyntaxError(`Unexpected ${current}`, this.index);
|
|
99
|
+
}
|
|
100
|
+
text += current;
|
|
101
|
+
this.index += 1;
|
|
102
|
+
}
|
|
109
103
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
104
|
+
if (end) throw new FormatSyntaxError(`Missing ${end}`, this.index);
|
|
105
|
+
flushText();
|
|
106
|
+
return nodes;
|
|
107
|
+
}
|
|
114
108
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
109
|
+
private parseVariable(): string {
|
|
110
|
+
const start = this.index;
|
|
111
|
+
this.index += 1;
|
|
112
|
+
if (this.input[this.index] === "{") {
|
|
113
|
+
this.index += 1;
|
|
114
|
+
const nameStart = this.index;
|
|
115
|
+
while (!this.done() && this.input[this.index] !== "}") {
|
|
116
|
+
const current = this.input[this.index];
|
|
117
|
+
if (!current || FUNCTIONAL.has(current) || current === "{") {
|
|
118
|
+
throw new FormatSyntaxError("Invalid scoped variable", this.index);
|
|
119
|
+
}
|
|
120
|
+
this.index += 1;
|
|
121
|
+
}
|
|
122
|
+
if (this.done() || this.index === nameStart) {
|
|
123
|
+
throw new FormatSyntaxError("Unclosed scoped variable", start);
|
|
124
|
+
}
|
|
125
|
+
const name = this.input.slice(nameStart, this.index);
|
|
126
|
+
this.index += 1;
|
|
127
|
+
return name;
|
|
128
|
+
}
|
|
129
|
+
const match = /^[A-Za-z_][A-Za-z0-9_]*/u.exec(this.input.slice(this.index));
|
|
130
|
+
if (!match) throw new FormatSyntaxError("Invalid variable", start);
|
|
131
|
+
this.index += match[0].length;
|
|
132
|
+
return match[0];
|
|
133
|
+
}
|
|
140
134
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
135
|
+
private parseStyleNodes(): StyleNode[] {
|
|
136
|
+
const nodes: StyleNode[] = [];
|
|
137
|
+
let text = "";
|
|
138
|
+
const flushText = () => {
|
|
139
|
+
if (!text) return;
|
|
140
|
+
nodes.push({ type: "text", value: text });
|
|
141
|
+
text = "";
|
|
142
|
+
};
|
|
143
|
+
while (!this.done()) {
|
|
144
|
+
const current = this.input[this.index];
|
|
145
|
+
if (current === ")") {
|
|
146
|
+
flushText();
|
|
147
|
+
this.index += 1;
|
|
148
|
+
return nodes;
|
|
149
|
+
}
|
|
150
|
+
if (current === "$") {
|
|
151
|
+
flushText();
|
|
152
|
+
nodes.push({ type: "variable", name: this.parseVariable() });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (current === "(" || current === "[" || current === "]" || current === "\\") {
|
|
156
|
+
throw new FormatSyntaxError("Invalid style character", this.index);
|
|
157
|
+
}
|
|
158
|
+
text += current;
|
|
159
|
+
this.index += 1;
|
|
160
|
+
}
|
|
161
|
+
throw new FormatSyntaxError("Missing )", this.index);
|
|
162
|
+
}
|
|
169
163
|
}
|
|
170
164
|
|
|
171
165
|
export function formatVariables(nodes: readonly FormatNode[]): Set<string> {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
166
|
+
const variables = new Set<string>();
|
|
167
|
+
for (const node of nodes) {
|
|
168
|
+
if (node.type === "variable") variables.add(node.name);
|
|
169
|
+
else if (node.type === "group" || node.type === "conditional") {
|
|
170
|
+
for (const variable of formatVariables(node.children)) variables.add(variable);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return variables;
|
|
180
174
|
}
|
|
181
175
|
|
|
182
176
|
export function styleVariables(nodes: readonly FormatNode[]): Set<string> {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
177
|
+
const variables = new Set<string>();
|
|
178
|
+
for (const node of nodes) {
|
|
179
|
+
if (node.type === "group") {
|
|
180
|
+
for (const part of node.style) {
|
|
181
|
+
if (part.type === "variable") variables.add(part.name);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (node.type === "group" || node.type === "conditional") {
|
|
185
|
+
for (const variable of styleVariables(node.children)) variables.add(variable);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return variables;
|
|
195
189
|
}
|
|
196
190
|
|
|
197
|
-
export function renderFormat(
|
|
198
|
-
|
|
199
|
-
options: RenderFormatOptions,
|
|
200
|
-
): LayoutChunk[] {
|
|
201
|
-
return renderNodes(nodes, options, undefined);
|
|
191
|
+
export function renderFormat(nodes: readonly FormatNode[], options: RenderFormatOptions): LayoutChunk[] {
|
|
192
|
+
return renderNodes(nodes, options, undefined);
|
|
202
193
|
}
|
|
203
194
|
|
|
204
195
|
function renderNodes(
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
196
|
+
nodes: readonly FormatNode[],
|
|
197
|
+
options: RenderFormatOptions,
|
|
198
|
+
inheritedStyle: TextStyle | undefined,
|
|
208
199
|
): LayoutChunk[] {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
return chunks;
|
|
200
|
+
const chunks: LayoutChunk[] = [];
|
|
201
|
+
for (const node of nodes) {
|
|
202
|
+
switch (node.type) {
|
|
203
|
+
case "text":
|
|
204
|
+
chunks.push({ text: node.value, style: inheritedStyle });
|
|
205
|
+
break;
|
|
206
|
+
case "variable":
|
|
207
|
+
chunks.push(...chunksForValue(ownValue(options.variables, node.name), inheritedStyle));
|
|
208
|
+
break;
|
|
209
|
+
case "conditional":
|
|
210
|
+
if (conditionalVisible(node.children, options.variables)) {
|
|
211
|
+
chunks.push(...renderNodes(node.children, options, inheritedStyle));
|
|
212
|
+
}
|
|
213
|
+
break;
|
|
214
|
+
case "group": {
|
|
215
|
+
const styleString = node.style
|
|
216
|
+
.map((part) => (part.type === "text" ? part.value : (ownValue(options.styleVariables, part.name) ?? "")))
|
|
217
|
+
.join("");
|
|
218
|
+
const style = parseStyle(styleString, options.palette);
|
|
219
|
+
const rendered = renderNodes(node.children, options, style);
|
|
220
|
+
if (node.children.length === 0) chunks.push({ text: "", style });
|
|
221
|
+
else chunks.push(...rendered);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return chunks;
|
|
238
227
|
}
|
|
239
228
|
|
|
240
229
|
function ownValue<T>(record: Readonly<Record<string, T>> | undefined, key: string): T | undefined {
|
|
241
|
-
|
|
230
|
+
return record && Object.hasOwn(record, key) ? record[key] : undefined;
|
|
242
231
|
}
|
|
243
232
|
|
|
244
233
|
function chunksForValue(value: FormatValue, inheritedStyle: TextStyle | undefined): LayoutChunk[] {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
234
|
+
if (value === undefined) return [];
|
|
235
|
+
if (typeof value === "string") return [{ text: value, style: inheritedStyle }];
|
|
236
|
+
return value.map((chunk) =>
|
|
237
|
+
isFillChunk(chunk)
|
|
238
|
+
? {
|
|
239
|
+
type: "fill" as const,
|
|
240
|
+
pattern: chunk.pattern.map((part) => ({
|
|
241
|
+
...part,
|
|
242
|
+
style: part.style ?? inheritedStyle,
|
|
243
|
+
})),
|
|
244
|
+
}
|
|
245
|
+
: { ...chunk, style: chunk.style ?? inheritedStyle },
|
|
246
|
+
);
|
|
258
247
|
}
|
|
259
248
|
|
|
260
|
-
function conditionalVisible(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
) {
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return false;
|
|
249
|
+
function conditionalVisible(nodes: readonly FormatNode[], variables: Readonly<Record<string, FormatValue>>): boolean {
|
|
250
|
+
for (const variable of formatVariables(nodes)) {
|
|
251
|
+
const value = ownValue(variables, variable);
|
|
252
|
+
if (
|
|
253
|
+
typeof value === "string" ? value.length > 0 : value?.some((chunk) => isFillChunk(chunk) || chunk.text.length > 0)
|
|
254
|
+
) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return false;
|
|
275
259
|
}
|