Skip to content

Commit 5574883

Browse files
committed
rubyのプロンプト仕様を整備
1 parent 4f2b0e1 commit 5574883

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

app/terminal/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const sampleConfig: Record<RuntimeLang, SampleConfig> = {
6060
},
6161
ruby: {
6262
repl: true,
63-
replInitContent: '>> puts "Hello, World!"\nHello, World!',
63+
replInitContent: 'irb(main):001:0> puts "Hello, World!"\nHello, World!',
6464
editor: {
6565
"main.rb": 'puts "Hello, World!"',
6666
},

app/terminal/repl.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export type SyntaxStatus = "complete" | "incomplete" | "invalid"; // 構文チ
2828
export function writeOutput(
2929
term: Terminal,
3030
outputs: ReplOutput[],
31-
endNewLine: boolean
31+
endNewLine: boolean,
32+
returnPrefix: string | undefined,
33+
language: RuntimeLang
3234
) {
3335
for (let i = 0; i < outputs.length; i++) {
3436
const output = outputs[i];
@@ -47,6 +49,12 @@ export function writeOutput(
4749
case "system":
4850
term.write(systemMessageColor(message));
4951
break;
52+
case "return":
53+
if (returnPrefix) {
54+
term.write(returnPrefix);
55+
}
56+
term.write(highlightCodeToAnsi(message, language));
57+
break;
5058
default:
5159
term.write(message);
5260
break;
@@ -77,7 +85,7 @@ export function ReplTerminal({
7785
checkSyntax,
7886
splitReplExamples,
7987
} = useRuntime(language);
80-
const { tabSize, prompt, promptMore } = langConstants(language);
88+
const { tabSize, prompt, promptMore, returnPrefix } = langConstants(language);
8189
if (!prompt) {
8290
console.warn(`prompt not defined for language: ${language}`);
8391
}
@@ -160,12 +168,18 @@ export function ReplTerminal({
160168
const handleOutput = useCallback(
161169
(outputs: ReplOutput[]) => {
162170
if (terminalInstanceRef.current) {
163-
writeOutput(terminalInstanceRef.current, outputs, true);
171+
writeOutput(
172+
terminalInstanceRef.current,
173+
outputs,
174+
true,
175+
returnPrefix,
176+
language
177+
);
164178
// 出力が終わったらプロンプトを表示
165179
updateBuffer(() => [""]);
166180
}
167181
},
168-
[updateBuffer, terminalInstanceRef]
182+
[updateBuffer, terminalInstanceRef, returnPrefix, language]
169183
);
170184

171185
const keyHandler = useCallback(

app/terminal/runtime.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface LangConstants {
3030
tabSize: number;
3131
prompt?: string;
3232
promptMore?: string;
33+
returnPrefix?: string;
3334
}
3435
export type RuntimeLang = "python" | "ruby" | "cpp" | "javascript";
3536

@@ -99,8 +100,9 @@ export function langConstants(lang: RuntimeLang | AceLang): LangConstants {
99100
case "ruby":
100101
return {
101102
tabSize: 2,
102-
prompt: ">> ",
103-
promptMore: "?> ",
103+
// TODO: 実際のirbのプロンプトは静的でなく、(main)や番号などの動的な表示がある
104+
prompt: "irb> ",
105+
returnPrefix: "=> ",
104106
};
105107
case "javascript":
106108
return {

app/terminal/worker/ruby.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ export function useRuby() {
2121
function splitReplExamples(content: string): ReplCommand[] {
2222
const initCommands: { command: string; output: ReplOutput[] }[] = [];
2323
for (const line of content.split("\n")) {
24-
if (line.startsWith(">> ")) {
25-
// Ruby IRB uses >> as the prompt
26-
initCommands.push({ command: line.slice(3), output: [] });
27-
} else if (line.startsWith("?> ")) {
28-
// Ruby IRB uses ?> for continuation
24+
if (line.startsWith("irb")) {
25+
initCommands.push({
26+
command: line.slice(line.indexOf(" ") + 1),
27+
output: [],
28+
});
29+
} else if (line.startsWith("=> ")) {
2930
if (initCommands.length > 0) {
30-
initCommands[initCommands.length - 1].command += "\n" + line.slice(3);
31+
initCommands[initCommands.length - 1].output.push({
32+
type: "return",
33+
message: line.slice(3),
34+
});
3135
}
3236
} else {
3337
// Lines without prompt are output from the previous command

0 commit comments

Comments
 (0)