@lune_99/pi-shell-inspector 0.1.0 → 1.4.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.
@@ -1,24 +1,51 @@
1
1
  import type { ExtensionContext, ThemeColor } from "@earendil-works/pi-coding-agent";
2
- import { truncateToWidth, type Component } from "@earendil-works/pi-tui";
3
- import { createDockContribution, type LuneDockSnapshot } from "@lune_99/pi-lune-extensions-core/dock";
4
- import { shellDock } from "./shell-dock.ts";
2
+ import { visibleWidth, type Component } from "@earendil-works/pi-tui";
3
+ import {
4
+ createDockContribution,
5
+ type DockHandle,
6
+ type LuneDockSnapshot,
7
+ } from "@lune_99/pi-lune-extensions-core/dock";
8
+ import { DockAlerts } from "./dock-alert.ts";
9
+ import { fitRanked, type RankedPart } from "./dock-line.ts";
10
+ import { ShellDock } from "./shell-dock.ts";
5
11
  import { collapseShellCommand } from "./shell-command.ts";
6
12
  import { openShellInspector } from "./shell-inspector.ts";
7
- import { shellManager, type ShellManagerEvent } from "./shell-manager.ts";
13
+ import type { ShellManager, ShellManagerEvent } from "./shell-manager.ts";
8
14
 
9
15
  export const SHELL_CONTRIBUTION_ID = "shell-inspector";
10
16
 
17
+ /**
18
+ * Ranks of the line's parts: a narrow cell drops the lowest first, so failures stay longest and
19
+ * the runtime goes before the command it belongs to.
20
+ */
21
+ const FAILED_RANK = 5;
22
+ const RUNNING_RANK = 4;
23
+ const COMPLETED_RANK = 3;
24
+ const LATEST_RANK = 2;
25
+ const ELAPSED_RANK = 1;
26
+
27
+ /**
28
+ * The Dock item of the background shells. Its lines count the jobs by status and never cut a
29
+ * part: a narrow cell leaves out whole parts, then the title. A failed or killed job turns the
30
+ * marker red for a short time, or until the user opens the inspector.
31
+ */
11
32
  export class ShellDockContribution {
12
33
  private generation = 0;
13
- private readonly acknowledged = new Set<string>();
34
+ private readonly alerts = new DockAlerts(() => this.render());
14
35
  private ctx: ExtensionContext | undefined;
15
36
  private timer: ReturnType<typeof setInterval> | undefined;
16
- private readonly dock = createDockContribution({
17
- id: SHELL_CONTRIBUTION_ID,
18
- getSnapshot: (ctx) => this.getSnapshot(ctx),
19
- activate: (ctx) => this.open(ctx),
20
- standalone: shellDock,
21
- });
37
+ private readonly manager: ShellManager;
38
+ private readonly dock: DockHandle;
39
+
40
+ constructor(manager: ShellManager) {
41
+ this.manager = manager;
42
+ this.dock = createDockContribution({
43
+ id: SHELL_CONTRIBUTION_ID,
44
+ getSnapshot: (ctx) => this.getSnapshot(ctx),
45
+ activate: (ctx) => this.open(ctx),
46
+ standalone: new ShellDock(manager),
47
+ });
48
+ }
22
49
 
23
50
  attach(ctx: ExtensionContext): void {
24
51
  this.detach({ retired: true });
@@ -33,12 +60,12 @@ export class ShellDockContribution {
33
60
  this.timer = undefined;
34
61
  this.ctx = undefined;
35
62
  this.dock.detach(options);
36
- this.acknowledged.clear();
63
+ this.alerts.clear();
37
64
  }
38
65
 
39
66
  onManagerEvent(event: ShellManagerEvent): void {
40
- if (event.type === "jobs-cleared") this.acknowledged.clear();
41
- if (event.type === "job-cleared") this.acknowledged.delete(event.id);
67
+ if (event.type === "jobs-cleared") this.alerts.clear();
68
+ if (event.type === "job-cleared") this.alerts.forget(event.id);
42
69
  // Output chunks affect the independent command/runtime row, not the shared summary.
43
70
  this.dock.refresh({ summaryChanged: event.type !== "output-updated" });
44
71
  this.syncTimer();
@@ -49,7 +76,7 @@ export class ShellDockContribution {
49
76
  }
50
77
 
51
78
  private syncTimer(): void {
52
- if (this.ctx?.mode === "tui" && shellManager.getRunningJobsList().length > 0) {
79
+ if (this.ctx?.mode === "tui" && this.manager.getRunningJobsList().length > 0) {
53
80
  this.timer ??= setInterval(() => this.render(), 1000);
54
81
  } else if (this.timer) {
55
82
  clearInterval(this.timer);
@@ -57,66 +84,73 @@ export class ShellDockContribution {
57
84
  }
58
85
  }
59
86
 
87
+ /** Base names the item; detail adds the counts; full adds the latest job and its runtime. */
60
88
  getSnapshot(ctx = this.ctx!): LuneDockSnapshot {
61
- const jobs = shellManager.getAllJobsList();
89
+ const theme = ctx.ui.theme;
90
+ const jobs = this.manager.getAllJobsList();
62
91
  const failed = jobs.filter((job) => job.status === "failed" || job.status === "killed");
63
92
  const running = jobs.filter((job) => job.status === "running");
93
+ const completed = jobs.filter((job) => job.status === "completed");
64
94
  let color: ThemeColor;
65
95
  let marker: string;
66
- if (failed.some((job) => !this.acknowledged.has(job.id))) {
96
+ if (failed.some((job) => this.alerts.shows(job.id, job.finishedAt))) {
67
97
  color = "error";
68
98
  marker = "✕";
69
99
  } else if (running.length > 0) {
70
100
  color = "warning";
71
101
  marker = "◐";
72
- } else if (jobs.some((job) => job.status === "completed")) {
102
+ } else if (failed.length > 0) {
103
+ // Settled with failures: green would claim success, and red stays for new failures.
104
+ color = "muted";
105
+ marker = "●";
106
+ } else if (completed.length > 0) {
73
107
  color = "success";
74
108
  marker = "●";
75
- } else if (jobs.length > 0) {
76
- color = "error";
77
- marker = "✕";
78
109
  } else {
79
110
  color = "dim";
80
111
  marker = "○";
81
112
  }
82
- const summary =
83
- jobs.length === 0
84
- ? "idle"
85
- : [
86
- running.length > 0 ? `${running.length} running` : undefined,
87
- jobs.filter((job) => job.status === "completed").length > 0
88
- ? `${jobs.filter((job) => job.status === "completed").length} completed`
89
- : undefined,
90
- failed.length > 0 ? `${failed.length} failed/killed` : undefined,
91
- ]
92
- .filter(Boolean)
93
- .join(" · ");
113
+ const muted = (text: string): string => theme.fg("muted", text);
114
+ const summary: RankedPart[] = [
115
+ running.length > 0
116
+ ? { text: muted(`${running.length} running`), rank: RUNNING_RANK }
117
+ : undefined,
118
+ completed.length > 0
119
+ ? { text: muted(`${completed.length} completed`), rank: COMPLETED_RANK }
120
+ : undefined,
121
+ failed.length > 0
122
+ ? { text: muted(`${failed.length} failed/killed`), rank: FAILED_RANK }
123
+ : undefined,
124
+ ].filter((part) => part !== undefined);
94
125
  const latest = jobs[jobs.length - 1];
95
- const command = latest ? collapseShellCommand(latest.command) : undefined;
96
- const elapsed = latest
97
- ? Math.floor(((latest.finishedAt ?? Date.now()) - latest.startedAt) / 1000)
98
- : undefined;
99
- const component = (suffix: string): Component => ({
126
+ const details: RankedPart[] = latest
127
+ ? [
128
+ {
129
+ text: muted(`latest: ${collapseShellCommand(latest.command)}`),
130
+ rank: LATEST_RANK,
131
+ },
132
+ {
133
+ text: muted(
134
+ `${Math.floor(((latest.finishedAt ?? Date.now()) - latest.startedAt) / 1000)}s`,
135
+ ),
136
+ rank: ELAPSED_RANK,
137
+ },
138
+ ]
139
+ : [];
140
+ const symbol = theme.fg(color, marker);
141
+ // `● Shell · 2 running · 1 completed` with every part that fits, down to the marker alone.
142
+ const component = (parts: readonly RankedPart[]): Component => ({
100
143
  render: (width) => [
101
- truncateToWidth(
102
- `${ctx.ui.theme.fg(color, marker)} Shell${suffix ? ctx.ui.theme.fg("muted", ` · ${suffix}`) : ""}`,
103
- width,
104
- ),
144
+ fitRanked(parts, width, (texts) =>
145
+ [`${symbol} Shell`, ...texts].join(muted(" · ")),
146
+ ) ?? (visibleWidth(symbol) <= width ? symbol : ""),
105
147
  ],
106
148
  invalidate() {},
107
149
  });
108
150
  return {
109
- base: component(""),
151
+ base: component([]),
110
152
  detail: component(summary),
111
- full: component(
112
- [
113
- summary,
114
- command ? `latest: ${command}` : undefined,
115
- elapsed !== undefined ? `${elapsed}s` : undefined,
116
- ]
117
- .filter(Boolean)
118
- .join(" · "),
119
- ),
153
+ full: component([...summary, ...details]),
120
154
  };
121
155
  }
122
156
 
@@ -124,13 +158,13 @@ export class ShellDockContribution {
124
158
  const generation = this.generation;
125
159
  // A failure arriving after the panel opened has not necessarily been seen. Only
126
160
  // the opening snapshot is acknowledged, and only after a successful interaction.
127
- const seen = shellManager
161
+ const seen = this.manager
128
162
  .getAllJobsList()
129
163
  .filter((job) => job.status === "failed" || job.status === "killed")
130
164
  .map((job) => job.id);
131
- await openShellInspector(ctx);
165
+ await openShellInspector(ctx, this.manager);
132
166
  if (generation !== this.generation) return;
133
- for (const id of seen) this.acknowledged.add(id);
167
+ for (const id of seen) this.alerts.acknowledge(id);
134
168
  this.render();
135
169
  }
136
170
  }
@@ -1,20 +1,34 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import { shellManager } from "./shell-manager.ts";
3
- import { formatShellCommand } from "./shell-command.ts";
4
-
2
+ import { fitRanked, type RankedPart } from "./dock-line.ts";
3
+ import type { ShellManager } from "./shell-manager.ts";
4
+ import { collapseShellCommand } from "./shell-command.ts";
5
5
 
6
6
  const WIDGET_ID = "pi-shell-inspector";
7
- const PADDING = "";
7
+
8
+ /**
9
+ * Ranks of the parts after the row's lead: a narrow row drops the lowest first, so failures stay
10
+ * longest and the hint goes first. The lead itself is never left out.
11
+ */
12
+ const FAILED_RANK = 6;
13
+ const KILLED_RANK = 5;
14
+ const RUNNING_RANK = 4;
15
+ const COMPLETED_RANK = 3;
16
+ const COMMAND_RANK = 2;
17
+ const ELAPSED_RANK = 1;
18
+ const HINT_RANK = 0;
8
19
 
9
20
  export class ShellDock {
10
21
  private refreshTimer: ReturnType<typeof setInterval> | undefined;
11
22
  private selected = false;
12
23
  private ctx: ExtensionContext | undefined;
24
+ private readonly manager: ShellManager;
13
25
 
14
- constructor() { };
26
+ constructor(manager: ShellManager) {
27
+ this.manager = manager;
28
+ }
15
29
 
16
30
  private syncRefreshTimer(): void {
17
- const hasRunningJobs = shellManager.jobsStatusStat.runningCount > 0;
31
+ const hasRunningJobs = this.manager.jobsStatusStat.runningCount > 0;
18
32
 
19
33
  if (hasRunningJobs && !this.refreshTimer) {
20
34
  this.refreshTimer = setInterval(() => {
@@ -46,11 +60,13 @@ export class ShellDock {
46
60
  }
47
61
 
48
62
  render(): void {
49
- if (!this.ctx || !this.ctx.hasUI) { return; }
63
+ if (!this.ctx || !this.ctx.hasUI) {
64
+ return;
65
+ }
50
66
 
51
67
  this.syncRefreshTimer();
52
68
 
53
- const jobs = shellManager.getAllJobsList();
69
+ const jobs = this.manager.getAllJobsList();
54
70
 
55
71
  if (jobs.length === 0) {
56
72
  this.selected = false;
@@ -58,13 +74,22 @@ export class ShellDock {
58
74
  return;
59
75
  }
60
76
 
61
- const summary = shellDockSummary(this.ctx);
77
+ const { lead, parts } = shellDockSummary(this.ctx, this.manager);
78
+ const separator = this.ctx.ui.theme.fg("dim", " · ");
62
79
 
80
+ // Pi pads string rows by one column on each side; this row keeps the same margins.
63
81
  this.ctx.ui.setWidget(
64
82
  WIDGET_ID,
65
- [
66
- `${PADDING}${summary}${this.ctx.ui.theme.fg("dim", " · /shell to open")}`,
67
- ],
83
+ () => ({
84
+ render: (width) => [
85
+ ` ${
86
+ fitRanked(parts, Math.max(0, width - 2), (texts) =>
87
+ [lead, ...texts].join(separator),
88
+ ) ?? ""
89
+ }`,
90
+ ],
91
+ invalidate() {},
92
+ }),
68
93
  {
69
94
  placement: "belowEditor",
70
95
  },
@@ -79,15 +104,22 @@ export class ShellDock {
79
104
  this.refreshTimer = undefined;
80
105
  }
81
106
 
82
- if (!this.ctx || !this.ctx.hasUI) { return; }
107
+ if (!this.ctx || !this.ctx.hasUI) {
108
+ return;
109
+ }
83
110
 
84
111
  this.selected = false;
85
112
  this.ctx.ui.setWidget(WIDGET_ID, undefined);
86
113
  }
87
114
  }
88
115
 
89
- function shellDockSummary(ctx: ExtensionContext): string {
90
- const status = shellManager.getAllJobsStatusStat();
116
+ /** The lead names the jobs; the parts after it are left out whole while the row is too wide. */
117
+ function shellDockSummary(
118
+ ctx: ExtensionContext,
119
+ manager: ShellManager,
120
+ ): { lead: string; parts: RankedPart[] } {
121
+ const status = manager.getAllJobsStatusStat();
122
+ const hint = { text: ctx.ui.theme.fg("dim", "/shell to open"), rank: HINT_RANK };
91
123
 
92
124
  // only one running shell case
93
125
  if (
@@ -96,39 +128,64 @@ function shellDockSummary(ctx: ExtensionContext): string {
96
128
  status.failedCount === 0 &&
97
129
  status.killedCount === 0
98
130
  ) {
99
- const runningJob = shellManager.getRunningJobsList()[0];
131
+ const runningJob = manager.getRunningJobsList()[0];
100
132
  const elapsedTime = Math.floor((Date.now() - runningJob!.startedAt) / 1000);
101
- return [
102
- ctx.ui.theme.fg("accent", `${status.runningCount} running shell`),
103
- formatShellCommand(runningJob!.command, 20),
104
- `${elapsedTime}s`,
105
- ].join(ctx.ui.theme.fg("dim", " · "));
133
+ return {
134
+ lead: ctx.ui.theme.fg("accent", `${status.runningCount} running shell`),
135
+ parts: [
136
+ { text: collapseShellCommand(runningJob!.command), rank: COMMAND_RANK },
137
+ { text: `${elapsedTime}s`, rank: ELAPSED_RANK },
138
+ hint,
139
+ ],
140
+ };
106
141
  }
107
142
  // only one completed shell case
108
- if (status.completedCount === 1 && status.runningCount === 0 && status.failedCount === 0 && status.killedCount === 0) {
109
- const completedJob = shellManager.getCompletedJobsList()[0];
110
- const elapsedTime = Math.floor((completedJob!.finishedAt! - completedJob!.startedAt) / 1000);
111
- return (ctx.ui.theme.fg("success", `${status.completedCount} shell completed`) + ` in ${elapsedTime}s`);
143
+ if (
144
+ status.completedCount === 1 &&
145
+ status.runningCount === 0 &&
146
+ status.failedCount === 0 &&
147
+ status.killedCount === 0
148
+ ) {
149
+ const completedJob = manager.getCompletedJobsList()[0];
150
+ const elapsedTime = Math.floor(
151
+ (completedJob!.finishedAt! - completedJob!.startedAt) / 1000,
152
+ );
153
+ return {
154
+ lead:
155
+ ctx.ui.theme.fg("success", `${status.completedCount} shell completed`) +
156
+ ` in ${elapsedTime}s`,
157
+ parts: [hint],
158
+ };
112
159
  }
113
160
 
114
- const parts: string[] = [];
115
- const jobCount = shellManager.getAllJobsList().length;
116
- parts.push(`${jobCount} ${jobCount === 1 ? "shell" : "shells"}`);
161
+ const parts: RankedPart[] = [];
162
+ const jobCount = manager.getAllJobsList().length;
117
163
 
118
164
  if (status.runningCount > 0) {
119
- parts.push(ctx.ui.theme.fg("accent", `${status.runningCount} running`));
165
+ parts.push({
166
+ text: ctx.ui.theme.fg("accent", `${status.runningCount} running`),
167
+ rank: RUNNING_RANK,
168
+ });
120
169
  }
121
170
  if (status.completedCount > 0) {
122
- parts.push(ctx.ui.theme.fg("success", `${status.completedCount} completed`));
171
+ parts.push({
172
+ text: ctx.ui.theme.fg("success", `${status.completedCount} completed`),
173
+ rank: COMPLETED_RANK,
174
+ });
123
175
  }
124
176
  if (status.failedCount > 0) {
125
- parts.push(ctx.ui.theme.fg("error", `${status.failedCount} failed`));
177
+ parts.push({
178
+ text: ctx.ui.theme.fg("error", `${status.failedCount} failed`),
179
+ rank: FAILED_RANK,
180
+ });
126
181
  }
127
182
  if (status.killedCount > 0) {
128
- parts.push(ctx.ui.theme.fg("error", `${status.killedCount} killed`));
183
+ parts.push({
184
+ text: ctx.ui.theme.fg("error", `${status.killedCount} killed`),
185
+ rank: KILLED_RANK,
186
+ });
129
187
  }
188
+ parts.push(hint);
130
189
 
131
- return `${parts.join(ctx.ui.theme.fg("dim", " · "))}`;
190
+ return { lead: `${jobCount} ${jobCount === 1 ? "shell" : "shells"}`, parts };
132
191
  }
133
-
134
- export const shellDock = new ShellDock();