Skip to content

Commit 26ee453

Browse files
committed
🐛 Contain foreground command output within REPL entries
1 parent 9b98cbe commit 26ee453

8 files changed

Lines changed: 607 additions & 12 deletions

File tree

‎packages/cli/src/repl/application.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ import type { ReplHistoryNavigation } from "./navigation.ts";
111111
import { blankBand, EMPTY_BAND } from "./history-rail.ts";
112112
import type { ReplPreparedRail } from "./history-rail.ts";
113113
import { entryReading, NO_READING } from "./source-reading.ts";
114+
import type { ReplCommandOutput } from "./command-output.ts";
114115
import type { ReplReading } from "./source-reading.ts";
115116
import { readingRuns, fitPlain } from "./fitting.ts";
116117
import type { ReplPlainFitStore } from "./fitting.ts";
@@ -185,6 +186,7 @@ export type { ReplAction };
185186

186187
/** What this process holds that the Journal does not. */
187188
export interface ReplLive {
189+
readonly commandOutput?: readonly ReplCommandOutput[];
188190
/** Output no recorded outcome has replaced yet. */
189191
readonly output: string;
190192
/** The question waiting right now, or none. */
@@ -6147,6 +6149,7 @@ export function readingOf(view: ReplView): ReplReading {
61476149
entry,
61486150
lifecycle: view.live.lifecycle,
61496151
live: livesHere(view.model, view.selection) ? view.live.output : "",
6152+
commandOutput: view.live.commandOutput?.find((one) => one.entry === entry.key)?.chunks ?? [],
61506153
inspected: view.state.route.at !== undefined,
61516154
});
61526155
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export interface ReplCommandChunk {
2+
readonly channel: "stdout" | "stderr";
3+
readonly text: string;
4+
}
5+
6+
export interface ReplCommandOutput {
7+
readonly entry: string;
8+
readonly chunks: readonly ReplCommandChunk[];
9+
}
10+
11+
export function inertCommandText(text: string): string {
12+
return text.replace(
13+
/[\x00-\x09\x0b-\x1f\x7f-\x9f]/g,
14+
(control) => `\\x${control.charCodeAt(0).toString(16).padStart(2, "0")}`,
15+
);
16+
}

‎packages/cli/src/repl/program.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,7 @@ function liveOf(
24512451
): ReplLive {
24522452
return {
24532453
output: session.overlay.output,
2454+
commandOutput: session.commandOutput,
24542455
question: session.overlay.question,
24552456
expansion: session.expansion.state,
24562457
pausable: session.controller !== undefined,

‎packages/cli/src/repl/session.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ import { INLINE_SOURCE_PATH, inlineSource, validateDocument } from "@executablem
8080
import type { DocumentValidationDiagnostic, Json, PermissionMode } from "@executablemd/core";
8181
import type { GeneratedSelection } from "@executablemd/core/api";
8282
import { API, Permissions } from "@executablemd/runtime";
83+
import { Stdio } from "@effectionx/process";
84+
import type { ReplCommandChunk, ReplCommandOutput } from "./command-output.ts";
8385
import { executeInstalled } from "@executablemd/core/host";
8486
import { useGeneratedFallback } from "../evaluation-profile.ts";
8587
import type {
@@ -391,6 +393,7 @@ export interface ReplSession {
391393
* {@link ReplPrinting} for why the overlay is not this.
392394
*/
393395
readonly printing: ReplPrinting | undefined;
396+
readonly commandOutput: readonly ReplCommandOutput[];
394397
/**
395398
* What this process is doing element by element, for the entry that is open.
396399
*
@@ -791,6 +794,7 @@ function* start(
791794

792795
/** Text the entry that is running has printed, which no record holds yet. */
793796
let output = "";
797+
const commandOutput = new Map<string, ReplCommandChunk[]>();
794798
/** Whether an entry's execution is still running in this process. */
795799
let live = false;
796800
/**
@@ -1014,6 +1018,16 @@ function* start(
10141018
// the record that outcome carries.
10151019
return live && running !== undefined ? { entry: running.key, output } : undefined;
10161020
},
1021+
get commandOutput() {
1022+
return Object.freeze(
1023+
[...commandOutput].map(([entry, chunks]) =>
1024+
Object.freeze({
1025+
entry,
1026+
chunks: Object.freeze([...chunks]),
1027+
}),
1028+
),
1029+
);
1030+
},
10171031
get lifecycle() {
10181032
return lifecycle.reading();
10191033
},
@@ -1780,6 +1794,42 @@ function* start(
17801794
workspace?: true,
17811795
): Operation<Result<unknown>> {
17821796
try {
1797+
const chunks: ReplCommandChunk[] = [];
1798+
const stdout = new TextDecoder();
1799+
const stderr = new TextDecoder();
1800+
function append(channel: ReplCommandChunk["channel"], text: string): void {
1801+
if (text.length === 0) {
1802+
return;
1803+
}
1804+
const last = chunks[chunks.length - 1];
1805+
const chunk = Object.freeze({
1806+
channel,
1807+
text: last?.channel === channel ? last.text + text : text,
1808+
});
1809+
if (last?.channel === channel) {
1810+
chunks[chunks.length - 1] = chunk;
1811+
} else {
1812+
chunks.push(chunk);
1813+
}
1814+
commandOutput.set(key, chunks);
1815+
outputs.send(output);
1816+
}
1817+
// Registered before entry resources so their cleanup can still emit.
1818+
yield* ensure(() => {
1819+
append("stdout", stdout.decode());
1820+
append("stderr", stderr.decode());
1821+
});
1822+
yield* Stdio.around(
1823+
{
1824+
*stdout([bytes]) {
1825+
append("stdout", stdout.decode(bytes, { stream: true }));
1826+
},
1827+
*stderr([bytes]) {
1828+
append("stderr", stderr.decode(bytes, { stream: true }));
1829+
},
1830+
},
1831+
{ at: "min" },
1832+
);
17831833
const directory = workspace === true ? options.workspace : options.projectCwd;
17841834
if (directory !== undefined) {
17851835
yield* API.Env.around(

‎packages/cli/src/repl/source-reading.ts‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import type { ReplPresentationRole, ReplRowStyle } from "./presentation-style.ts
4848
import { tokenRuns } from "./description.ts";
4949
import type { ReplTokenRun } from "./description.ts";
5050
import { sourceRuns } from "./presentation-text.ts";
51+
import { inertCommandText } from "./command-output.ts";
52+
import type { ReplCommandChunk } from "./command-output.ts";
5153

5254
/** What this terminal draws for each phase, glyph and word together. */
5355
const BADGES: Readonly<
@@ -117,6 +119,7 @@ export interface ReplReadingSection {
117119
/** One entry's whole reading: what it produced, then what produced it. */
118120
export interface ReplReading {
119121
readonly output: ReplReadingSection;
122+
readonly commands?: readonly ReplReadingSection[];
120123
readonly source: ReplReadingSection;
121124
}
122125

@@ -146,6 +149,10 @@ export function readingLines(reading: ReplReading): readonly ReplReadingLine[] {
146149
return Object.freeze([
147150
caption("output", reading.output),
148151
...reading.output.lines,
152+
...(reading.commands ?? []).flatMap((section, index) => [
153+
caption(`command:${index}`, section),
154+
...section.lines,
155+
]),
149156
caption("source", reading.source),
150157
...reading.source.lines,
151158
]);
@@ -165,6 +172,7 @@ function caption(key: string, section: ReplReadingSection): ReplReadingLine {
165172

166173
/** What a reading is being built for. */
167174
export interface ReplReadingRequest {
175+
readonly commandOutput?: readonly ReplCommandChunk[];
168176
readonly entry: ReplEntry;
169177
/** This session's observations, or an empty reading for a retained prefix. */
170178
readonly lifecycle: ReplLifecycleReading;
@@ -184,6 +192,19 @@ export interface ReplReadingRequest {
184192
export function entryReading(request: ReplReadingRequest): ReplReading {
185193
return Object.freeze({
186194
output: outputSection(request),
195+
commands: Object.freeze(
196+
(request.commandOutput ?? []).map((chunk, index) =>
197+
Object.freeze({
198+
caption: `Command ${chunk.channel}`,
199+
captionStyle: styleOf("pane-heading", { inspected: request.inspected }),
200+
lines: plain(
201+
chunk.text,
202+
`command:${index}`,
203+
styleOf("output", { inspected: request.inspected }),
204+
),
205+
}),
206+
),
207+
),
187208
source: sourceSection(request),
188209
});
189210
}
@@ -257,17 +278,19 @@ function outputSection(request: ReplReadingRequest): ReplReadingSection {
257278
/** Text as lines nothing has been observed in. */
258279
function plain(text: string, prefix: string, style: ReplRowStyle): readonly ReplReadingLine[] {
259280
return Object.freeze(
260-
text.split("\n").map((one, index) =>
261-
Object.freeze({
262-
key: `${prefix}:${index}`,
263-
text: one,
264-
runs: tokenRuns([{ text: one, token: style.role }]),
265-
rail: "rail-pending" as const,
266-
depth: 0,
267-
badge: undefined,
268-
style,
269-
}),
270-
),
281+
inertCommandText(text)
282+
.split("\n")
283+
.map((one, index) =>
284+
Object.freeze({
285+
key: `${prefix}:${index}`,
286+
text: one,
287+
runs: tokenRuns([{ text: one, token: style.role }]),
288+
rail: "rail-pending" as const,
289+
depth: 0,
290+
badge: undefined,
291+
style,
292+
}),
293+
),
271294
);
272295
}
273296

0 commit comments

Comments
 (0)