Skip to content

Commit fb61395

Browse files
committed
実行終了後もoutputをキャプチャする
1 parent 85f47a8 commit fb61395

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

app/terminal/exec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function ExecFile(props: ExecProps) {
6363
null, // ファイル実行で"return"メッセージが返ってくることはないはずなので、Prismを渡す必要はない
6464
props.language
6565
);
66+
// TODO: 実行が完了したあとに出力された場合、embedContextのsetExecResultにも出力を追加する必要があるが、それに対応したAPIになっていない
6667
});
6768
// TODO: 1つのファイル名しか受け付けないところに無理やりコンマ区切りで全部のファイル名を突っ込んでいる
6869
setExecResult(props.filenames.join(","), outputs);

app/terminal/repl.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function ReplTerminal({
130130

131131
// inputBufferを更新し、画面に描画する
132132
const updateBuffer = useCallback(
133-
(newBuffer: () => string[]) => {
133+
(newBuffer: (() => string[]) | null, insertBefore?: () => void) => {
134134
if (terminalInstanceRef.current) {
135135
hideCursor(terminalInstanceRef.current);
136136
// バッファの行数分カーソルを戻す
@@ -142,8 +142,12 @@ export function ReplTerminal({
142142
terminalInstanceRef.current.write("\r");
143143
// バッファの内容をクリア
144144
terminalInstanceRef.current.write("\x1b[0J");
145-
// 新しいバッファの内容を表示
146-
inputBuffer.current = newBuffer();
145+
// バッファの前に追加で出力する内容(前のコマンドの出力)があればここで書き込む
146+
insertBefore?.();
147+
// 新しいバッファの内容を表示、nullなら現状維持
148+
if (newBuffer) {
149+
inputBuffer.current = newBuffer();
150+
}
147151
for (let i = 0; i < inputBuffer.current.length; i++) {
148152
terminalInstanceRef.current.write(
149153
(i === 0 ? prompt : (promptMore ?? prompt)) ?? "> "
@@ -214,12 +218,22 @@ export function ReplTerminal({
214218
const command = inputBuffer.current.join("\n").trim();
215219
inputBuffer.current = [];
216220
const collectedOutputs: ReplOutput[] = [];
221+
let executionDone = false;
217222
await runtimeMutex.runExclusive(async () => {
218223
await runCommand(command, (output) => {
219-
collectedOutputs.push(output);
220-
handleOutput(output);
224+
if (executionDone) {
225+
// すでに完了していて次のコマンドのプロンプトが出ている場合、その前に挿入
226+
updateBuffer(null, () => {
227+
handleOutput(output);
228+
});
229+
// TODO: embedContextのaddReplOutputにも出力を追加する必要があるが、それに対応したAPIになっていない
230+
} else {
231+
collectedOutputs.push(output);
232+
handleOutput(output);
233+
}
221234
});
222235
});
236+
executionDone = true;
223237
updateBuffer(() => [""]);
224238
addReplOutput?.(terminalId, command, collectedOutputs);
225239
}

app/terminal/worker/jsEval.worker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ async function runCode(
9696
message: `${String(e)}`,
9797
});
9898
}
99-
} finally {
100-
currentOutputCallback = null;
10199
}
102100

103101
return { updatedFiles: {} as Record<string, string> };
@@ -126,8 +124,6 @@ function runFile(
126124
message: `${String(e)}`,
127125
});
128126
}
129-
} finally {
130-
currentOutputCallback = null;
131127
}
132128

133129
return { updatedFiles: {} as Record<string, string> };

app/terminal/worker/pyodide.worker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ async function runCode(
107107
message: `予期せぬエラー: ${String(e).trim()}`,
108108
});
109109
}
110-
} finally {
111-
currentOutputCallback = null;
112110
}
113111

114112
const updatedFiles = readAllFiles();
@@ -165,8 +163,6 @@ async function runFile(
165163
message: `予期せぬエラー: ${String(e).trim()}`,
166164
});
167165
}
168-
} finally {
169-
currentOutputCallback = null;
170166
}
171167

172168
const updatedFiles = readAllFiles();

app/terminal/worker/ruby.worker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ async function runCode(
137137
type: "error",
138138
message: formatRubyError(e, false),
139139
});
140-
} finally {
141-
currentOutputCallback = null;
142140
}
143141

144142
const updatedFiles = readAllFiles();
@@ -189,8 +187,6 @@ async function runFile(
189187
type: "error",
190188
message: formatRubyError(e, true),
191189
});
192-
} finally {
193-
currentOutputCallback = null;
194190
}
195191

196192
const updatedFiles = readAllFiles();

0 commit comments

Comments
 (0)