@aliou/pi-processes 0.4.5 → 0.4.6
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/package.json +1 -1
- package/src/commands/index.ts +334 -0
- package/src/commands/settings-command.ts +157 -0
- package/src/components/log-stream-component.ts +149 -0
- package/src/components/process-picker-component.ts +155 -0
- package/src/components/processes-component.ts +450 -0
- package/src/components/status-format.ts +38 -0
- package/src/config.ts +70 -0
- package/src/constants/index.ts +11 -0
- package/src/constants/types.ts +65 -0
- package/src/hooks/cleanup.ts +10 -0
- package/src/hooks/index.ts +18 -0
- package/src/hooks/message-renderer.ts +83 -0
- package/src/hooks/process-end.ts +92 -0
- package/src/hooks/widget.ts +146 -0
- package/src/index.ts +22 -20
- package/src/manager.ts +522 -0
- package/src/test/test-exit-crash.sh +19 -0
- package/src/test/test-exit-failure.sh +17 -0
- package/src/test/test-exit-success.sh +16 -0
- package/src/test/test-output.sh +28 -0
- package/src/tools/actions/clear.ts +20 -0
- package/src/tools/actions/index.ts +49 -0
- package/src/tools/actions/kill.ts +76 -0
- package/src/tools/actions/list.ts +37 -0
- package/src/tools/actions/logs.ts +59 -0
- package/src/tools/actions/output.ts +144 -0
- package/src/tools/actions/start.ts +55 -0
- package/src/tools/index.ts +280 -0
- package/src/utils/ansi.ts +36 -0
- package/src/utils/command-executor.test.ts +47 -0
- package/src/utils/command-executor.ts +56 -0
- package/src/utils/format.ts +42 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/process-group.ts +22 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPanelPadder,
|
|
3
|
+
renderPanelRule,
|
|
4
|
+
renderPanelTitleLine,
|
|
5
|
+
} from "@aliou/pi-utils-ui";
|
|
6
|
+
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
7
|
+
import { type Component, matchesKey } from "@mariozechner/pi-tui";
|
|
8
|
+
import type { ProcessInfo } from "../constants";
|
|
9
|
+
import type { ProcessManager } from "../manager";
|
|
10
|
+
import { statusIcon, statusLabel } from "./status-format";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A simple process picker component. Shows a list of processes and lets the
|
|
14
|
+
* user select one with up/down + Enter, or dismiss with Escape/q.
|
|
15
|
+
*/
|
|
16
|
+
export class ProcessPickerComponent implements Component {
|
|
17
|
+
private tui: { requestRender: () => void };
|
|
18
|
+
private theme: Theme;
|
|
19
|
+
private onClose: (processId?: string) => void;
|
|
20
|
+
private manager: ProcessManager;
|
|
21
|
+
private title: string;
|
|
22
|
+
private filter: (proc: ProcessInfo) => boolean;
|
|
23
|
+
|
|
24
|
+
private selectedIndex = 0;
|
|
25
|
+
private cachedLines: string[] = [];
|
|
26
|
+
private cachedWidth = 0;
|
|
27
|
+
private unsubscribe: (() => void) | null = null;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
tui: { requestRender: () => void },
|
|
31
|
+
theme: Theme,
|
|
32
|
+
onClose: (processId?: string) => void,
|
|
33
|
+
manager: ProcessManager,
|
|
34
|
+
title: string,
|
|
35
|
+
filter?: (proc: ProcessInfo) => boolean,
|
|
36
|
+
) {
|
|
37
|
+
this.tui = tui;
|
|
38
|
+
this.theme = theme;
|
|
39
|
+
this.onClose = onClose;
|
|
40
|
+
this.manager = manager;
|
|
41
|
+
this.title = title;
|
|
42
|
+
this.filter = filter ?? (() => true);
|
|
43
|
+
|
|
44
|
+
this.unsubscribe = this.manager.onEvent(() => {
|
|
45
|
+
this.invalidate();
|
|
46
|
+
this.tui.requestRender();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private getProcesses(): ProcessInfo[] {
|
|
51
|
+
return this.manager.list().filter(this.filter);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
handleInput(data: string): boolean {
|
|
55
|
+
const processes = this.getProcesses();
|
|
56
|
+
|
|
57
|
+
if (matchesKey(data, "down") || data === "j") {
|
|
58
|
+
if (processes.length > 0) {
|
|
59
|
+
this.selectedIndex = Math.min(
|
|
60
|
+
this.selectedIndex + 1,
|
|
61
|
+
processes.length - 1,
|
|
62
|
+
);
|
|
63
|
+
this.invalidate();
|
|
64
|
+
this.tui.requestRender();
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (matchesKey(data, "up") || data === "k") {
|
|
70
|
+
if (processes.length > 0) {
|
|
71
|
+
this.selectedIndex = Math.max(this.selectedIndex - 1, 0);
|
|
72
|
+
this.invalidate();
|
|
73
|
+
this.tui.requestRender();
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (matchesKey(data, "return")) {
|
|
79
|
+
if (processes.length > 0 && this.selectedIndex < processes.length) {
|
|
80
|
+
const proc = processes[this.selectedIndex];
|
|
81
|
+
if (proc) {
|
|
82
|
+
this.unsubscribe?.();
|
|
83
|
+
this.unsubscribe = null;
|
|
84
|
+
this.onClose(proc.id);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (matchesKey(data, "escape") || data === "q" || data === "Q") {
|
|
91
|
+
this.unsubscribe?.();
|
|
92
|
+
this.unsubscribe = null;
|
|
93
|
+
this.onClose();
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
invalidate(): void {
|
|
101
|
+
this.cachedWidth = 0;
|
|
102
|
+
this.cachedLines = [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
render(width: number): string[] {
|
|
106
|
+
if (width === this.cachedWidth && this.cachedLines.length > 0) {
|
|
107
|
+
return this.cachedLines;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const theme = this.theme;
|
|
111
|
+
const dim = (s: string) => theme.fg("dim", s);
|
|
112
|
+
const accent = (s: string) => theme.fg("accent", s);
|
|
113
|
+
|
|
114
|
+
const padLine = createPanelPadder(width);
|
|
115
|
+
|
|
116
|
+
const lines: string[] = [];
|
|
117
|
+
const processes = this.getProcesses();
|
|
118
|
+
|
|
119
|
+
lines.push(renderPanelTitleLine(this.title, width, theme));
|
|
120
|
+
|
|
121
|
+
if (processes.length === 0) {
|
|
122
|
+
lines.push(padLine(""));
|
|
123
|
+
lines.push(padLine(dim("No processes available")));
|
|
124
|
+
lines.push(padLine(""));
|
|
125
|
+
} else {
|
|
126
|
+
lines.push(padLine(""));
|
|
127
|
+
for (let i = 0; i < processes.length; i++) {
|
|
128
|
+
const proc = processes[i];
|
|
129
|
+
if (!proc) continue;
|
|
130
|
+
const isSelected = i === this.selectedIndex;
|
|
131
|
+
const icon = statusIcon(proc.status, proc.success);
|
|
132
|
+
const label = statusLabel(proc);
|
|
133
|
+
const prefix = isSelected ? accent("> ") : " ";
|
|
134
|
+
const name = isSelected ? accent(proc.name) : proc.name;
|
|
135
|
+
const id = dim(`(${proc.id})`);
|
|
136
|
+
const status = dim(`${icon} ${label}`);
|
|
137
|
+
lines.push(padLine(`${prefix}${name} ${id} ${status}`));
|
|
138
|
+
}
|
|
139
|
+
lines.push(padLine(""));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Footer
|
|
143
|
+
lines.push(renderPanelRule(width, theme));
|
|
144
|
+
lines.push(
|
|
145
|
+
padLine(
|
|
146
|
+
`${dim("j/k")} select ${dim("enter")} confirm ${dim("q")} cancel`,
|
|
147
|
+
),
|
|
148
|
+
);
|
|
149
|
+
lines.push(renderPanelRule(width, theme));
|
|
150
|
+
|
|
151
|
+
this.cachedLines = lines;
|
|
152
|
+
this.cachedWidth = width;
|
|
153
|
+
return this.cachedLines;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPanelPadder,
|
|
3
|
+
renderPanelRule,
|
|
4
|
+
renderPanelTitleLine,
|
|
5
|
+
} from "@aliou/pi-utils-ui";
|
|
6
|
+
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
7
|
+
import { type Component, matchesKey, visibleWidth } from "@mariozechner/pi-tui";
|
|
8
|
+
import { configLoader } from "../config";
|
|
9
|
+
import type { ProcessInfo } from "../constants";
|
|
10
|
+
import type { ProcessManager } from "../manager";
|
|
11
|
+
import { stripAnsi } from "../utils";
|
|
12
|
+
import { statusIcon, statusLabel } from "./status-format";
|
|
13
|
+
|
|
14
|
+
function formatRuntime(startTime: number, endTime: number | null): string {
|
|
15
|
+
const end = endTime ?? Date.now();
|
|
16
|
+
const ms = end - startTime;
|
|
17
|
+
const seconds = Math.floor(ms / 1000);
|
|
18
|
+
const minutes = Math.floor(seconds / 60);
|
|
19
|
+
const hours = Math.floor(minutes / 60);
|
|
20
|
+
|
|
21
|
+
if (hours > 0) {
|
|
22
|
+
return `${hours}h ${minutes % 60}m`;
|
|
23
|
+
}
|
|
24
|
+
if (minutes > 0) {
|
|
25
|
+
return `${minutes}m ${seconds % 60}s`;
|
|
26
|
+
}
|
|
27
|
+
return `${seconds}s`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function formatBytes(bytes: number): string {
|
|
31
|
+
if (bytes >= 1024 * 1024) {
|
|
32
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
33
|
+
}
|
|
34
|
+
if (bytes >= 1024) {
|
|
35
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
36
|
+
}
|
|
37
|
+
return `${bytes}B`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function truncate(str: string, maxLen: number): string {
|
|
41
|
+
if (maxLen <= 3) return str.slice(0, maxLen);
|
|
42
|
+
if (str.length <= maxLen) return str;
|
|
43
|
+
return `${str.slice(0, maxLen - 3)}...`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class ProcessesComponent implements Component {
|
|
47
|
+
private tui: { requestRender: () => void };
|
|
48
|
+
private theme: Theme;
|
|
49
|
+
private onClose: (processId?: string) => void;
|
|
50
|
+
private manager: ProcessManager;
|
|
51
|
+
|
|
52
|
+
private selectedIndex = 0;
|
|
53
|
+
private processScrollOffset = 0;
|
|
54
|
+
private logScrollOffset = 0;
|
|
55
|
+
private scrollInfo = { above: 0, below: 0 };
|
|
56
|
+
private cachedLines: string[] = [];
|
|
57
|
+
private cachedWidth = 0;
|
|
58
|
+
private unsubscribe: (() => void) | null = null;
|
|
59
|
+
|
|
60
|
+
constructor(
|
|
61
|
+
tui: { requestRender: () => void },
|
|
62
|
+
theme: Theme,
|
|
63
|
+
onClose: (processId?: string) => void,
|
|
64
|
+
manager: ProcessManager,
|
|
65
|
+
) {
|
|
66
|
+
this.tui = tui;
|
|
67
|
+
this.theme = theme;
|
|
68
|
+
this.onClose = onClose;
|
|
69
|
+
this.manager = manager;
|
|
70
|
+
|
|
71
|
+
this.unsubscribe = this.manager.onEvent(() => {
|
|
72
|
+
this.invalidate();
|
|
73
|
+
this.tui.requestRender();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
handleInput(data: string): boolean {
|
|
78
|
+
const processes = this.manager.list();
|
|
79
|
+
|
|
80
|
+
// Navigation
|
|
81
|
+
if (matchesKey(data, "down") || data === "j") {
|
|
82
|
+
if (processes.length > 0) {
|
|
83
|
+
this.selectedIndex = Math.min(
|
|
84
|
+
this.selectedIndex + 1,
|
|
85
|
+
processes.length - 1,
|
|
86
|
+
);
|
|
87
|
+
this.logScrollOffset = 0;
|
|
88
|
+
this.ensureProcessVisible(processes.length);
|
|
89
|
+
this.invalidate();
|
|
90
|
+
this.tui.requestRender();
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (matchesKey(data, "up") || data === "k") {
|
|
96
|
+
if (processes.length > 0) {
|
|
97
|
+
this.selectedIndex = Math.max(this.selectedIndex - 1, 0);
|
|
98
|
+
this.logScrollOffset = 0;
|
|
99
|
+
this.ensureProcessVisible(processes.length);
|
|
100
|
+
this.invalidate();
|
|
101
|
+
this.tui.requestRender();
|
|
102
|
+
}
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Scroll logs
|
|
107
|
+
if (data === "J") {
|
|
108
|
+
this.logScrollOffset = Math.max(0, this.logScrollOffset - 5);
|
|
109
|
+
this.invalidate();
|
|
110
|
+
this.tui.requestRender();
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (data === "K") {
|
|
115
|
+
this.logScrollOffset += 5;
|
|
116
|
+
this.invalidate();
|
|
117
|
+
this.tui.requestRender();
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Stream logs for selected process
|
|
122
|
+
if (matchesKey(data, "return")) {
|
|
123
|
+
if (processes.length > 0 && this.selectedIndex < processes.length) {
|
|
124
|
+
const proc = processes[this.selectedIndex];
|
|
125
|
+
if (proc) {
|
|
126
|
+
this.unsubscribe?.();
|
|
127
|
+
this.unsubscribe = null;
|
|
128
|
+
this.onClose(proc.id);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Kill selected process
|
|
135
|
+
if (data === "x") {
|
|
136
|
+
if (processes.length > 0 && this.selectedIndex < processes.length) {
|
|
137
|
+
const proc = processes[this.selectedIndex];
|
|
138
|
+
if (proc?.status === "running") {
|
|
139
|
+
void this.manager.kill(proc.id, {
|
|
140
|
+
signal: "SIGTERM",
|
|
141
|
+
timeoutMs: 3000,
|
|
142
|
+
});
|
|
143
|
+
} else if (proc?.status === "terminate_timeout") {
|
|
144
|
+
void this.manager.kill(proc.id, {
|
|
145
|
+
signal: "SIGKILL",
|
|
146
|
+
timeoutMs: 200,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Clear finished processes
|
|
154
|
+
if (data === "c" || data === "C") {
|
|
155
|
+
const cleared = this.manager.clearFinished();
|
|
156
|
+
if (cleared > 0) {
|
|
157
|
+
const remaining = this.manager.list();
|
|
158
|
+
if (this.selectedIndex >= remaining.length) {
|
|
159
|
+
this.selectedIndex = Math.max(0, remaining.length - 1);
|
|
160
|
+
}
|
|
161
|
+
this.ensureProcessVisible(remaining.length);
|
|
162
|
+
this.invalidate();
|
|
163
|
+
this.tui.requestRender();
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Close
|
|
169
|
+
if (matchesKey(data, "escape") || data === "q" || data === "Q") {
|
|
170
|
+
this.unsubscribe?.();
|
|
171
|
+
this.unsubscribe = null;
|
|
172
|
+
this.onClose();
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private ensureProcessVisible(totalProcesses: number): void {
|
|
180
|
+
const maxVisibleProcesses =
|
|
181
|
+
configLoader.getConfig().processList.maxVisibleProcesses;
|
|
182
|
+
const visibleCount = Math.min(maxVisibleProcesses, totalProcesses);
|
|
183
|
+
if (this.selectedIndex < this.processScrollOffset) {
|
|
184
|
+
this.processScrollOffset = this.selectedIndex;
|
|
185
|
+
} else if (this.selectedIndex >= this.processScrollOffset + visibleCount) {
|
|
186
|
+
this.processScrollOffset = this.selectedIndex - visibleCount + 1;
|
|
187
|
+
}
|
|
188
|
+
this.processScrollOffset = Math.max(
|
|
189
|
+
0,
|
|
190
|
+
Math.min(this.processScrollOffset, totalProcesses - visibleCount),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
invalidate(): void {
|
|
195
|
+
this.cachedWidth = 0;
|
|
196
|
+
this.cachedLines = [];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
render(width: number): string[] {
|
|
200
|
+
if (width === this.cachedWidth && this.cachedLines.length > 0) {
|
|
201
|
+
return this.cachedLines;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const cfg = configLoader.getConfig().processList;
|
|
205
|
+
const maxVisibleProcesses = cfg.maxVisibleProcesses;
|
|
206
|
+
const maxPreviewLines = cfg.maxPreviewLines;
|
|
207
|
+
|
|
208
|
+
const theme = this.theme;
|
|
209
|
+
const dim = (s: string) => theme.fg("dim", s);
|
|
210
|
+
const accent = (s: string) => theme.fg("accent", s);
|
|
211
|
+
const warning = (s: string) => theme.fg("warning", s);
|
|
212
|
+
|
|
213
|
+
const lines: string[] = [];
|
|
214
|
+
const processes = this.manager.list();
|
|
215
|
+
const innerWidth = width - 2;
|
|
216
|
+
|
|
217
|
+
const padLine = createPanelPadder(width);
|
|
218
|
+
|
|
219
|
+
lines.push(renderPanelTitleLine("Background Processes", width, theme));
|
|
220
|
+
|
|
221
|
+
if (processes.length === 0) {
|
|
222
|
+
lines.push(padLine(""));
|
|
223
|
+
lines.push(padLine(dim("No background processes")));
|
|
224
|
+
lines.push(padLine(dim("Use the processes tool to start commands")));
|
|
225
|
+
lines.push(padLine(""));
|
|
226
|
+
} else {
|
|
227
|
+
const prefixWidth = 2;
|
|
228
|
+
const idWidth = 9;
|
|
229
|
+
const nameWidth = 15;
|
|
230
|
+
const statusWidth = 18;
|
|
231
|
+
const timeWidth = 8;
|
|
232
|
+
const sizeWidth = 8;
|
|
233
|
+
|
|
234
|
+
const hasProcessScroll = processes.length > maxVisibleProcesses;
|
|
235
|
+
const headerSuffixText = hasProcessScroll
|
|
236
|
+
? ` [${this.processScrollOffset + 1}-${Math.min(this.processScrollOffset + maxVisibleProcesses, processes.length)}/${processes.length}]`
|
|
237
|
+
: "";
|
|
238
|
+
const headerSuffixLen = hasProcessScroll ? headerSuffixText.length : 0;
|
|
239
|
+
|
|
240
|
+
// Reserve space for scroll suffix in the command column
|
|
241
|
+
const cmdWidth = Math.max(
|
|
242
|
+
10,
|
|
243
|
+
innerWidth -
|
|
244
|
+
prefixWidth -
|
|
245
|
+
idWidth -
|
|
246
|
+
nameWidth -
|
|
247
|
+
statusWidth -
|
|
248
|
+
timeWidth -
|
|
249
|
+
sizeWidth -
|
|
250
|
+
headerSuffixLen,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
lines.push(padLine(""));
|
|
254
|
+
const header =
|
|
255
|
+
" " +
|
|
256
|
+
dim("ID".padEnd(idWidth)) +
|
|
257
|
+
dim("Name".padEnd(nameWidth)) +
|
|
258
|
+
dim("Command".padEnd(cmdWidth)) +
|
|
259
|
+
dim("Status".padEnd(statusWidth)) +
|
|
260
|
+
dim("Time".padEnd(timeWidth)) +
|
|
261
|
+
dim("Size".padStart(sizeWidth)) +
|
|
262
|
+
(hasProcessScroll ? dim(headerSuffixText) : "");
|
|
263
|
+
lines.push(padLine(header));
|
|
264
|
+
lines.push(renderPanelRule(width, theme));
|
|
265
|
+
|
|
266
|
+
const visibleProcessCount = Math.min(
|
|
267
|
+
maxVisibleProcesses,
|
|
268
|
+
processes.length,
|
|
269
|
+
);
|
|
270
|
+
const startIdx = this.processScrollOffset;
|
|
271
|
+
const endIdx = startIdx + visibleProcessCount;
|
|
272
|
+
|
|
273
|
+
for (let i = startIdx; i < endIdx; i++) {
|
|
274
|
+
const proc = processes[i];
|
|
275
|
+
if (!proc) continue;
|
|
276
|
+
const isSelected = i === this.selectedIndex;
|
|
277
|
+
const sizes = this.manager.getFileSize(proc.id);
|
|
278
|
+
const totalSize = sizes ? sizes.stdout + sizes.stderr : 0;
|
|
279
|
+
|
|
280
|
+
const statusText = this.formatStatus(proc);
|
|
281
|
+
const statusPadding =
|
|
282
|
+
statusWidth + (statusText.length - visibleWidth(statusText));
|
|
283
|
+
|
|
284
|
+
const row =
|
|
285
|
+
(isSelected
|
|
286
|
+
? accent(proc.id.padEnd(idWidth))
|
|
287
|
+
: proc.id.padEnd(idWidth)) +
|
|
288
|
+
truncate(proc.name, nameWidth - 1).padEnd(nameWidth) +
|
|
289
|
+
truncate(proc.command, cmdWidth - 1).padEnd(cmdWidth) +
|
|
290
|
+
statusText.padEnd(statusPadding) +
|
|
291
|
+
formatRuntime(proc.startTime, proc.endTime).padEnd(timeWidth) +
|
|
292
|
+
formatBytes(totalSize).padStart(sizeWidth);
|
|
293
|
+
|
|
294
|
+
if (isSelected) {
|
|
295
|
+
lines.push(padLine(`${accent(">")} ${row}`));
|
|
296
|
+
} else {
|
|
297
|
+
lines.push(padLine(` ${row}`));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (let i = visibleProcessCount; i < maxVisibleProcesses; i++) {
|
|
302
|
+
lines.push(padLine(""));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (this.selectedIndex < processes.length) {
|
|
306
|
+
const selected = processes[this.selectedIndex];
|
|
307
|
+
if (!selected) {
|
|
308
|
+
this.cachedLines = lines;
|
|
309
|
+
this.cachedWidth = width;
|
|
310
|
+
return this.cachedLines;
|
|
311
|
+
}
|
|
312
|
+
const output = this.manager.getOutput(selected.id, maxPreviewLines * 2);
|
|
313
|
+
const sizes = this.manager.getFileSize(selected.id);
|
|
314
|
+
|
|
315
|
+
lines.push(renderPanelRule(width, theme));
|
|
316
|
+
|
|
317
|
+
const logTitlePlain = `Output: ${selected.name} (${selected.id})`;
|
|
318
|
+
const sizeInfoPlain = sizes
|
|
319
|
+
? ` stdout: ${formatBytes(sizes.stdout)}, stderr: ${formatBytes(sizes.stderr)}`
|
|
320
|
+
: "";
|
|
321
|
+
const combinedPlain = logTitlePlain + sizeInfoPlain;
|
|
322
|
+
// Truncate if combined exceeds innerWidth, prioritizing the title
|
|
323
|
+
if (combinedPlain.length <= innerWidth) {
|
|
324
|
+
const logTitle = `Output: ${accent(selected.name)} ${dim(`(${selected.id})`)}`;
|
|
325
|
+
const sizeInfo = sizes ? dim(sizeInfoPlain) : "";
|
|
326
|
+
lines.push(padLine(logTitle + sizeInfo));
|
|
327
|
+
} else {
|
|
328
|
+
const maxNameLen = Math.max(
|
|
329
|
+
8,
|
|
330
|
+
innerWidth -
|
|
331
|
+
(`Output: (${selected.id})`.length + sizeInfoPlain.length),
|
|
332
|
+
);
|
|
333
|
+
const tName = truncate(selected.name, maxNameLen);
|
|
334
|
+
const logTitle = `Output: ${accent(tName)} ${dim(`(${selected.id})`)}`;
|
|
335
|
+
const sizeInfo = sizes ? dim(sizeInfoPlain) : "";
|
|
336
|
+
lines.push(padLine(logTitle + sizeInfo));
|
|
337
|
+
}
|
|
338
|
+
lines.push(padLine(""));
|
|
339
|
+
|
|
340
|
+
let renderedLines = 0;
|
|
341
|
+
|
|
342
|
+
if (output) {
|
|
343
|
+
const logLines: { type: "stdout" | "stderr"; text: string }[] = [];
|
|
344
|
+
for (const line of output.stdout) {
|
|
345
|
+
logLines.push({ type: "stdout", text: line });
|
|
346
|
+
}
|
|
347
|
+
for (const line of output.stderr) {
|
|
348
|
+
logLines.push({ type: "stderr", text: line });
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (logLines.length === 0) {
|
|
352
|
+
lines.push(padLine(dim("(no output yet)")));
|
|
353
|
+
renderedLines = 1;
|
|
354
|
+
} else {
|
|
355
|
+
const startIdx = Math.max(
|
|
356
|
+
0,
|
|
357
|
+
logLines.length - maxPreviewLines - this.logScrollOffset,
|
|
358
|
+
);
|
|
359
|
+
const endIdx = Math.max(0, logLines.length - this.logScrollOffset);
|
|
360
|
+
const visibleLines = logLines.slice(startIdx, endIdx);
|
|
361
|
+
|
|
362
|
+
this.scrollInfo.above = startIdx;
|
|
363
|
+
this.scrollInfo.below =
|
|
364
|
+
this.logScrollOffset > 0 ? logLines.length - endIdx : 0;
|
|
365
|
+
|
|
366
|
+
for (const line of visibleLines) {
|
|
367
|
+
const displayLine = truncate(
|
|
368
|
+
stripAnsi(line.text),
|
|
369
|
+
innerWidth - 2,
|
|
370
|
+
);
|
|
371
|
+
if (line.type === "stderr") {
|
|
372
|
+
lines.push(padLine(warning(displayLine)));
|
|
373
|
+
} else {
|
|
374
|
+
lines.push(padLine(displayLine));
|
|
375
|
+
}
|
|
376
|
+
renderedLines++;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
while (renderedLines < maxPreviewLines) {
|
|
382
|
+
lines.push(padLine(""));
|
|
383
|
+
renderedLines++;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
lines.push(renderPanelRule(width, theme));
|
|
389
|
+
|
|
390
|
+
const footerLeft =
|
|
391
|
+
`${dim("enter")} stream ` +
|
|
392
|
+
`${dim("j/k")} select ` +
|
|
393
|
+
`${dim("x")} term/kill ` +
|
|
394
|
+
`${dim("c")} clear ` +
|
|
395
|
+
`${dim("q")} quit`;
|
|
396
|
+
|
|
397
|
+
let footerRight = "";
|
|
398
|
+
if (this.scrollInfo.above > 0 || this.scrollInfo.below > 0) {
|
|
399
|
+
const parts: string[] = [];
|
|
400
|
+
if (this.scrollInfo.above > 0) {
|
|
401
|
+
parts.push(`↑${this.scrollInfo.above}`);
|
|
402
|
+
}
|
|
403
|
+
if (this.scrollInfo.below > 0) {
|
|
404
|
+
parts.push(`↓${this.scrollInfo.below}`);
|
|
405
|
+
}
|
|
406
|
+
footerRight = `${dim("J/K")} scroll ${dim(parts.join(" "))}`;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const footerLeftLen = visibleWidth(footerLeft);
|
|
410
|
+
const footerRightLen = visibleWidth(footerRight);
|
|
411
|
+
const footerGap = Math.max(2, innerWidth - footerLeftLen - footerRightLen);
|
|
412
|
+
const footer = footerLeft + " ".repeat(footerGap) + footerRight;
|
|
413
|
+
|
|
414
|
+
lines.push(padLine(footer));
|
|
415
|
+
lines.push(renderPanelRule(width, theme));
|
|
416
|
+
|
|
417
|
+
this.cachedLines = lines;
|
|
418
|
+
this.cachedWidth = width;
|
|
419
|
+
|
|
420
|
+
return this.cachedLines;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private formatStatus(proc: ProcessInfo): string {
|
|
424
|
+
const theme = this.theme;
|
|
425
|
+
const dim = (s: string) => theme.fg("dim", s);
|
|
426
|
+
const success = (s: string) => theme.fg("success", s);
|
|
427
|
+
const warning = (s: string) => theme.fg("warning", s);
|
|
428
|
+
const error = (s: string) => theme.fg("error", s);
|
|
429
|
+
|
|
430
|
+
const icon = statusIcon(proc.status, proc.success);
|
|
431
|
+
const label = statusLabel(proc);
|
|
432
|
+
|
|
433
|
+
switch (proc.status) {
|
|
434
|
+
case "running":
|
|
435
|
+
return success(`${icon} ${label}`);
|
|
436
|
+
case "terminating":
|
|
437
|
+
return warning(`${icon} ${label}`);
|
|
438
|
+
case "terminate_timeout":
|
|
439
|
+
return error(`${icon} ${label}`);
|
|
440
|
+
case "killed":
|
|
441
|
+
return warning(`${icon} ${label}`);
|
|
442
|
+
case "exited":
|
|
443
|
+
return proc.success
|
|
444
|
+
? dim(`${icon} ${label}`)
|
|
445
|
+
: error(`${icon} ${label}`);
|
|
446
|
+
default:
|
|
447
|
+
return dim(`${icon} ${label}`);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ProcessInfo, ProcessStatus } from "../constants";
|
|
2
|
+
|
|
3
|
+
export function statusLabel(proc: ProcessInfo): string {
|
|
4
|
+
switch (proc.status) {
|
|
5
|
+
case "running":
|
|
6
|
+
return "running";
|
|
7
|
+
case "terminating":
|
|
8
|
+
return "terminating";
|
|
9
|
+
case "terminate_timeout":
|
|
10
|
+
return "terminate_timeout";
|
|
11
|
+
case "killed":
|
|
12
|
+
return "killed";
|
|
13
|
+
case "exited":
|
|
14
|
+
return proc.success ? "exit(0)" : `exit(${proc.exitCode ?? "?"})`;
|
|
15
|
+
default:
|
|
16
|
+
return proc.status;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function statusIcon(
|
|
21
|
+
status: ProcessStatus,
|
|
22
|
+
success: boolean | null,
|
|
23
|
+
): string {
|
|
24
|
+
switch (status) {
|
|
25
|
+
case "running":
|
|
26
|
+
return "\u25CF"; // filled circle
|
|
27
|
+
case "terminating":
|
|
28
|
+
return "\u25CF"; // filled circle
|
|
29
|
+
case "terminate_timeout":
|
|
30
|
+
return "\u2717"; // x mark
|
|
31
|
+
case "exited":
|
|
32
|
+
return success ? "\u2713" : "\u2717";
|
|
33
|
+
case "killed":
|
|
34
|
+
return "\u2717";
|
|
35
|
+
default:
|
|
36
|
+
return "?";
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for the processes extension.
|
|
3
|
+
*
|
|
4
|
+
* Global: ~/.pi/agent/extensions/processes.json
|
|
5
|
+
* Memory: ephemeral overrides via /process:settings
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ConfigLoader } from "@aliou/pi-utils-settings";
|
|
9
|
+
|
|
10
|
+
export interface ProcessesConfig {
|
|
11
|
+
processList?: {
|
|
12
|
+
/** Max visible processes in the /process:list TUI list. */
|
|
13
|
+
maxVisibleProcesses?: number;
|
|
14
|
+
/** Max log preview lines shown below the selected process. */
|
|
15
|
+
maxPreviewLines?: number;
|
|
16
|
+
};
|
|
17
|
+
output?: {
|
|
18
|
+
/** Default number of tail lines returned to the agent. */
|
|
19
|
+
defaultTailLines?: number;
|
|
20
|
+
/** Hard cap on output lines returned to the agent. */
|
|
21
|
+
maxOutputLines?: number;
|
|
22
|
+
};
|
|
23
|
+
execution?: {
|
|
24
|
+
/** Absolute shell path override. Leave unset to auto-resolve. */
|
|
25
|
+
shellPath?: string;
|
|
26
|
+
};
|
|
27
|
+
widget?: {
|
|
28
|
+
/** Show the status widget below the editor. */
|
|
29
|
+
showStatusWidget?: boolean;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ResolvedProcessesConfig {
|
|
34
|
+
processList: {
|
|
35
|
+
maxVisibleProcesses: number;
|
|
36
|
+
maxPreviewLines: number;
|
|
37
|
+
};
|
|
38
|
+
output: {
|
|
39
|
+
defaultTailLines: number;
|
|
40
|
+
maxOutputLines: number;
|
|
41
|
+
};
|
|
42
|
+
execution: {
|
|
43
|
+
shellPath?: string;
|
|
44
|
+
};
|
|
45
|
+
widget: {
|
|
46
|
+
showStatusWidget: boolean;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const DEFAULT_CONFIG: ResolvedProcessesConfig = {
|
|
51
|
+
processList: {
|
|
52
|
+
maxVisibleProcesses: 8,
|
|
53
|
+
maxPreviewLines: 12,
|
|
54
|
+
},
|
|
55
|
+
output: {
|
|
56
|
+
defaultTailLines: 100,
|
|
57
|
+
maxOutputLines: 200,
|
|
58
|
+
},
|
|
59
|
+
execution: {},
|
|
60
|
+
widget: {
|
|
61
|
+
showStatusWidget: true,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const configLoader = new ConfigLoader<
|
|
66
|
+
ProcessesConfig,
|
|
67
|
+
ResolvedProcessesConfig
|
|
68
|
+
>("process", DEFAULT_CONFIG, {
|
|
69
|
+
scopes: ["global", "memory"],
|
|
70
|
+
});
|