@narumitw/pi-starship 0.22.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/LICENSE +21 -0
- package/NOTICES.md +21 -0
- package/README.md +166 -0
- package/package.json +52 -0
- package/src/commands.ts +108 -0
- package/src/config.ts +569 -0
- package/src/format/formatter.ts +260 -0
- package/src/format/style.ts +247 -0
- package/src/installed-packages.ts +84 -0
- package/src/modules/activity.ts +38 -0
- package/src/modules/brand.ts +13 -0
- package/src/modules/catalog.ts +41 -0
- package/src/modules/context.ts +21 -0
- package/src/modules/cost.ts +17 -0
- package/src/modules/directory.ts +17 -0
- package/src/modules/extension-status.ts +152 -0
- package/src/modules/git/branch.ts +52 -0
- package/src/modules/git/runtime.ts +111 -0
- package/src/modules/git/status.ts +36 -0
- package/src/modules/git/worktree.ts +16 -0
- package/src/modules/helpers.ts +5 -0
- package/src/modules/index.ts +16 -0
- package/src/modules/model.ts +21 -0
- package/src/modules/provider.ts +13 -0
- package/src/modules/render.ts +73 -0
- package/src/modules/thinking.ts +13 -0
- package/src/modules/time.ts +20 -0
- package/src/modules/tokens.ts +18 -0
- package/src/modules/turn.ts +13 -0
- package/src/modules/types.ts +79 -0
- package/src/pi-starship.ts +325 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { type ColorPalette, parseStyle, type StyledChunk, type TextStyle } from "./style.js";
|
|
2
|
+
|
|
3
|
+
export type FormatNode =
|
|
4
|
+
| { type: "text"; value: string }
|
|
5
|
+
| { type: "variable"; name: string }
|
|
6
|
+
| { type: "group"; children: FormatNode[]; style: StyleNode[] }
|
|
7
|
+
| { type: "conditional"; children: FormatNode[] };
|
|
8
|
+
|
|
9
|
+
export type StyleNode = { type: "text"; value: string } | { type: "variable"; name: string };
|
|
10
|
+
export type FormatValue = string | readonly StyledChunk[] | undefined;
|
|
11
|
+
|
|
12
|
+
export interface RenderFormatOptions {
|
|
13
|
+
variables: Readonly<Record<string, FormatValue>>;
|
|
14
|
+
styleVariables?: Readonly<Record<string, string | undefined>>;
|
|
15
|
+
palette?: ColorPalette;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class FormatSyntaxError extends Error {
|
|
19
|
+
readonly offset: number;
|
|
20
|
+
|
|
21
|
+
constructor(message: string, offset: number) {
|
|
22
|
+
super(`${message} at offset ${offset}`);
|
|
23
|
+
this.name = "FormatSyntaxError";
|
|
24
|
+
this.offset = offset;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const FUNCTIONAL = new Set(["[", "]", "(", ")", "\\", "$"]);
|
|
29
|
+
|
|
30
|
+
export function parseFormat(format: string): FormatNode[] {
|
|
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;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class FormatParser {
|
|
38
|
+
private index = 0;
|
|
39
|
+
|
|
40
|
+
constructor(private readonly input: string) {}
|
|
41
|
+
|
|
42
|
+
offset() {
|
|
43
|
+
return this.index;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
done() {
|
|
47
|
+
return this.index === this.input.length;
|
|
48
|
+
}
|
|
49
|
+
|
|
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
|
+
};
|
|
58
|
+
|
|
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
|
+
}
|
|
103
|
+
|
|
104
|
+
if (end) throw new FormatSyntaxError(`Missing ${end}`, this.index);
|
|
105
|
+
flushText();
|
|
106
|
+
return nodes;
|
|
107
|
+
}
|
|
108
|
+
|
|
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
|
+
}
|
|
134
|
+
|
|
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
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function formatVariables(nodes: readonly FormatNode[]): Set<string> {
|
|
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;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function styleVariables(nodes: readonly FormatNode[]): Set<string> {
|
|
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;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function renderFormat(
|
|
192
|
+
nodes: readonly FormatNode[],
|
|
193
|
+
options: RenderFormatOptions,
|
|
194
|
+
): StyledChunk[] {
|
|
195
|
+
return renderNodes(nodes, options, undefined);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function renderNodes(
|
|
199
|
+
nodes: readonly FormatNode[],
|
|
200
|
+
options: RenderFormatOptions,
|
|
201
|
+
inheritedStyle: TextStyle | undefined,
|
|
202
|
+
): StyledChunk[] {
|
|
203
|
+
const chunks: StyledChunk[] = [];
|
|
204
|
+
for (const node of nodes) {
|
|
205
|
+
switch (node.type) {
|
|
206
|
+
case "text":
|
|
207
|
+
chunks.push({ text: node.value, style: inheritedStyle });
|
|
208
|
+
break;
|
|
209
|
+
case "variable":
|
|
210
|
+
chunks.push(...chunksForValue(ownValue(options.variables, node.name), inheritedStyle));
|
|
211
|
+
break;
|
|
212
|
+
case "conditional":
|
|
213
|
+
if (conditionalVisible(node.children, options.variables)) {
|
|
214
|
+
chunks.push(...renderNodes(node.children, options, inheritedStyle));
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case "group": {
|
|
218
|
+
const styleString = node.style
|
|
219
|
+
.map((part) =>
|
|
220
|
+
part.type === "text" ? part.value : (ownValue(options.styleVariables, part.name) ?? ""),
|
|
221
|
+
)
|
|
222
|
+
.join("");
|
|
223
|
+
const style = parseStyle(styleString, options.palette);
|
|
224
|
+
const rendered = renderNodes(node.children, options, style);
|
|
225
|
+
if (node.children.length === 0) chunks.push({ text: "", style });
|
|
226
|
+
else chunks.push(...rendered);
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return chunks;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function ownValue<T>(record: Readonly<Record<string, T>> | undefined, key: string): T | undefined {
|
|
235
|
+
return record && Object.hasOwn(record, key) ? record[key] : undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function chunksForValue(value: FormatValue, inheritedStyle: TextStyle | undefined): StyledChunk[] {
|
|
239
|
+
if (value === undefined) return [];
|
|
240
|
+
if (typeof value === "string") return [{ text: value, style: inheritedStyle }];
|
|
241
|
+
return value.map((chunk) => ({
|
|
242
|
+
...chunk,
|
|
243
|
+
style: chunk.style ?? inheritedStyle,
|
|
244
|
+
}));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function conditionalVisible(
|
|
248
|
+
nodes: readonly FormatNode[],
|
|
249
|
+
variables: Readonly<Record<string, FormatValue>>,
|
|
250
|
+
): boolean {
|
|
251
|
+
for (const variable of formatVariables(nodes)) {
|
|
252
|
+
const value = ownValue(variables, variable);
|
|
253
|
+
if (
|
|
254
|
+
typeof value === "string" ? value.length > 0 : value?.some((chunk) => chunk.text.length > 0)
|
|
255
|
+
) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
export type NamedColor =
|
|
2
|
+
| "black"
|
|
3
|
+
| "red"
|
|
4
|
+
| "green"
|
|
5
|
+
| "yellow"
|
|
6
|
+
| "blue"
|
|
7
|
+
| "purple"
|
|
8
|
+
| "cyan"
|
|
9
|
+
| "white"
|
|
10
|
+
| "bright-black"
|
|
11
|
+
| "bright-red"
|
|
12
|
+
| "bright-green"
|
|
13
|
+
| "bright-yellow"
|
|
14
|
+
| "bright-blue"
|
|
15
|
+
| "bright-purple"
|
|
16
|
+
| "bright-cyan"
|
|
17
|
+
| "bright-white";
|
|
18
|
+
|
|
19
|
+
export type ColorSpec =
|
|
20
|
+
| { kind: "named"; name: NamedColor }
|
|
21
|
+
| { kind: "fixed"; value: number }
|
|
22
|
+
| { kind: "rgb"; red: number; green: number; blue: number }
|
|
23
|
+
| { kind: "previous"; source: "foreground" | "background" };
|
|
24
|
+
|
|
25
|
+
export interface TextStyle {
|
|
26
|
+
foreground?: ColorSpec;
|
|
27
|
+
background?: ColorSpec;
|
|
28
|
+
bold?: boolean;
|
|
29
|
+
italic?: boolean;
|
|
30
|
+
underline?: boolean;
|
|
31
|
+
dimmed?: boolean;
|
|
32
|
+
inverted?: boolean;
|
|
33
|
+
blink?: boolean;
|
|
34
|
+
hidden?: boolean;
|
|
35
|
+
strikethrough?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface StyledChunk {
|
|
39
|
+
text: string;
|
|
40
|
+
style?: TextStyle;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type ColorPalette = Readonly<Record<string, string>>;
|
|
44
|
+
|
|
45
|
+
const NAMED_COLORS = new Set<NamedColor>([
|
|
46
|
+
"black",
|
|
47
|
+
"red",
|
|
48
|
+
"green",
|
|
49
|
+
"yellow",
|
|
50
|
+
"blue",
|
|
51
|
+
"purple",
|
|
52
|
+
"cyan",
|
|
53
|
+
"white",
|
|
54
|
+
"bright-black",
|
|
55
|
+
"bright-red",
|
|
56
|
+
"bright-green",
|
|
57
|
+
"bright-yellow",
|
|
58
|
+
"bright-blue",
|
|
59
|
+
"bright-purple",
|
|
60
|
+
"bright-cyan",
|
|
61
|
+
"bright-white",
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
const FOREGROUND_CODES: Record<NamedColor, number> = {
|
|
65
|
+
black: 30,
|
|
66
|
+
red: 31,
|
|
67
|
+
green: 32,
|
|
68
|
+
yellow: 33,
|
|
69
|
+
blue: 34,
|
|
70
|
+
purple: 35,
|
|
71
|
+
cyan: 36,
|
|
72
|
+
white: 37,
|
|
73
|
+
"bright-black": 90,
|
|
74
|
+
"bright-red": 91,
|
|
75
|
+
"bright-green": 92,
|
|
76
|
+
"bright-yellow": 93,
|
|
77
|
+
"bright-blue": 94,
|
|
78
|
+
"bright-purple": 95,
|
|
79
|
+
"bright-cyan": 96,
|
|
80
|
+
"bright-white": 97,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export function isValidStyle(styleString: string, palette: ColorPalette = {}): boolean {
|
|
84
|
+
for (const rawToken of styleString.split(/\s+/u).filter(Boolean)) {
|
|
85
|
+
const token = rawToken.toLowerCase();
|
|
86
|
+
if (token === "none" || token === "fg:none" || token === "bg:none") continue;
|
|
87
|
+
const probe: TextStyle = {};
|
|
88
|
+
if (applyModifier(probe, token)) continue;
|
|
89
|
+
const colorToken = token.startsWith("fg:") || token.startsWith("bg:") ? token.slice(3) : token;
|
|
90
|
+
if (!parseColor(colorToken, palette)) return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function parseStyle(styleString: string, palette: ColorPalette = {}): TextStyle | undefined {
|
|
96
|
+
const style: TextStyle = {};
|
|
97
|
+
for (const rawToken of styleString.split(/\s+/u).filter(Boolean)) {
|
|
98
|
+
const token = rawToken.toLowerCase();
|
|
99
|
+
if (token === "none") return undefined;
|
|
100
|
+
if (token === "fg:none") {
|
|
101
|
+
delete style.foreground;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (applyModifier(style, token)) continue;
|
|
105
|
+
|
|
106
|
+
const foreground = token.startsWith("fg:");
|
|
107
|
+
const background = token.startsWith("bg:");
|
|
108
|
+
const colorToken = foreground || background ? token.slice(3) : token;
|
|
109
|
+
if (background && colorToken === "none") {
|
|
110
|
+
delete style.background;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const color = parseColor(colorToken, palette);
|
|
114
|
+
if (!color) return undefined;
|
|
115
|
+
if (background) style.background = color;
|
|
116
|
+
else style.foreground = color;
|
|
117
|
+
}
|
|
118
|
+
return Object.keys(style).length > 0 ? style : undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function applyModifier(style: TextStyle, token: string): boolean {
|
|
122
|
+
switch (token) {
|
|
123
|
+
case "bold":
|
|
124
|
+
style.bold = true;
|
|
125
|
+
return true;
|
|
126
|
+
case "italic":
|
|
127
|
+
style.italic = true;
|
|
128
|
+
return true;
|
|
129
|
+
case "underline":
|
|
130
|
+
style.underline = true;
|
|
131
|
+
return true;
|
|
132
|
+
case "dimmed":
|
|
133
|
+
style.dimmed = true;
|
|
134
|
+
return true;
|
|
135
|
+
case "inverted":
|
|
136
|
+
style.inverted = true;
|
|
137
|
+
return true;
|
|
138
|
+
case "blink":
|
|
139
|
+
style.blink = true;
|
|
140
|
+
return true;
|
|
141
|
+
case "hidden":
|
|
142
|
+
style.hidden = true;
|
|
143
|
+
return true;
|
|
144
|
+
case "strikethrough":
|
|
145
|
+
style.strikethrough = true;
|
|
146
|
+
return true;
|
|
147
|
+
default:
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function parseColor(token: string, palette: ColorPalette = {}): ColorSpec | undefined {
|
|
153
|
+
if (token === "prev_fg") return { kind: "previous", source: "foreground" };
|
|
154
|
+
if (token === "prev_bg") return { kind: "previous", source: "background" };
|
|
155
|
+
const paletteValue = Object.hasOwn(palette, token) ? palette[token] : undefined;
|
|
156
|
+
if (paletteValue !== undefined) return parseColor(paletteValue.toLowerCase(), {});
|
|
157
|
+
if (NAMED_COLORS.has(token as NamedColor)) {
|
|
158
|
+
return { kind: "named", name: token as NamedColor };
|
|
159
|
+
}
|
|
160
|
+
if (/^\d{1,3}$/u.test(token)) {
|
|
161
|
+
const value = Number(token);
|
|
162
|
+
return value <= 255 ? { kind: "fixed", value } : undefined;
|
|
163
|
+
}
|
|
164
|
+
const rgb = /^#([0-9a-f]{6})$/iu.exec(token);
|
|
165
|
+
if (!rgb?.[1]) return undefined;
|
|
166
|
+
return {
|
|
167
|
+
kind: "rgb",
|
|
168
|
+
red: Number.parseInt(rgb[1].slice(0, 2), 16),
|
|
169
|
+
green: Number.parseInt(rgb[1].slice(2, 4), 16),
|
|
170
|
+
blue: Number.parseInt(rgb[1].slice(4, 6), 16),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
interface ResolvedStyle extends Omit<TextStyle, "foreground" | "background"> {
|
|
175
|
+
foreground?: Exclude<ColorSpec, { kind: "previous" }>;
|
|
176
|
+
background?: Exclude<ColorSpec, { kind: "previous" }>;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function renderChunksToAnsi(chunks: readonly StyledChunk[]): string {
|
|
180
|
+
const runs: Array<{ text: string; style: ResolvedStyle }> = [];
|
|
181
|
+
let previous: ResolvedStyle | undefined;
|
|
182
|
+
for (const chunk of chunks) {
|
|
183
|
+
const style = resolveStyle(chunk.style, previous);
|
|
184
|
+
const last = runs.at(-1);
|
|
185
|
+
if (chunk.text && last && stylesEqual(last.style, style)) last.text += chunk.text;
|
|
186
|
+
else if (chunk.text) runs.push({ text: chunk.text, style });
|
|
187
|
+
previous = style;
|
|
188
|
+
}
|
|
189
|
+
return runs
|
|
190
|
+
.map(({ text, style }) => {
|
|
191
|
+
const codes = ansiCodes(style);
|
|
192
|
+
return codes.length > 0 ? `\u001b[${codes.join(";")}m${text}\u001b[0m` : text;
|
|
193
|
+
})
|
|
194
|
+
.join("");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function stylesEqual(left: ResolvedStyle, right: ResolvedStyle): boolean {
|
|
198
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function resolveStyle(
|
|
202
|
+
style: TextStyle | undefined,
|
|
203
|
+
previous: ResolvedStyle | undefined,
|
|
204
|
+
): ResolvedStyle {
|
|
205
|
+
if (!style) return {};
|
|
206
|
+
return {
|
|
207
|
+
...style,
|
|
208
|
+
foreground: resolveColor(style.foreground, previous),
|
|
209
|
+
background: resolveColor(style.background, previous),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function resolveColor(
|
|
214
|
+
color: ColorSpec | undefined,
|
|
215
|
+
previous: ResolvedStyle | undefined,
|
|
216
|
+
): ResolvedStyle["foreground"] {
|
|
217
|
+
if (!color) return undefined;
|
|
218
|
+
if (color.kind !== "previous") return color;
|
|
219
|
+
return color.source === "foreground" ? previous?.foreground : previous?.background;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function ansiCodes(style: ResolvedStyle): string[] {
|
|
223
|
+
const codes: string[] = [];
|
|
224
|
+
if (style.foreground) codes.push(...colorCodes(style.foreground, false));
|
|
225
|
+
if (style.background) codes.push(...colorCodes(style.background, true));
|
|
226
|
+
if (style.bold) codes.push("1");
|
|
227
|
+
if (style.dimmed) codes.push("2");
|
|
228
|
+
if (style.italic) codes.push("3");
|
|
229
|
+
if (style.underline) codes.push("4");
|
|
230
|
+
if (style.blink) codes.push("5");
|
|
231
|
+
if (style.inverted) codes.push("7");
|
|
232
|
+
if (style.hidden) codes.push("8");
|
|
233
|
+
if (style.strikethrough) codes.push("9");
|
|
234
|
+
return codes;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function colorCodes(
|
|
238
|
+
color: Exclude<ColorSpec, { kind: "previous" }>,
|
|
239
|
+
background: boolean,
|
|
240
|
+
): string[] {
|
|
241
|
+
if (color.kind === "named") {
|
|
242
|
+
const foreground = FOREGROUND_CODES[color.name];
|
|
243
|
+
return [`${background ? foreground + 10 : foreground}`];
|
|
244
|
+
}
|
|
245
|
+
if (color.kind === "fixed") return [background ? "48" : "38", "5", `${color.value}`];
|
|
246
|
+
return [background ? "48" : "38", "2", `${color.red}`, `${color.green}`, `${color.blue}`];
|
|
247
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import {
|
|
5
|
+
buildExtensionStatusIconAliases,
|
|
6
|
+
type ExtensionStatusIconAliasMap,
|
|
7
|
+
} from "./modules/index.js";
|
|
8
|
+
|
|
9
|
+
export interface InstalledPackage {
|
|
10
|
+
packageName: string;
|
|
11
|
+
source: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface InstalledPackageInfo {
|
|
15
|
+
packages: InstalledPackage[];
|
|
16
|
+
aliases: ExtensionStatusIconAliasMap;
|
|
17
|
+
hasStatuslineConflict: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function readInstalledPackageInfo(
|
|
21
|
+
agentDir: string,
|
|
22
|
+
cwd: string,
|
|
23
|
+
projectTrusted: boolean,
|
|
24
|
+
): InstalledPackageInfo {
|
|
25
|
+
const settingsPaths = [
|
|
26
|
+
join(agentDir, "settings.json"),
|
|
27
|
+
...(projectTrusted ? [join(cwd, CONFIG_DIR_NAME, "settings.json")] : []),
|
|
28
|
+
];
|
|
29
|
+
const packages = settingsPaths.flatMap(readPackagesFromSettings);
|
|
30
|
+
return {
|
|
31
|
+
packages,
|
|
32
|
+
aliases: buildExtensionStatusIconAliases(packages),
|
|
33
|
+
hasStatuslineConflict: packages.some(
|
|
34
|
+
(item) =>
|
|
35
|
+
item.packageName === "@narumitw/pi-statusline" || item.packageName === "pi-statusline",
|
|
36
|
+
),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readPackagesFromSettings(settingsPath: string): InstalledPackage[] {
|
|
41
|
+
if (!existsSync(settingsPath)) return [];
|
|
42
|
+
let entries: unknown[];
|
|
43
|
+
try {
|
|
44
|
+
const parsed = JSON.parse(readFileSync(settingsPath, "utf8")) as { packages?: unknown };
|
|
45
|
+
entries = Array.isArray(parsed.packages) ? parsed.packages : [];
|
|
46
|
+
} catch {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
const baseDirectory = dirname(settingsPath);
|
|
50
|
+
return entries.flatMap((entry) => {
|
|
51
|
+
const source =
|
|
52
|
+
typeof entry === "string"
|
|
53
|
+
? entry
|
|
54
|
+
: entry &&
|
|
55
|
+
typeof entry === "object" &&
|
|
56
|
+
typeof (entry as { source?: unknown }).source === "string"
|
|
57
|
+
? (entry as { source: string }).source
|
|
58
|
+
: undefined;
|
|
59
|
+
if (!source) return [];
|
|
60
|
+
const packageName = packageNameForSource(source, baseDirectory);
|
|
61
|
+
return packageName ? [{ packageName, source }] : [];
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function packageNameForSource(source: string, baseDirectory: string): string | undefined {
|
|
66
|
+
if (source.startsWith("npm:")) return npmPackageName(source);
|
|
67
|
+
const packageJson = join(resolveSourcePath(source, baseDirectory), "package.json");
|
|
68
|
+
try {
|
|
69
|
+
const parsed = JSON.parse(readFileSync(packageJson, "utf8")) as { name?: unknown };
|
|
70
|
+
return typeof parsed.name === "string" ? parsed.name : undefined;
|
|
71
|
+
} catch {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function npmPackageName(source: string): string {
|
|
77
|
+
const spec = source.slice("npm:".length);
|
|
78
|
+
if (spec.startsWith("@")) return spec.split("@").slice(0, 2).join("@").replace(/^@/u, "@");
|
|
79
|
+
return spec.split("@")[0] ?? spec;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function resolveSourcePath(source: string, baseDirectory: string): string {
|
|
83
|
+
return isAbsolute(source) ? source : resolve(baseDirectory, source);
|
|
84
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { defineModule } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export const activityModule = defineModule({
|
|
4
|
+
name: "activity",
|
|
5
|
+
variables: ["symbol", "state", "tool", "count", "text"],
|
|
6
|
+
defaults: {
|
|
7
|
+
format: "[ $text ]($style)",
|
|
8
|
+
symbol: "⚙",
|
|
9
|
+
style: "fg:runtime_fg bg:runtime",
|
|
10
|
+
disabled: false,
|
|
11
|
+
},
|
|
12
|
+
values: ({ runtime, symbol }) => {
|
|
13
|
+
const active = [...runtime.activeTools.entries()];
|
|
14
|
+
if (active.length > 0) {
|
|
15
|
+
const [tool = "tool", count = 1] = active[0] ?? [];
|
|
16
|
+
const suffix = count > 1 ? `×${count}` : "";
|
|
17
|
+
const more = active.length > 1 ? `+${active.length - 1}` : "";
|
|
18
|
+
return {
|
|
19
|
+
state: "active",
|
|
20
|
+
tool,
|
|
21
|
+
count: `${count}`,
|
|
22
|
+
text: `${symbol} ${tool}${suffix}${more}`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (runtime.isStreaming) {
|
|
26
|
+
return { state: "thinking", tool: "", count: "0", text: `${symbol} thinking` };
|
|
27
|
+
}
|
|
28
|
+
if (runtime.lastCompletedTool) {
|
|
29
|
+
return {
|
|
30
|
+
state: "completed",
|
|
31
|
+
tool: runtime.lastCompletedTool,
|
|
32
|
+
count: "0",
|
|
33
|
+
text: `${symbol} completed ${runtime.lastCompletedTool}`,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return { state: "idle", tool: "", count: "0", text: `${symbol} idle` };
|
|
37
|
+
},
|
|
38
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineModule } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export const brandModule = defineModule({
|
|
4
|
+
name: "brand",
|
|
5
|
+
variables: ["symbol"],
|
|
6
|
+
defaults: {
|
|
7
|
+
format: "[ $symbol ]($style)",
|
|
8
|
+
symbol: "π",
|
|
9
|
+
style: "bold fg:header_fg bg:header",
|
|
10
|
+
disabled: false,
|
|
11
|
+
},
|
|
12
|
+
values: () => ({}),
|
|
13
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { activityModule } from "./activity.js";
|
|
2
|
+
import { brandModule } from "./brand.js";
|
|
3
|
+
import { contextModule } from "./context.js";
|
|
4
|
+
import { costModule } from "./cost.js";
|
|
5
|
+
import { directoryModule } from "./directory.js";
|
|
6
|
+
import { extensionStatusModule } from "./extension-status.js";
|
|
7
|
+
import { gitBranchModule } from "./git/branch.js";
|
|
8
|
+
import { gitStatusModule } from "./git/status.js";
|
|
9
|
+
import { gitWorktreeModule } from "./git/worktree.js";
|
|
10
|
+
import { modelModule } from "./model.js";
|
|
11
|
+
import { providerModule } from "./provider.js";
|
|
12
|
+
import { thinkingModule } from "./thinking.js";
|
|
13
|
+
import { timeModule } from "./time.js";
|
|
14
|
+
import { tokensModule } from "./tokens.js";
|
|
15
|
+
import { turnModule } from "./turn.js";
|
|
16
|
+
import type { ModuleDefinition } from "./types.js";
|
|
17
|
+
|
|
18
|
+
export const MODULE_DEFINITIONS = [
|
|
19
|
+
brandModule,
|
|
20
|
+
providerModule,
|
|
21
|
+
modelModule,
|
|
22
|
+
thinkingModule,
|
|
23
|
+
directoryModule,
|
|
24
|
+
gitWorktreeModule,
|
|
25
|
+
gitBranchModule,
|
|
26
|
+
gitStatusModule,
|
|
27
|
+
activityModule,
|
|
28
|
+
contextModule,
|
|
29
|
+
tokensModule,
|
|
30
|
+
costModule,
|
|
31
|
+
timeModule,
|
|
32
|
+
turnModule,
|
|
33
|
+
// Render last so earlier modules can consume extension-owned status values.
|
|
34
|
+
extensionStatusModule,
|
|
35
|
+
] as const satisfies readonly ModuleDefinition<string>[];
|
|
36
|
+
|
|
37
|
+
export type ModuleName = (typeof MODULE_DEFINITIONS)[number]["name"];
|
|
38
|
+
|
|
39
|
+
export const MODULE_NAMES: readonly ModuleName[] = MODULE_DEFINITIONS.map(
|
|
40
|
+
(definition) => definition.name,
|
|
41
|
+
);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { formatCount } from "./helpers.js";
|
|
2
|
+
import { defineModule } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export const contextModule = defineModule({
|
|
5
|
+
name: "context",
|
|
6
|
+
variables: ["symbol", "percentage", "tokens", "window"],
|
|
7
|
+
defaults: {
|
|
8
|
+
format: "[$symbol ctx $percentage ]($style)",
|
|
9
|
+
symbol: "🪟",
|
|
10
|
+
style: "fg:runtime_fg bg:runtime",
|
|
11
|
+
disabled: false,
|
|
12
|
+
},
|
|
13
|
+
values: ({ runtime }) => {
|
|
14
|
+
const percent = runtime.contextUsage?.percent;
|
|
15
|
+
return {
|
|
16
|
+
percentage: percent === null || percent === undefined ? "?" : `${percent.toFixed(0)}%`,
|
|
17
|
+
tokens: formatCount(runtime.contextUsage?.tokens ?? 0),
|
|
18
|
+
window: formatCount(runtime.contextUsage?.contextWindow ?? 0),
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
});
|