@narumitw/pi-starship 0.45.0 → 0.49.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 +61 -8
- package/package.json +2 -2
- package/src/command-contract.ts +13 -0
- package/src/command-inspector.ts +2 -274
- package/src/commands.ts +77 -55
- package/src/config.ts +12 -8
- package/src/modules/development.ts +23 -6
- package/src/modules/directory.ts +140 -6
- package/src/modules/execution.ts +1 -1
- package/src/modules/git/branch.ts +13 -4
- package/src/modules/git/commit.ts +9 -2
- package/src/modules/git/runtime.ts +3 -0
- package/src/modules/model.ts +14 -64
- package/src/modules/terminal.ts +60 -0
- package/src/modules/truncation.ts +37 -0
- package/src/modules/types.ts +3 -0
- package/src/pi-starship.ts +31 -3
- package/src/runtime/development.ts +1 -2
- package/src/runtime/execution.ts +4 -6
- package/src/runtime/workspace.ts +39 -19
package/src/config.ts
CHANGED
|
@@ -8,8 +8,9 @@ import {
|
|
|
8
8
|
rmSync,
|
|
9
9
|
writeFileSync,
|
|
10
10
|
} from "node:fs";
|
|
11
|
+
import { createRequire } from "node:module";
|
|
11
12
|
import { dirname, join } from "node:path";
|
|
12
|
-
import {
|
|
13
|
+
import type { TomlTable } from "smol-toml";
|
|
13
14
|
import {
|
|
14
15
|
type FormatNode,
|
|
15
16
|
formatVariables,
|
|
@@ -83,7 +84,8 @@ $activity\
|
|
|
83
84
|
$context\
|
|
84
85
|
$time"""`;
|
|
85
86
|
|
|
86
|
-
const BUILT_IN_FORMAT =
|
|
87
|
+
const BUILT_IN_FORMAT =
|
|
88
|
+
"$brand$model$thinking$directory$git_branch$git_status$activity$context$time";
|
|
87
89
|
|
|
88
90
|
const BUILT_IN_MODULES = Object.fromEntries(
|
|
89
91
|
MODULE_DEFINITIONS.map(({ name, defaults, styleDefaults, displayDefaults, options }) => [
|
|
@@ -114,10 +116,12 @@ export const BUILT_IN_CONFIG: StarshipConfig = {
|
|
|
114
116
|
|
|
115
117
|
export const BUILT_IN_EXAMPLE = `# Native Pi modules with Starship-compatible format and style syntax.\n${BUILT_IN_FORMAT_DOCUMENT}\n`;
|
|
116
118
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
const require = createRequire(import.meta.url);
|
|
120
|
+
let parseTomlImplementation: typeof import("smol-toml")["parse"] | undefined;
|
|
121
|
+
|
|
122
|
+
function parseToml(document: string): TomlTable {
|
|
123
|
+
parseTomlImplementation ??= (require("smol-toml") as typeof import("smol-toml")).parse;
|
|
124
|
+
return parseTomlImplementation(document);
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
export function settingsFilePath(agentDir: string): string {
|
|
@@ -147,7 +151,7 @@ export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
|
|
|
147
151
|
|
|
148
152
|
let parsed: TomlTable;
|
|
149
153
|
try {
|
|
150
|
-
parsed =
|
|
154
|
+
parsed = parseToml(rawDocument);
|
|
151
155
|
} catch (error) {
|
|
152
156
|
return {
|
|
153
157
|
config: cloneBuiltInConfig(),
|
|
@@ -413,7 +417,7 @@ export function validateConfigDocument(
|
|
|
413
417
|
): LoadedStarshipConfig {
|
|
414
418
|
let parsed: TomlTable;
|
|
415
419
|
try {
|
|
416
|
-
parsed =
|
|
420
|
+
parsed = parseToml(rawDocument);
|
|
417
421
|
} catch (error) {
|
|
418
422
|
throw new Error(`Unable to parse TOML: ${formatError(error)}`);
|
|
419
423
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { truncatePathComponents } from "./truncation.js";
|
|
1
2
|
import { defineModule, type ModuleDefinition, type ModuleOptionSchema } from "./types.js";
|
|
2
3
|
import { workspaceModuleValues } from "./workspace-helpers.js";
|
|
3
4
|
|
|
@@ -47,13 +48,29 @@ export const direnvModule = developmentModule({
|
|
|
47
48
|
options: directDetection,
|
|
48
49
|
});
|
|
49
50
|
|
|
50
|
-
export const condaModule =
|
|
51
|
+
export const condaModule = defineModule({
|
|
51
52
|
name: "conda",
|
|
52
|
-
variables: ["environment"],
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
variables: ["symbol", "environment"],
|
|
54
|
+
defaults: {
|
|
55
|
+
format: "via [$symbol$environment]($style) ",
|
|
56
|
+
symbol: "🅒 ",
|
|
57
|
+
style: "green bold",
|
|
58
|
+
disabled: false,
|
|
59
|
+
},
|
|
60
|
+
options: {
|
|
61
|
+
ignore_base: { kind: "boolean", default: true },
|
|
62
|
+
truncation_length: { kind: "integer", default: 1, minimum: 0, maximum: 1_000_000 },
|
|
63
|
+
},
|
|
64
|
+
values: (context) => {
|
|
65
|
+
const values = workspaceModuleValues("conda", context);
|
|
66
|
+
const environment = values?.environment;
|
|
67
|
+
if (!environment) return undefined;
|
|
68
|
+
const length =
|
|
69
|
+
typeof context.options.truncation_length === "number" ? context.options.truncation_length : 1;
|
|
70
|
+
return {
|
|
71
|
+
environment: truncatePathComponents(environment, length).value,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
57
74
|
});
|
|
58
75
|
|
|
59
76
|
export const pixiModule = developmentModule({
|
package/src/modules/directory.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { basename } from "node:path";
|
|
2
|
-
import {
|
|
1
|
+
import { basename, isAbsolute, relative, sep } from "node:path";
|
|
2
|
+
import { sanitizeTerminalText } from "./terminal.js";
|
|
3
|
+
import {
|
|
4
|
+
graphemes,
|
|
5
|
+
toSlashPath,
|
|
6
|
+
truncatePathComponents,
|
|
7
|
+
useNativePathSeparator,
|
|
8
|
+
} from "./truncation.js";
|
|
9
|
+
import { defineModule, type ModuleOptionValue } from "./types.js";
|
|
3
10
|
|
|
4
11
|
export const directoryModule = defineModule({
|
|
5
12
|
name: "directory",
|
|
@@ -10,8 +17,135 @@ export const directoryModule = defineModule({
|
|
|
10
17
|
style: "cyan bold",
|
|
11
18
|
disabled: false,
|
|
12
19
|
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
options: {
|
|
21
|
+
truncation_length: { kind: "integer", default: 3, minimum: 0, maximum: 1_000_000 },
|
|
22
|
+
truncate_to_repo: { kind: "boolean", default: true },
|
|
23
|
+
fish_style_pwd_dir_length: { kind: "integer", default: 0, minimum: 0, maximum: 1_000 },
|
|
24
|
+
truncation_symbol: { kind: "string", default: "" },
|
|
25
|
+
home_symbol: { kind: "string", default: "~" },
|
|
26
|
+
use_os_path_sep: { kind: "boolean", default: true },
|
|
27
|
+
substitutions: { kind: "string-map", default: {} },
|
|
28
|
+
},
|
|
29
|
+
values: ({ runtime, options }) => {
|
|
30
|
+
const fullPath = runtime.cwd;
|
|
31
|
+
const home = runtime.homeDir;
|
|
32
|
+
const repoRoot = runtime.gitRoot;
|
|
33
|
+
const homeSymbol = stringOption(options, "home_symbol", "~");
|
|
34
|
+
const truncateToRepo = booleanOption(options, "truncate_to_repo", true);
|
|
35
|
+
const substitutions = mapOption(options, "substitutions");
|
|
36
|
+
const homeContracted = contractPath(fullPath, home, homeSymbol);
|
|
37
|
+
const repoContracted =
|
|
38
|
+
truncateToRepo && repoRoot && (!home || !samePath(repoRoot, home))
|
|
39
|
+
? contractRepositoryPath(fullPath, repoRoot)
|
|
40
|
+
: undefined;
|
|
41
|
+
let path = sanitizeTerminalText(repoContracted ?? homeContracted);
|
|
42
|
+
let truncated = repoContracted !== undefined;
|
|
43
|
+
|
|
44
|
+
for (const [from, to] of Object.entries(substitutions)) {
|
|
45
|
+
if (from) path = path.replaceAll(from, to);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const componentResult = truncatePathComponents(
|
|
49
|
+
path,
|
|
50
|
+
numberOption(options, "truncation_length", 3),
|
|
51
|
+
);
|
|
52
|
+
path = componentResult.value;
|
|
53
|
+
truncated ||= componentResult.truncated;
|
|
54
|
+
|
|
55
|
+
if (truncated) {
|
|
56
|
+
const fishLength = numberOption(options, "fish_style_pwd_dir_length", 0);
|
|
57
|
+
if (fishLength > 0 && Object.keys(substitutions).length === 0) {
|
|
58
|
+
path = `${fishPrefix(homeContracted, path, fishLength)}${path}`;
|
|
59
|
+
} else {
|
|
60
|
+
path = `${stringOption(options, "truncation_symbol", "")}${path}`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (booleanOption(options, "use_os_path_sep", true)) path = useNativePathSeparator(path);
|
|
65
|
+
const fallback = basename(fullPath) || fullPath;
|
|
66
|
+
return {
|
|
67
|
+
path: sanitizeTerminalText(path || fallback),
|
|
68
|
+
full_path: sanitizeTerminalText(fullPath),
|
|
69
|
+
};
|
|
70
|
+
},
|
|
17
71
|
});
|
|
72
|
+
|
|
73
|
+
function contractPath(path: string, root: string | undefined, replacement: string): string {
|
|
74
|
+
if (!root) return toSlashPath(path);
|
|
75
|
+
const child = relativeWithin(path, root);
|
|
76
|
+
if (child === undefined) return toSlashPath(path);
|
|
77
|
+
return child ? `${replacement}/${child}` : replacement;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function contractRepositoryPath(path: string, root: string): string | undefined {
|
|
81
|
+
const child = relativeWithin(path, root);
|
|
82
|
+
if (child === undefined) return undefined;
|
|
83
|
+
const name = basename(root) || toSlashPath(root);
|
|
84
|
+
return child ? `${name}/${child}` : name;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function relativeWithin(path: string, root: string): string | undefined {
|
|
88
|
+
const child = relative(root, path);
|
|
89
|
+
if (child === "") return "";
|
|
90
|
+
if (child === ".." || child.startsWith(`..${sep}`) || isAbsolute(child)) return undefined;
|
|
91
|
+
return toSlashPath(child);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function samePath(left: string, right: string): boolean {
|
|
95
|
+
return relative(left, right) === "";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function fishPrefix(source: string, displayed: string, length: number): string {
|
|
99
|
+
const prefix = source.endsWith(displayed) ? source.slice(0, -displayed.length) : "";
|
|
100
|
+
if (!prefix) return "";
|
|
101
|
+
return prefix
|
|
102
|
+
.split("/")
|
|
103
|
+
.map((component) => abbreviateComponent(component, length))
|
|
104
|
+
.join("/");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function abbreviateComponent(component: string, length: number): string {
|
|
108
|
+
if (!component) return "";
|
|
109
|
+
const parts = graphemes(component);
|
|
110
|
+
if (parts.length <= length) return component;
|
|
111
|
+
return component.startsWith(".")
|
|
112
|
+
? parts.slice(0, length + 1).join("")
|
|
113
|
+
: parts.slice(0, length).join("");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function numberOption(
|
|
117
|
+
options: Readonly<Record<string, ModuleOptionValue>>,
|
|
118
|
+
name: string,
|
|
119
|
+
fallback: number,
|
|
120
|
+
): number {
|
|
121
|
+
const value = options[name];
|
|
122
|
+
return typeof value === "number" ? value : fallback;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function stringOption(
|
|
126
|
+
options: Readonly<Record<string, ModuleOptionValue>>,
|
|
127
|
+
name: string,
|
|
128
|
+
fallback: string,
|
|
129
|
+
): string {
|
|
130
|
+
const value = options[name];
|
|
131
|
+
return typeof value === "string" ? value : fallback;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function booleanOption(
|
|
135
|
+
options: Readonly<Record<string, ModuleOptionValue>>,
|
|
136
|
+
name: string,
|
|
137
|
+
fallback: boolean,
|
|
138
|
+
): boolean {
|
|
139
|
+
const value = options[name];
|
|
140
|
+
return typeof value === "boolean" ? value : fallback;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function mapOption(
|
|
144
|
+
options: Readonly<Record<string, ModuleOptionValue>>,
|
|
145
|
+
name: string,
|
|
146
|
+
): Readonly<Record<string, string>> {
|
|
147
|
+
const value = options[name];
|
|
148
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
149
|
+
? (value as Readonly<Record<string, string>>)
|
|
150
|
+
: {};
|
|
151
|
+
}
|
package/src/modules/execution.ts
CHANGED
|
@@ -42,7 +42,7 @@ export const hostnameModule = defineModule({
|
|
|
42
42
|
},
|
|
43
43
|
options: {
|
|
44
44
|
ssh_only: { kind: "boolean", default: true },
|
|
45
|
-
trim_at: { kind: "string", default: "."
|
|
45
|
+
trim_at: { kind: "string", default: "." },
|
|
46
46
|
aliases: { kind: "string-map", default: {} },
|
|
47
47
|
},
|
|
48
48
|
values: (context) => workspaceModuleValues("hostname", context),
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { truncateLeadingGraphemes } from "../truncation.js";
|
|
1
2
|
import { defineModule } from "../types.js";
|
|
2
3
|
|
|
3
4
|
export const gitBranchModule = defineModule({
|
|
@@ -9,14 +10,22 @@ export const gitBranchModule = defineModule({
|
|
|
9
10
|
style: "bold purple",
|
|
10
11
|
disabled: false,
|
|
11
12
|
},
|
|
12
|
-
|
|
13
|
+
options: {
|
|
14
|
+
// Starship uses i64::MAX as its effective no-truncation default. Zero is the
|
|
15
|
+
// equivalent stable TOML representation in pi-starship's bounded integer schema.
|
|
16
|
+
truncation_length: { kind: "integer", default: 0, minimum: 0, maximum: 1_000_000 },
|
|
17
|
+
truncation_symbol: { kind: "string", default: "…" },
|
|
18
|
+
},
|
|
19
|
+
values: ({ runtime, options }) => {
|
|
13
20
|
const branch = runtime.gitBranchDetails;
|
|
14
21
|
const name = branch?.name ?? runtime.gitBranch;
|
|
15
22
|
if (!name) return undefined;
|
|
23
|
+
const length = typeof options.truncation_length === "number" ? options.truncation_length : 0;
|
|
24
|
+
const symbol = typeof options.truncation_symbol === "string" ? options.truncation_symbol : "…";
|
|
16
25
|
return {
|
|
17
|
-
branch: name,
|
|
18
|
-
remote_name: branch?.remoteName ?? "",
|
|
19
|
-
remote_branch: branch?.remoteBranch ?? "",
|
|
26
|
+
branch: truncateLeadingGraphemes(name, length, symbol),
|
|
27
|
+
remote_name: truncateLeadingGraphemes(branch?.remoteName ?? "", length, symbol),
|
|
28
|
+
remote_branch: truncateLeadingGraphemes(branch?.remoteBranch ?? "", length, symbol),
|
|
20
29
|
};
|
|
21
30
|
},
|
|
22
31
|
});
|
|
@@ -11,11 +11,18 @@ export const gitCommitModule = defineModule({
|
|
|
11
11
|
style: "green bold",
|
|
12
12
|
disabled: false,
|
|
13
13
|
},
|
|
14
|
-
|
|
14
|
+
options: {
|
|
15
|
+
commit_hash_length: { kind: "integer", default: DEFAULT_HASH_LENGTH, minimum: 0, maximum: 64 },
|
|
16
|
+
},
|
|
17
|
+
values: ({ runtime, options }) => {
|
|
15
18
|
const commit = runtime.gitCommit;
|
|
16
19
|
if (!commit) return undefined;
|
|
20
|
+
const hashLength =
|
|
21
|
+
typeof options.commit_hash_length === "number"
|
|
22
|
+
? options.commit_hash_length
|
|
23
|
+
: DEFAULT_HASH_LENGTH;
|
|
17
24
|
return {
|
|
18
|
-
hash: commit.hash.slice(0,
|
|
25
|
+
hash: commit.hash.slice(0, hashLength),
|
|
19
26
|
tag: commit.tag ? ` 🏷 ${commit.tag}` : "",
|
|
20
27
|
};
|
|
21
28
|
},
|
|
@@ -77,6 +77,7 @@ export async function readGitSnapshot(
|
|
|
77
77
|
|
|
78
78
|
return {
|
|
79
79
|
...parsed,
|
|
80
|
+
root: metadata?.root,
|
|
80
81
|
commit,
|
|
81
82
|
state: metadata ? parseGitState(metadata.gitDirectory) : undefined,
|
|
82
83
|
metrics,
|
|
@@ -250,6 +251,7 @@ export function parseGitState(gitDirectory: string): GitStateSnapshot | undefine
|
|
|
250
251
|
}
|
|
251
252
|
|
|
252
253
|
interface GitRepositoryMetadata {
|
|
254
|
+
root: string;
|
|
253
255
|
gitDirectory: string;
|
|
254
256
|
worktree?: GitWorktreeSnapshot;
|
|
255
257
|
}
|
|
@@ -261,6 +263,7 @@ function parseGitRepositoryMetadata(output: string): GitRepositoryMetadata | und
|
|
|
261
263
|
if (!path || !commonDir || !gitDirectory) return undefined;
|
|
262
264
|
if (![path, commonDir, gitDirectory].every(isAbsolute)) return undefined;
|
|
263
265
|
return {
|
|
266
|
+
root: path,
|
|
264
267
|
gitDirectory,
|
|
265
268
|
worktree: samePath(commonDir, gitDirectory)
|
|
266
269
|
? undefined
|
package/src/modules/model.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sanitizeTerminalText } from "./terminal.js";
|
|
1
2
|
import { defineModule } from "./types.js";
|
|
2
3
|
|
|
3
4
|
const TRUNCATION_DIRECTIONS = ["start", "middle", "end"] as const;
|
|
@@ -21,6 +22,7 @@ export const modelModule = defineModule({
|
|
|
21
22
|
default: "end",
|
|
22
23
|
values: TRUNCATION_DIRECTIONS,
|
|
23
24
|
},
|
|
25
|
+
model_aliases: { kind: "string-map", default: {} },
|
|
24
26
|
},
|
|
25
27
|
values: ({ runtime, options }) => {
|
|
26
28
|
if (!runtime.model) return undefined;
|
|
@@ -29,8 +31,17 @@ export const modelModule = defineModule({
|
|
|
29
31
|
const direction = isTruncationDirection(options.truncation_direction)
|
|
30
32
|
? options.truncation_direction
|
|
31
33
|
: "end";
|
|
34
|
+
const aliases = options.model_aliases;
|
|
35
|
+
const aliasMap =
|
|
36
|
+
aliases && typeof aliases === "object" && !Array.isArray(aliases)
|
|
37
|
+
? (aliases as Readonly<Record<string, string>>)
|
|
38
|
+
: undefined;
|
|
39
|
+
const alias =
|
|
40
|
+
aliasMap && Object.hasOwn(aliasMap, runtime.model.id)
|
|
41
|
+
? aliasMap[runtime.model.id]
|
|
42
|
+
: undefined;
|
|
32
43
|
return {
|
|
33
|
-
model: truncateModel(shortenModel(runtime.model.id), length, symbol, direction),
|
|
44
|
+
model: truncateModel(alias ?? shortenModel(runtime.model.id), length, symbol, direction),
|
|
34
45
|
};
|
|
35
46
|
},
|
|
36
47
|
});
|
|
@@ -41,11 +52,11 @@ export function truncateModel(
|
|
|
41
52
|
symbol: string,
|
|
42
53
|
direction: TruncationDirection,
|
|
43
54
|
): string {
|
|
44
|
-
const safeModel =
|
|
55
|
+
const safeModel = sanitizeTerminalText(model);
|
|
45
56
|
if (length === 0) return safeModel;
|
|
46
57
|
const graphemes = [...graphemeSegmenter.segment(safeModel)].map(({ segment }) => segment);
|
|
47
58
|
if (graphemes.length <= length) return safeModel;
|
|
48
|
-
const safeSymbol =
|
|
59
|
+
const safeSymbol = sanitizeTerminalText(symbol);
|
|
49
60
|
|
|
50
61
|
switch (direction) {
|
|
51
62
|
case "start":
|
|
@@ -61,67 +72,6 @@ export function truncateModel(
|
|
|
61
72
|
}
|
|
62
73
|
}
|
|
63
74
|
|
|
64
|
-
function sanitizeModelDisplay(value: string): string {
|
|
65
|
-
let result = "";
|
|
66
|
-
for (let index = 0; index < value.length; ) {
|
|
67
|
-
const codePoint = value.codePointAt(index) ?? 0;
|
|
68
|
-
const character = String.fromCodePoint(codePoint);
|
|
69
|
-
if (codePoint === 0x1b || codePoint === 0x9b || codePoint === 0x9d) {
|
|
70
|
-
index = skipTerminalEscape(value, index, codePoint);
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
if (
|
|
74
|
-
codePoint <= 0x1f ||
|
|
75
|
-
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
76
|
-
codePoint === 0x2028 ||
|
|
77
|
-
codePoint === 0x2029
|
|
78
|
-
) {
|
|
79
|
-
if (
|
|
80
|
-
codePoint === 0x09 ||
|
|
81
|
-
codePoint === 0x0a ||
|
|
82
|
-
codePoint === 0x0d ||
|
|
83
|
-
codePoint === 0x85 ||
|
|
84
|
-
codePoint === 0x2028 ||
|
|
85
|
-
codePoint === 0x2029
|
|
86
|
-
) {
|
|
87
|
-
result += " ";
|
|
88
|
-
}
|
|
89
|
-
index += character.length;
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
result += character;
|
|
93
|
-
index += character.length;
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function skipTerminalEscape(value: string, start: number, codePoint: number): number {
|
|
99
|
-
let index = start + 1;
|
|
100
|
-
const next = value.charCodeAt(index);
|
|
101
|
-
const isOsc = codePoint === 0x9d || (codePoint === 0x1b && next === 0x5d);
|
|
102
|
-
if (isOsc) {
|
|
103
|
-
if (codePoint === 0x1b) index += 1;
|
|
104
|
-
while (index < value.length) {
|
|
105
|
-
const current = value.charCodeAt(index);
|
|
106
|
-
if (current === 0x07 || current === 0x9c) return index + 1;
|
|
107
|
-
if (current === 0x1b && value.charCodeAt(index + 1) === 0x5c) return index + 2;
|
|
108
|
-
index += 1;
|
|
109
|
-
}
|
|
110
|
-
return index;
|
|
111
|
-
}
|
|
112
|
-
const isCsi = codePoint === 0x9b || (codePoint === 0x1b && next === 0x5b);
|
|
113
|
-
if (isCsi) {
|
|
114
|
-
if (codePoint === 0x1b) index += 1;
|
|
115
|
-
while (index < value.length) {
|
|
116
|
-
const current = value.charCodeAt(index);
|
|
117
|
-
index += 1;
|
|
118
|
-
if (current >= 0x40 && current <= 0x7e) break;
|
|
119
|
-
}
|
|
120
|
-
return index;
|
|
121
|
-
}
|
|
122
|
-
return Math.min(value.length, start + (codePoint === 0x1b ? 2 : 1));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
75
|
function isTruncationDirection(value: unknown): value is TruncationDirection {
|
|
126
76
|
return TRUNCATION_DIRECTIONS.includes(value as TruncationDirection);
|
|
127
77
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export function sanitizeTerminalText(value: string): string {
|
|
2
|
+
let result = "";
|
|
3
|
+
for (let index = 0; index < value.length; ) {
|
|
4
|
+
const codePoint = value.codePointAt(index) ?? 0;
|
|
5
|
+
const character = String.fromCodePoint(codePoint);
|
|
6
|
+
if (codePoint === 0x1b || codePoint === 0x9b || codePoint === 0x9d) {
|
|
7
|
+
index = skipTerminalEscape(value, index, codePoint);
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
if (
|
|
11
|
+
codePoint <= 0x1f ||
|
|
12
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
13
|
+
codePoint === 0x2028 ||
|
|
14
|
+
codePoint === 0x2029
|
|
15
|
+
) {
|
|
16
|
+
if (
|
|
17
|
+
codePoint === 0x09 ||
|
|
18
|
+
codePoint === 0x0a ||
|
|
19
|
+
codePoint === 0x0d ||
|
|
20
|
+
codePoint === 0x85 ||
|
|
21
|
+
codePoint === 0x2028 ||
|
|
22
|
+
codePoint === 0x2029
|
|
23
|
+
) {
|
|
24
|
+
result += " ";
|
|
25
|
+
}
|
|
26
|
+
index += character.length;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
result += character;
|
|
30
|
+
index += character.length;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function skipTerminalEscape(value: string, start: number, codePoint: number): number {
|
|
36
|
+
let index = start + 1;
|
|
37
|
+
const next = value.charCodeAt(index);
|
|
38
|
+
const isOsc = codePoint === 0x9d || (codePoint === 0x1b && next === 0x5d);
|
|
39
|
+
if (isOsc) {
|
|
40
|
+
if (codePoint === 0x1b) index += 1;
|
|
41
|
+
while (index < value.length) {
|
|
42
|
+
const current = value.charCodeAt(index);
|
|
43
|
+
if (current === 0x07 || current === 0x9c) return index + 1;
|
|
44
|
+
if (current === 0x1b && value.charCodeAt(index + 1) === 0x5c) return index + 2;
|
|
45
|
+
index += 1;
|
|
46
|
+
}
|
|
47
|
+
return index;
|
|
48
|
+
}
|
|
49
|
+
const isCsi = codePoint === 0x9b || (codePoint === 0x1b && next === 0x5b);
|
|
50
|
+
if (isCsi) {
|
|
51
|
+
if (codePoint === 0x1b) index += 1;
|
|
52
|
+
while (index < value.length) {
|
|
53
|
+
const current = value.charCodeAt(index);
|
|
54
|
+
index += 1;
|
|
55
|
+
if (current >= 0x40 && current <= 0x7e) break;
|
|
56
|
+
}
|
|
57
|
+
return index;
|
|
58
|
+
}
|
|
59
|
+
return Math.min(value.length, start + (codePoint === 0x1b ? 2 : 1));
|
|
60
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { sep } from "node:path";
|
|
2
|
+
|
|
3
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
4
|
+
|
|
5
|
+
export function graphemes(value: string): string[] {
|
|
6
|
+
return [...graphemeSegmenter.segment(value)].map(({ segment }) => segment);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function firstGrapheme(value: string): string {
|
|
10
|
+
return graphemes(value)[0] ?? "";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function truncateLeadingGraphemes(value: string, length: number, symbol: string): string {
|
|
14
|
+
if (length <= 0) return value;
|
|
15
|
+
const parts = graphemes(value);
|
|
16
|
+
if (parts.length <= length) return value;
|
|
17
|
+
return `${parts.slice(0, length).join("")}${firstGrapheme(symbol)}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function toSlashPath(value: string): string {
|
|
21
|
+
return sep === "\\" ? value.replaceAll("\\", "/") : value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function truncatePathComponents(
|
|
25
|
+
value: string,
|
|
26
|
+
length: number,
|
|
27
|
+
): { value: string; truncated: boolean } {
|
|
28
|
+
if (length <= 0) return { value, truncated: false };
|
|
29
|
+
const normalized = toSlashPath(value);
|
|
30
|
+
const components = normalized.split("/").filter((component) => component.length > 0);
|
|
31
|
+
if (components.length <= length) return { value: normalized, truncated: false };
|
|
32
|
+
return { value: components.slice(-length).join("/"), truncated: true };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useNativePathSeparator(value: string): string {
|
|
36
|
+
return sep === "/" ? value : value.replaceAll("/", sep);
|
|
37
|
+
}
|
package/src/modules/types.ts
CHANGED
|
@@ -63,6 +63,7 @@ export interface GithubPrSnapshot {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export interface GitSnapshot {
|
|
66
|
+
root?: string;
|
|
66
67
|
branch?: GitBranchSnapshot;
|
|
67
68
|
commit?: GitCommitSnapshot;
|
|
68
69
|
state?: GitStateSnapshot;
|
|
@@ -78,6 +79,8 @@ export interface WorkspaceSnapshot {
|
|
|
78
79
|
|
|
79
80
|
export interface StarshipRuntimeSnapshot {
|
|
80
81
|
cwd: string;
|
|
82
|
+
homeDir?: string;
|
|
83
|
+
gitRoot?: string;
|
|
81
84
|
model?: { provider: string; id: string };
|
|
82
85
|
thinkingLevel: string;
|
|
83
86
|
turnCount: number;
|
package/src/pi-starship.ts
CHANGED
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
type ReadonlyFooterDataProvider,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
9
|
-
import {
|
|
9
|
+
import { completeStarshipArguments } from "./command-contract.js";
|
|
10
|
+
import type { StarshipCommandOptions } from "./commands.js";
|
|
10
11
|
import {
|
|
11
12
|
type LoadedStarshipConfig,
|
|
12
13
|
loadStarshipConfig,
|
|
@@ -40,6 +41,19 @@ const GITHUB_PR_REFRESH_INTERVAL_MS = 60_000;
|
|
|
40
41
|
const EVENT_DEBOUNCE_MS = 250;
|
|
41
42
|
const MAX_TIMER_DELAY_MS = 2_147_483_647;
|
|
42
43
|
|
|
44
|
+
type StarshipCommands = typeof import("./commands.js");
|
|
45
|
+
let starshipCommandsPromise: Promise<StarshipCommands> | undefined;
|
|
46
|
+
|
|
47
|
+
function loadStarshipCommands(): Promise<StarshipCommands> {
|
|
48
|
+
if (!starshipCommandsPromise) {
|
|
49
|
+
starshipCommandsPromise = import("./commands.js").catch((error) => {
|
|
50
|
+
starshipCommandsPromise = undefined;
|
|
51
|
+
throw error;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return starshipCommandsPromise;
|
|
55
|
+
}
|
|
56
|
+
|
|
43
57
|
interface RuntimeState {
|
|
44
58
|
activeTools: Map<string, number>;
|
|
45
59
|
isStreaming: boolean;
|
|
@@ -90,7 +104,10 @@ export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions
|
|
|
90
104
|
const gitController = new AsyncRefreshController<GitRefreshInput, GitSnapshot | undefined>({
|
|
91
105
|
async read(input, signal) {
|
|
92
106
|
const requirements = reachableModuleRequirements(input.config);
|
|
93
|
-
const gitReachable =
|
|
107
|
+
const gitReachable =
|
|
108
|
+
[...requirements.keys()].some((name) => name.startsWith("git_")) ||
|
|
109
|
+
(requirements.has("directory") &&
|
|
110
|
+
input.config.modules.directory.options.truncate_to_repo === true);
|
|
94
111
|
if (!gitReachable) return undefined;
|
|
95
112
|
try {
|
|
96
113
|
return await readGitSnapshot(pi, input.cwd, {
|
|
@@ -325,7 +342,7 @@ export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions
|
|
|
325
342
|
};
|
|
326
343
|
|
|
327
344
|
const configPath = settingsFilePath(getAgentDir());
|
|
328
|
-
|
|
345
|
+
const commandOptions: StarshipCommandOptions = {
|
|
329
346
|
settingsPath: configPath,
|
|
330
347
|
getLoaded: () => loaded ?? loadStarshipConfig(configPath),
|
|
331
348
|
getInspection: () => {
|
|
@@ -360,6 +377,15 @@ export default function piStarship(pi: ExtensionAPI, options: PiStarshipOptions
|
|
|
360
377
|
]
|
|
361
378
|
);
|
|
362
379
|
},
|
|
380
|
+
};
|
|
381
|
+
pi.registerCommand("starship", {
|
|
382
|
+
description: "Customize or inspect the native Starship-style footer",
|
|
383
|
+
getArgumentCompletions: completeStarshipArguments,
|
|
384
|
+
handler: async (args, ctx) => {
|
|
385
|
+
const commands = await loadStarshipCommands();
|
|
386
|
+
if (sessionOwner !== ctx.sessionManager) return;
|
|
387
|
+
await commands.handleStarshipCommand(args, ctx, commandOptions);
|
|
388
|
+
},
|
|
363
389
|
});
|
|
364
390
|
|
|
365
391
|
pi.on("session_start", (_event, ctx) => {
|
|
@@ -521,6 +547,8 @@ function runtimeSnapshot(
|
|
|
521
547
|
): StarshipRuntimeSnapshot {
|
|
522
548
|
return {
|
|
523
549
|
cwd: ctx.cwd,
|
|
550
|
+
homeDir: homedir(),
|
|
551
|
+
gitRoot: runtime.git?.root,
|
|
524
552
|
model: ctx.model ? { provider: ctx.model.provider, id: ctx.model.id } : undefined,
|
|
525
553
|
thinkingLevel: runtime.thinkingLevel,
|
|
526
554
|
turnCount: userTurnCount(ctx),
|
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
optionBoolean,
|
|
9
9
|
optionString,
|
|
10
10
|
optionStrings,
|
|
11
|
-
pathName,
|
|
12
11
|
runBounded,
|
|
13
12
|
safeMetadata,
|
|
14
13
|
setString,
|
|
@@ -51,7 +50,7 @@ export async function collectDevelopment(
|
|
|
51
50
|
conda &&
|
|
52
51
|
!(optionBoolean(context, "conda", "ignore_base") && conda === "base")
|
|
53
52
|
) {
|
|
54
|
-
result.conda = { environment:
|
|
53
|
+
result.conda = { environment: conda };
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
if (context.needs("pixi") && detected(context, entries, "pixi", ["pixi.toml", "pixi.lock"])) {
|
package/src/runtime/execution.ts
CHANGED
|
@@ -98,12 +98,10 @@ function hostnameValues(context: CollectorContext): Record<string, string> | und
|
|
|
98
98
|
const raw = safeMetadata(context.input.hostname);
|
|
99
99
|
if (!raw) return undefined;
|
|
100
100
|
const aliases = optionMap(context, "hostname", "aliases");
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (index > 0) hostname = hostname.slice(0, index);
|
|
106
|
-
}
|
|
101
|
+
const trimAt = optionString(context, "hostname", "trim_at");
|
|
102
|
+
const index = trimAt ? raw.indexOf(trimAt) : -1;
|
|
103
|
+
const trimmed = index >= 0 ? raw.slice(0, index) : raw;
|
|
104
|
+
const hostname = exactAlias(trimmed, aliases) ?? trimmed;
|
|
107
105
|
return { hostname, ssh_symbol: ssh ? "🌐 " : "" };
|
|
108
106
|
}
|
|
109
107
|
|