-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.tsx
More file actions
184 lines (179 loc) · 6.53 KB
/
exec.tsx
File metadata and controls
184 lines (179 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use client";
import {
clearTerminal,
getRows,
hideCursor,
systemMessageColor,
useTerminal,
} from "./terminal";
import { writeOutput } from "./repl";
import { useEffect, useState } from "react";
import { useEmbedContext } from "./embedContext";
import { RuntimeLang, useRuntime } from "./runtime";
import clsx from "clsx";
interface ExecProps {
/*
* Pythonの場合はメインファイル1つのみを指定する。
* C++の場合はソースコード(.cpp)を全部指定する。
*/
filenames: string[];
language: RuntimeLang;
content: string;
}
export function ExecFile(props: ExecProps) {
const { terminalRef, terminalInstanceRef, termReady } = useTerminal({
getRows: (cols: number) => getRows(props.content, cols) + 1,
onReady: () => {
hideCursor(terminalInstanceRef.current!);
for (const line of props.content.split("\n")) {
terminalInstanceRef.current!.writeln(line);
}
},
});
const { files, clearExecResult, addExecOutput } = useEmbedContext();
const { ready, runFiles, getCommandlineStr, runtimeInfo, interrupt } =
useRuntime(props.language);
// ユーザーがクリックした時(triggered) && ランタイムが準備できた時に、実際にinitCommandを実行する(executing)
const [executionState, setExecutionState] = useState<
"idle" | "triggered" | "executing"
>("idle");
useEffect(() => {
if (executionState === "triggered" && ready) {
setExecutionState("executing");
(async () => {
clearTerminal(terminalInstanceRef.current!);
terminalInstanceRef.current!.write(systemMessageColor("実行中です..."));
// TODO: 1つのファイル名しか受け付けないところに無理やりコンマ区切りで全部のファイル名を突っ込んでいる
const filenameKey = props.filenames.join(",");
clearExecResult(filenameKey);
let isFirstOutput = true;
await runFiles(props.filenames, files, (output) => {
addExecOutput(filenameKey, output);
if (isFirstOutput) {
// Clear "実行中です..." message only on first output
clearTerminal(terminalInstanceRef.current!);
isFirstOutput = false;
}
// Append only the new output
writeOutput(
terminalInstanceRef.current!,
output,
undefined,
null, // ファイル実行で"return"メッセージが返ってくることはないはずなので、Prismを渡す必要はない
props.language
);
});
setExecutionState("idle");
})();
}
}, [
executionState,
ready,
props.filenames,
runFiles,
clearExecResult,
addExecOutput,
terminalInstanceRef,
props.language,
files,
]);
return (
<div className="border border-accent border-2 shadow-md m-2 rounded-box relative">
<div className="bg-base-200 flex items-center rounded-t-box">
<button
/* daisyuiのbtnはheightがvar(--size)で固定。
ここでは最小でそのサイズ、ただし親コンテナがそれより大きい場合に大きくしたい
→ heightを解除し、min-heightをデフォルトのサイズと同じにする */
className={clsx(
"btn btn-soft btn-accent h-[unset]! min-h-(--size) self-stretch",
"rounded-none rounded-tl-[calc(var(--radius-box)-2px)]"
)}
onClick={() => {
if (!ready) {
clearTerminal(terminalInstanceRef.current!);
terminalInstanceRef.current!.write(
systemMessageColor(
"(初期化しています...しばらくお待ちください)"
)
);
}
if (executionState === "idle") {
setExecutionState("triggered");
}
if (executionState === "executing" && interrupt) {
// Ctrl+C
interrupt();
terminalInstanceRef.current!.write("^C");
}
}}
disabled={
!termReady ||
!(
executionState === "idle" ||
(executionState === "executing" && interrupt)
)
}
>
{executionState === "idle" ? "▶ 実行" : "■ 停止"}
</button>
<code className="text-left break-all text-sm my-1 ml-4">
{getCommandlineStr?.(props.filenames)}
</code>
<div className="ml-1 mr-1 tooltip tooltip-secondary tooltip-bottom z-1">
{/*なぜかわからないがz-1がないと後ろに隠れてしまう*/}
<div className="tooltip-content bg-secondary/60 backdrop-blur-xs">
ブラウザ上で動作する
<span className="mx-0.5">
{runtimeInfo?.prettyLangName || props.language}
</span>
{runtimeInfo?.version && (
<span className="mr-0.5">{runtimeInfo?.version}</span>
)}
の実行環境です。
<br />
左上の実行ボタンを押して、このページ内の
{props.filenames.map((fname) => (
<span key={fname}>
<span className="font-mono mx-0.5">{fname}</span>
</span>
))}
に書かれている内容を実行します。
</div>
<button
className={clsx(
"btn btn-xs btn-soft btn-secondary rounded-full cursor-help"
)}
>
?
</button>
</div>
</div>
<div className="bg-base-300 p-4 pr-1 pt-2 relative rounded-b-box">
{/*
ターミナル表示の初期化が完了するまでの間、ターミナルは隠し、内容をそのまま表示する。
可能な限りレイアウトが崩れないようにするため & SSRでも内容が読めるように(SEO?)という意味もある
*/}
<pre
className={clsx(
"font-mono overflow-auto cursor-wait",
"min-h-26", // xterm.jsで5行分の高さ
termReady && "hidden"
)}
>
{props.content}
</pre>
<div
className={clsx(
!termReady &&
/* "hidden" だとterminalがdivのサイズを取得しようとしたときにバグる*/
"absolute invisible"
)}
ref={terminalRef}
/>
{executionState !== "idle" && (
<div className="absolute z-10 inset-0 cursor-wait" />
)}
</div>
</div>
);
}