Skip to content

Commit 24426e1

Browse files
committed
fix: 修复 Scala & Clojure 插件无效输出
1 parent 00ba2e4 commit 24426e1

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src-tauri/src/execution.rs

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ fn get_codeforge_cache_dir(language: &str) -> Result<PathBuf, String> {
5050
Ok(cache_dir)
5151
}
5252

53+
// 检查是否应该过滤 stderr 行
54+
fn should_filter_stderr_line(language: &str, line: &str) -> bool {
55+
match language {
56+
"clojure" => {
57+
// 过滤 Clojure 的常见警告信息
58+
line.contains("WARNING: Implicit use of clojure.main")
59+
|| line.contains("WARNING: name already refers to:")
60+
}
61+
"scala" => {
62+
// 过滤 Scala 的编译信息
63+
line.contains("[0m[0m[33mCompiling project")
64+
|| line.contains("[0m[0m")
65+
|| line.starts_with("\u{001b}")
66+
}
67+
_ => false,
68+
}
69+
}
70+
5371
// 停止执行命令
5472
#[tauri::command]
5573
pub async fn stop_execution(language: String) -> Result<bool, String> {
@@ -102,13 +120,11 @@ pub async fn execute_code(
102120
.duration_since(UNIX_EPOCH)
103121
.unwrap()
104122
.as_secs();
105-
let file_name = format!(
106-
"Codeforge_{}_{}.{}",
107-
request.language,
108-
timestamp,
109-
plugin.get_file_extension()
110-
);
111-
let file_path = temp_dir.join(file_name.clone());
123+
let file_work = format!("Codeforge_{}_{}", request.language, timestamp);
124+
let work_dir = temp_dir.join(&file_work);
125+
fs::create_dir_all(&work_dir).map_err(|e| format!("创建工作目录失败: {}", e))?;
126+
let file_name = format!("{}.{}", file_work, plugin.get_file_extension());
127+
let file_path = work_dir.join(&file_name);
112128

113129
// 写入代码到临时文件
114130
fs::write(&file_path, &request.code)
@@ -296,14 +312,17 @@ pub async fn execute_code(
296312
// 读取并发送 stderr
297313
while let Ok(line) = stderr_rx.try_recv() {
298314
stderr_lines.push(line.clone());
299-
let _ = app.emit(
300-
"code-output",
301-
serde_json::json!({
302-
"type": "stderr",
303-
"content": line,
304-
"language": request.language
305-
}),
306-
);
315+
// 过滤掉特定语言的警告信息
316+
if !should_filter_stderr_line(&request.language, &line) {
317+
let _ = app.emit(
318+
"code-output",
319+
serde_json::json!({
320+
"type": "stderr",
321+
"content": line,
322+
"language": request.language
323+
}),
324+
);
325+
}
307326
}
308327

309328
// 检查进程是否结束
@@ -323,14 +342,17 @@ pub async fn execute_code(
323342
}
324343
while let Ok(line) = stderr_rx.try_recv() {
325344
stderr_lines.push(line.clone());
326-
let _ = app.emit(
327-
"code-output",
328-
serde_json::json!({
329-
"type": "stderr",
330-
"content": line,
331-
"language": request.language
332-
}),
333-
);
345+
// 过滤掉特定语言的警告信息
346+
if !should_filter_stderr_line(&request.language, &line) {
347+
let _ = app.emit(
348+
"code-output",
349+
serde_json::json!({
350+
"type": "stderr",
351+
"content": line,
352+
"language": request.language
353+
}),
354+
);
355+
}
334356
}
335357

336358
let execution_time = start_time.elapsed().as_millis();

0 commit comments

Comments
 (0)