Skip to content

Commit 59ce490

Browse files
authored
Merge pull request #100 from ut-code/fix-repl-init-prism
replの初期化時にコマンドが表示されないのを修正
2 parents d2c40aa + 0508904 commit 59ce490

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

app/terminal/repl.tsx

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,6 @@ export function ReplTerminal({
124124
}
125125
return rows + 2; // 最後のプロンプト行を含める
126126
},
127-
onReady: () => {
128-
if (initCommand) {
129-
for (const cmd of initCommand) {
130-
updateBuffer(() => cmd.command.split("\n"));
131-
terminalInstanceRef.current!.writeln("");
132-
inputBuffer.current = [];
133-
handleOutput(cmd.output);
134-
}
135-
}
136-
terminalInstanceRef.current!.scrollToTop();
137-
},
138127
});
139128

140129
// REPLのユーザー入力
@@ -143,7 +132,7 @@ export function ReplTerminal({
143132
// inputBufferを更新し、画面に描画する
144133
const updateBuffer = useCallback(
145134
(newBuffer: () => string[]) => {
146-
if (terminalInstanceRef.current && Prism) {
135+
if (terminalInstanceRef.current) {
147136
hideCursor(terminalInstanceRef.current);
148137
// バッファの行数分カーソルを戻す
149138
if (inputBuffer.current.length >= 2) {
@@ -161,9 +150,14 @@ export function ReplTerminal({
161150
(i === 0 ? prompt : (promptMore ?? prompt)) ?? "> "
162151
);
163152
if (language) {
164-
terminalInstanceRef.current.write(
165-
highlightCodeToAnsi(Prism, inputBuffer.current[i], language)
166-
);
153+
if (Prism) {
154+
terminalInstanceRef.current.write(
155+
highlightCodeToAnsi(Prism, inputBuffer.current[i], language)
156+
);
157+
} else {
158+
console.warn("Prism is not loaded, cannot highlight input code");
159+
terminalInstanceRef.current.write(inputBuffer.current[i]);
160+
}
167161
} else {
168162
terminalInstanceRef.current.write(inputBuffer.current[i]);
169163
}
@@ -193,7 +187,7 @@ export function ReplTerminal({
193187
updateBuffer(() => [""]);
194188
}
195189
},
196-
[updateBuffer, terminalInstanceRef, returnPrefix, language]
190+
[Prism, updateBuffer, terminalInstanceRef, returnPrefix, language]
197191
);
198192

199193
const keyHandler = useCallback(
@@ -288,18 +282,34 @@ export function ReplTerminal({
288282
}
289283
}, [keyHandler, termReady, runtimeReady, terminalInstanceRef]);
290284

291-
// ユーザーがクリックした時(triggered) && ランタイムが準備できた時に、実際にinitCommandを実行する(executing)
292285
const [initCommandState, setInitCommandState] = useState<
293-
"idle" | "triggered" | "executing" | "done"
294-
>("idle");
286+
"initializing" | "idle" | "triggered" | "executing" | "done"
287+
>("initializing");
295288
useEffect(() => {
296289
if (
290+
terminalInstanceRef.current &&
291+
termReady &&
292+
Prism &&
293+
initCommandState === "initializing"
294+
) {
295+
// xtermの初期化とPrismの読み込みが完了したら、initCommandを実行せず描画する
296+
if (initCommand) {
297+
for (const cmd of initCommand) {
298+
updateBuffer(() => cmd.command.split("\n"));
299+
terminalInstanceRef.current!.writeln("");
300+
inputBuffer.current = [];
301+
handleOutput(cmd.output);
302+
}
303+
}
304+
terminalInstanceRef.current!.scrollToTop();
305+
setInitCommandState("idle");
306+
} else if (
297307
terminalInstanceRef.current &&
298308
termReady &&
299309
runtimeReady &&
300310
initCommandState === "triggered"
301311
) {
302-
setInitCommandState("executing");
312+
// ユーザーがクリックした時(triggered) && ランタイムが準備できた時に、実際にinitCommandを実行する(executing) setInitCommandState("executing");
303313
(async () => {
304314
if (initCommand) {
305315
// 初期化時に実行するコマンドがある場合はそれを実行
@@ -338,6 +348,7 @@ export function ReplTerminal({
338348
handleOutput,
339349
termReady,
340350
terminalInstanceRef,
351+
Prism,
341352
]);
342353

343354
return (

0 commit comments

Comments
 (0)