From 88b68d147e0974d4c5a48e7726bbf505e35c62d0 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Sun, 5 Jan 2025 15:28:00 +0800 Subject: [PATCH] feat(playground): enhance code compilation feedback and add syntax highlighting for output messages --- src/actions/docker/compile.ts | 25 ++++++++++++++++---- src/app/playground/page.tsx | 44 ++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/actions/docker/compile.ts b/src/actions/docker/compile.ts index cdb83e9..baa7436 100644 --- a/src/actions/docker/compile.ts +++ b/src/actions/docker/compile.ts @@ -23,7 +23,10 @@ export async function runCode(code: string, language: string) { Cmd: [ "sh", "-c", - `echo '${code}' > main.c && gcc main.c -o main && ./main`, + `export LANG=C.UTF-8 && printf '%s' '${code.replace( + /'/g, + "'\\''" + )}' > main.c && gcc main.c -o main && ./main`, ], Tty: false, }); @@ -39,7 +42,7 @@ export async function runCode(code: string, language: string) { const output = await new Promise((resolve, reject) => { let output = ""; stream.on("data", (chunk) => { - output += chunk.toString(); + output += chunk.toString("utf8"); }); stream.on("end", () => { resolve(output); @@ -50,13 +53,27 @@ export async function runCode(code: string, language: string) { }); console.log("Output:", output); - return output; + + // 检查输出中是否有错误信息 + const isCompilationSuccess = !output.includes("error:"); + + return { + success: isCompilationSuccess, + output: output, + message: isCompilationSuccess + ? "Compilation successful" + : "Compilation failed", + }; } else { throw new Error(`Unsupported language: ${language}`); } } catch (error) { console.error("Error running code:", error); - throw error; + return { + success: false, + output: error instanceof Error ? error.message : "Unknown error", + message: "Compilation failed", + }; } finally { if (container) { try { diff --git a/src/app/playground/page.tsx b/src/app/playground/page.tsx index 83373d3..05b9550 100644 --- a/src/app/playground/page.tsx +++ b/src/app/playground/page.tsx @@ -11,6 +11,7 @@ import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area"; import { COriginal, CplusplusOriginal, JavaOriginal } from "devicons-react"; import { connectToLanguageServer, LSP_SUPPORTED_LANGUAGES } from "@/lib/lsp"; +import { codeToHtml } from "shiki"; const files: { [key: string]: { name: string; language: string; value: string }; @@ -48,6 +49,7 @@ loader.config({ monaco }); export default function PlaygroundPage() { const [language, setLanguage] = useState("c"); + const [highlightedMessage, setHighlightedMessage] = useState(""); const file = files[language]; const editorRef = useRef(null); const webSocketRef = useRef(null); @@ -99,6 +101,29 @@ export default function PlaygroundPage() { handleLanguageChange(); }, [language]); + const handleSubmit = async () => { + const code = editorRef.current?.getValue(); + if (!code) { + console.error("No code to compile"); + return; + } + + const result = await runCode(code, language); + + // 根据编译结果显示不同的信息 + const statusMessage = result.success + ? "Compilation successful" + : "Compilation failed"; + const fullMessage = `${statusMessage}\n\n${result.output}`; + + const highlighted = await codeToHtml(fullMessage, { + lang: "log", // 或者根据你的需求选择合适的语言 + theme: "one-dark-pro", + }); + + setHighlightedMessage(highlighted); + }; + return (
@@ -151,17 +176,7 @@ export default function PlaygroundPage() {
-
-
-
+
-
+
+
+
);