@narumitw/pi-starship 0.44.0 → 0.46.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 +246 -71
- package/package.json +2 -2
- package/src/command-inspector.ts +198 -0
- package/src/command-preview.ts +206 -0
- package/src/commands.ts +190 -154
- package/src/config.ts +177 -68
- package/src/format/style.ts +64 -45
- package/src/modules/activity.ts +1 -1
- package/src/modules/brand.ts +1 -1
- package/src/modules/cache.ts +1 -1
- package/src/modules/catalog.ts +62 -3
- package/src/modules/context.ts +13 -1
- package/src/modules/cost.ts +12 -1
- package/src/modules/development.ts +24 -7
- package/src/modules/directory.ts +141 -7
- package/src/modules/display.ts +14 -0
- package/src/modules/execution.ts +13 -2
- package/src/modules/extension-status.ts +1 -1
- package/src/modules/fill.ts +1 -1
- package/src/modules/git/branch.ts +14 -5
- package/src/modules/git/commit.ts +10 -3
- package/src/modules/git/metrics.ts +8 -2
- package/src/modules/git/runtime.ts +3 -0
- package/src/modules/git/state.ts +1 -1
- package/src/modules/git/status.ts +1 -1
- package/src/modules/git/worktree.ts +1 -1
- package/src/modules/github-pr.ts +1 -1
- package/src/modules/index.ts +13 -1
- package/src/modules/inspection.ts +138 -0
- package/src/modules/model.ts +15 -65
- package/src/modules/provider.ts +1 -1
- package/src/modules/render.ts +22 -3
- package/src/modules/terminal.ts +60 -0
- package/src/modules/thinking.ts +1 -1
- package/src/modules/time.ts +1 -1
- package/src/modules/tokens.ts +1 -1
- package/src/modules/truncation.ts +37 -0
- package/src/modules/turn.ts +1 -1
- package/src/modules/types.ts +22 -0
- package/src/pi-starship.ts +27 -1
- package/src/runtime/development.ts +1 -2
- package/src/runtime/execution.ts +19 -9
- package/src/runtime/types.ts +2 -0
- package/src/runtime/workspace.ts +21 -7
package/src/modules/cost.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolveDisplayStyle } from "./display.js";
|
|
1
2
|
import { defineModule } from "./types.js";
|
|
2
3
|
|
|
3
4
|
export const costModule = defineModule({
|
|
@@ -6,9 +7,19 @@ export const costModule = defineModule({
|
|
|
6
7
|
defaults: {
|
|
7
8
|
format: "[ $symbol \\$$cost( $subscription) ]($style)",
|
|
8
9
|
symbol: "💸",
|
|
9
|
-
style: "
|
|
10
|
+
style: "none",
|
|
10
11
|
disabled: false,
|
|
11
12
|
},
|
|
13
|
+
displayDefaults: [
|
|
14
|
+
{ threshold: 0, style: "bold green", hidden: true },
|
|
15
|
+
{ threshold: 1, style: "bold yellow", hidden: false },
|
|
16
|
+
{ threshold: 5, style: "bold red", hidden: false },
|
|
17
|
+
],
|
|
18
|
+
styleVariables: ["style"],
|
|
19
|
+
resolveStyleVariables: ({ runtime, display }) => {
|
|
20
|
+
const style = resolveDisplayStyle(display, runtime.tokenTotals.cost);
|
|
21
|
+
return style === undefined ? undefined : { style };
|
|
22
|
+
},
|
|
12
23
|
values: ({ runtime }) => ({
|
|
13
24
|
cost: formatCost(runtime.tokenTotals.cost),
|
|
14
25
|
subscription: runtime.usingSubscription ? "(sub)" : "",
|
|
@@ -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
|
|
|
@@ -43,17 +44,33 @@ export const direnvModule = developmentModule({
|
|
|
43
44
|
variables: ["rc_path", "allowed", "loaded"],
|
|
44
45
|
format: "[$symbol$loaded]($style) ",
|
|
45
46
|
symbol: "direnv ",
|
|
46
|
-
style: "bold
|
|
47
|
+
style: "bold bright-yellow",
|
|
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",
|
|
@@ -7,11 +14,138 @@ export const directoryModule = defineModule({
|
|
|
7
14
|
defaults: {
|
|
8
15
|
format: "[ $symbol $path ]($style)",
|
|
9
16
|
symbol: "📁",
|
|
10
|
-
style: "
|
|
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
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ModuleDisplayConfig } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export function resolveDisplayStyle(
|
|
4
|
+
display: readonly ModuleDisplayConfig[],
|
|
5
|
+
value: number | null | undefined,
|
|
6
|
+
): string | undefined {
|
|
7
|
+
if (value === null || value === undefined || !Number.isFinite(value)) return undefined;
|
|
8
|
+
let selected: ModuleDisplayConfig | undefined;
|
|
9
|
+
for (const entry of display) {
|
|
10
|
+
if (entry.threshold > value) continue;
|
|
11
|
+
if (!selected || entry.threshold >= selected.threshold) selected = entry;
|
|
12
|
+
}
|
|
13
|
+
return selected && !selected.hidden ? selected.style : undefined;
|
|
14
|
+
}
|
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),
|
|
@@ -54,9 +54,20 @@ export const usernameModule = defineModule({
|
|
|
54
54
|
defaults: {
|
|
55
55
|
format: "[$user]($style) in ",
|
|
56
56
|
symbol: "",
|
|
57
|
-
style: "
|
|
57
|
+
style: "none",
|
|
58
58
|
disabled: false,
|
|
59
59
|
},
|
|
60
|
+
styleDefaults: {
|
|
61
|
+
style_user: "yellow bold",
|
|
62
|
+
style_root: "red bold",
|
|
63
|
+
},
|
|
64
|
+
styleVariables: ["style"],
|
|
65
|
+
resolveStyleVariables: ({ runtime, styles }) => ({
|
|
66
|
+
style:
|
|
67
|
+
runtime.workspace?.styleSelectors?.username === "root"
|
|
68
|
+
? (styles.style_root ?? "")
|
|
69
|
+
: (styles.style_user ?? ""),
|
|
70
|
+
}),
|
|
60
71
|
options: {
|
|
61
72
|
show_always: { kind: "boolean", default: false },
|
|
62
73
|
aliases: { kind: "string-map", default: {} },
|
package/src/modules/fill.ts
CHANGED
|
@@ -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({
|
|
@@ -6,17 +7,25 @@ export const gitBranchModule = defineModule({
|
|
|
6
7
|
defaults: {
|
|
7
8
|
format: "[ $symbol $branch ]($style)",
|
|
8
9
|
symbol: "🌿",
|
|
9
|
-
style: "
|
|
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
|
});
|
|
@@ -8,14 +8,21 @@ export const gitCommitModule = defineModule({
|
|
|
8
8
|
defaults: {
|
|
9
9
|
format: "[ ($hash) ]($style)",
|
|
10
10
|
symbol: "",
|
|
11
|
-
style: "
|
|
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
|
},
|
|
@@ -4,11 +4,17 @@ export const gitMetricsModule = defineModule({
|
|
|
4
4
|
name: "git_metrics",
|
|
5
5
|
variables: ["symbol", "added", "deleted"],
|
|
6
6
|
defaults: {
|
|
7
|
-
format: "[
|
|
7
|
+
format: "([+$added]($added_style) )([-$deleted]($deleted_style) )",
|
|
8
8
|
symbol: "",
|
|
9
|
-
style: "
|
|
9
|
+
style: "none",
|
|
10
10
|
disabled: true,
|
|
11
11
|
},
|
|
12
|
+
styleDefaults: {
|
|
13
|
+
added_style: "bold green",
|
|
14
|
+
deleted_style: "bold red",
|
|
15
|
+
},
|
|
16
|
+
styleVariables: ["added_style", "deleted_style"],
|
|
17
|
+
resolveStyleVariables: ({ styles }) => styles,
|
|
12
18
|
values: ({ runtime }) => {
|
|
13
19
|
const metrics = runtime.gitMetrics;
|
|
14
20
|
if (!metrics || (metrics.added === 0 && metrics.deleted === 0)) return undefined;
|
|
@@ -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/git/state.ts
CHANGED
package/src/modules/github-pr.ts
CHANGED
package/src/modules/index.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
type CatalogModuleDefinition,
|
|
3
|
+
MODULE_DEFINITIONS,
|
|
4
|
+
MODULE_NAMES,
|
|
5
|
+
type ModuleName,
|
|
6
|
+
} from "./catalog.js";
|
|
2
7
|
export { formatExtensionStatus } from "./extension-status.js";
|
|
3
8
|
export { formatCount } from "./helpers.js";
|
|
9
|
+
export {
|
|
10
|
+
inspectStatuslineModules,
|
|
11
|
+
inspectUnavailableModules,
|
|
12
|
+
type ModuleInspection,
|
|
13
|
+
type ModuleInspectionState,
|
|
14
|
+
type StatuslineInspection,
|
|
15
|
+
} from "./inspection.js";
|
|
4
16
|
export { shortenModel } from "./model.js";
|
|
5
17
|
export { reachableModuleRequirements, renderStatusline } from "./render.js";
|
|
6
18
|
export type {
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from "node:util";
|
|
2
|
+
import type { StarshipConfig } from "../config.js";
|
|
3
|
+
import { formatVariables } from "../format/formatter.js";
|
|
4
|
+
import { MODULE_DEFINITIONS, type ModuleName } from "./catalog.js";
|
|
5
|
+
import { reachableModuleRequirements, renderStatusline } from "./render.js";
|
|
6
|
+
import type { StarshipRuntimeSnapshot } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export type ModuleInspectionState =
|
|
9
|
+
| "Showing"
|
|
10
|
+
| "Empty"
|
|
11
|
+
| "Disabled"
|
|
12
|
+
| "Not in format"
|
|
13
|
+
| "Unavailable";
|
|
14
|
+
|
|
15
|
+
export interface ModuleInspection {
|
|
16
|
+
name: ModuleName;
|
|
17
|
+
description: string;
|
|
18
|
+
state: ModuleInspectionState;
|
|
19
|
+
preview: string;
|
|
20
|
+
variables: readonly string[];
|
|
21
|
+
styleFields: readonly string[];
|
|
22
|
+
displayRules: readonly string[];
|
|
23
|
+
rootReferenced: boolean;
|
|
24
|
+
reachable: boolean;
|
|
25
|
+
reason: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface StatuslineInspection {
|
|
29
|
+
modules: readonly ModuleInspection[];
|
|
30
|
+
showing: readonly ModuleInspection[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function inspectStatuslineModules(
|
|
34
|
+
config: StarshipConfig,
|
|
35
|
+
runtime: StarshipRuntimeSnapshot,
|
|
36
|
+
width = 80,
|
|
37
|
+
): StatuslineInspection {
|
|
38
|
+
const rendered = renderStatusline(config, runtime, width);
|
|
39
|
+
const requirements = reachableModuleRequirements(config);
|
|
40
|
+
const rootVariables = formatVariables(config.formatAst);
|
|
41
|
+
const includeAll = rootVariables.has("all");
|
|
42
|
+
const modules = MODULE_DEFINITIONS.map((definition): ModuleInspection => {
|
|
43
|
+
const module = config.modules[definition.name];
|
|
44
|
+
const reachable = requirements.has(definition.name);
|
|
45
|
+
const preview = plainPreview(
|
|
46
|
+
rendered.modules[definition.name].map((chunk) => chunk.text).join(""),
|
|
47
|
+
);
|
|
48
|
+
const state = module.disabled
|
|
49
|
+
? "Disabled"
|
|
50
|
+
: !reachable
|
|
51
|
+
? "Not in format"
|
|
52
|
+
: preview.length > 0
|
|
53
|
+
? "Showing"
|
|
54
|
+
: "Empty";
|
|
55
|
+
return moduleInspection(
|
|
56
|
+
config,
|
|
57
|
+
definition,
|
|
58
|
+
state,
|
|
59
|
+
preview,
|
|
60
|
+
includeAll || rootVariables.has(definition.name),
|
|
61
|
+
reachable,
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
modules,
|
|
66
|
+
showing: modules.filter((module) => module.state === "Showing"),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function inspectUnavailableModules(config: StarshipConfig): StatuslineInspection {
|
|
71
|
+
const requirements = reachableModuleRequirements(config);
|
|
72
|
+
const rootVariables = formatVariables(config.formatAst);
|
|
73
|
+
const includeAll = rootVariables.has("all");
|
|
74
|
+
return {
|
|
75
|
+
modules: MODULE_DEFINITIONS.map((definition) => {
|
|
76
|
+
const module = config.modules[definition.name];
|
|
77
|
+
const reachable = requirements.has(definition.name);
|
|
78
|
+
const state = module.disabled ? "Disabled" : reachable ? "Unavailable" : "Not in format";
|
|
79
|
+
return moduleInspection(
|
|
80
|
+
config,
|
|
81
|
+
definition,
|
|
82
|
+
state,
|
|
83
|
+
"",
|
|
84
|
+
includeAll || rootVariables.has(definition.name),
|
|
85
|
+
reachable,
|
|
86
|
+
);
|
|
87
|
+
}),
|
|
88
|
+
showing: [],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function moduleInspection(
|
|
93
|
+
config: StarshipConfig,
|
|
94
|
+
definition: (typeof MODULE_DEFINITIONS)[number],
|
|
95
|
+
state: ModuleInspectionState,
|
|
96
|
+
preview: string,
|
|
97
|
+
rootReferenced: boolean,
|
|
98
|
+
reachable: boolean,
|
|
99
|
+
): ModuleInspection {
|
|
100
|
+
const module = config.modules[definition.name];
|
|
101
|
+
return {
|
|
102
|
+
name: definition.name,
|
|
103
|
+
description: definition.description,
|
|
104
|
+
state,
|
|
105
|
+
preview,
|
|
106
|
+
variables: [...definition.variables],
|
|
107
|
+
styleFields: ["style", ...Object.keys(module.styles)],
|
|
108
|
+
displayRules: module.display.map(
|
|
109
|
+
(rule) => `${rule.threshold}: ${rule.hidden ? "hidden" : rule.style || "unstyled"}`,
|
|
110
|
+
),
|
|
111
|
+
rootReferenced,
|
|
112
|
+
reachable,
|
|
113
|
+
reason: inspectionReason(state),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function inspectionReason(state: ModuleInspectionState): string {
|
|
118
|
+
switch (state) {
|
|
119
|
+
case "Showing":
|
|
120
|
+
return "Rendered in the current footer.";
|
|
121
|
+
case "Empty":
|
|
122
|
+
return "Referenced by the root format, but the current snapshot produced no output.";
|
|
123
|
+
case "Disabled":
|
|
124
|
+
return "Disabled by this module's configuration.";
|
|
125
|
+
case "Not in format":
|
|
126
|
+
return "Not referenced by the root format or $all.";
|
|
127
|
+
case "Unavailable":
|
|
128
|
+
return "Current footer inspection is unavailable; no collector failure was inferred.";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function plainPreview(value: string): string {
|
|
133
|
+
return Array.from(stripVTControlCharacters(value), (character) => {
|
|
134
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
135
|
+
if (character === "\n") return character;
|
|
136
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
|
|
137
|
+
}).join("");
|
|
138
|
+
}
|