Skip to content

Commit ea73590

Browse files
committed
feat (python): 拆分为 python2 和 python3
1 parent 2e6c9ed commit ea73590

7 files changed

Lines changed: 76 additions & 15 deletions

File tree

src-tauri/src/plugins/manager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{python::PythonPlugin, LanguagePlugin};
1+
use super::{python2::Python2Plugin, python3::Python3Plugin, LanguagePlugin};
22
use std::collections::HashMap;
33

44
pub struct PluginManager {
@@ -9,7 +9,8 @@ impl PluginManager {
99
pub fn new() -> Self {
1010
let mut plugins: HashMap<String, Box<dyn LanguagePlugin>> = HashMap::new();
1111

12-
plugins.insert("python".to_string(), Box::new(PythonPlugin));
12+
plugins.insert("python2".to_string(), Box::new(Python2Plugin));
13+
plugins.insert("python3".to_string(), Box::new(Python3Plugin));
1314

1415
Self { plugins }
1516
}

src-tauri/src/plugins/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub trait LanguagePlugin: Send + Sync {
4646

4747
// 重新导出子模块
4848
pub mod manager;
49-
pub mod python;
49+
pub mod python2;
50+
pub mod python3;
5051

5152
pub use manager::PluginManager;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use super::{ExecutionResult, LanguagePlugin};
22

3-
pub struct PythonPlugin;
3+
pub struct Python2Plugin;
44

5-
impl LanguagePlugin for PythonPlugin {
5+
impl LanguagePlugin for Python2Plugin {
66
fn get_language_name(&self) -> &'static str {
7-
"Python"
7+
"Python 2"
88
}
99

1010
fn get_file_extension(&self) -> &'static str {
1111
"py"
1212
}
1313

1414
fn get_commands(&self) -> Vec<&'static str> {
15-
vec!["python", "python2", "python3"]
15+
vec!["python", "python2"]
1616
}
1717

1818
fn get_version_args(&self) -> Vec<&'static str> {

src-tauri/src/plugins/python3.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use super::{ExecutionResult, LanguagePlugin};
2+
3+
pub struct Python3Plugin;
4+
5+
impl LanguagePlugin for Python3Plugin {
6+
fn get_language_name(&self) -> &'static str {
7+
"Python 3"
8+
}
9+
10+
fn get_file_extension(&self) -> &'static str {
11+
"py"
12+
}
13+
14+
fn get_commands(&self) -> Vec<&'static str> {
15+
vec!["python", "python3"]
16+
}
17+
18+
fn get_version_args(&self) -> Vec<&'static str> {
19+
vec!["--version"]
20+
}
21+
22+
fn get_execute_args(&self, file_path: &str) -> Vec<String> {
23+
vec![file_path.to_string()]
24+
}
25+
26+
fn get_path_command(&self) -> String {
27+
"import sys; print(sys.executable)".to_string()
28+
}
29+
30+
fn pre_execute_hook(&self, code: &str) -> Result<String, String> {
31+
// 添加一些 Python 特定的预处理
32+
let processed_code = format!(
33+
"# CodeForge Python Execution\n# Generated at: {}\n\n{}",
34+
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC"),
35+
code
36+
);
37+
Ok(processed_code)
38+
}
39+
40+
fn post_execute_hook(&self, result: &mut ExecutionResult) -> Result<(), String> {
41+
// Python 特定的后处理
42+
if result.success && result.stdout.is_empty() && result.stderr.is_empty() {
43+
result.stdout = "Code executed successfully (no output)".to_string();
44+
}
45+
46+
// 清理 Python 特定的错误信息
47+
if !result.stderr.is_empty() {
48+
result.stderr = result
49+
.stderr
50+
.replace("Traceback (most recent call last):", "Error:");
51+
}
52+
53+
Ok(())
54+
}
55+
}

src/App.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ const isSuccess = ref(false)
110110
const lastExecutionTime = ref(0)
111111
const activeTab = ref('output')
112112
const showSettings = ref(false)
113-
const executionHistory = ref<ExecutionResult[]>([])
114113
115114
const envInfo = ref<EnvInfo>({
116115
installed: false,
@@ -135,7 +134,7 @@ const showToast = (message: string, type: 'success' | 'error' | 'info' = 'succes
135134
const refreshEnvInfo = async () => {
136135
try {
137136
const info: LanguageInfo = await invoke('get_info', {
138-
language: 'python'
137+
language: 'python2'
139138
})
140139
141140
envInfo.value = {
@@ -151,7 +150,7 @@ const refreshEnvInfo = async () => {
151150
installed: false,
152151
version: 'Error',
153152
path: 'Error',
154-
language: 'python'
153+
language: 'python2'
155154
}
156155
}
157156
}
@@ -169,7 +168,7 @@ const runCode = async () => {
169168
const result: ExecutionResult = await invoke('execute_code', {
170169
request: {
171170
code: code.value,
172-
language: 'python'
171+
language: 'python2'
173172
}
174173
})
175174
@@ -199,7 +198,7 @@ const runCode = async () => {
199198
200199
const clearOutput = () => {
201200
output.value = ''
202-
showToast('Output cleared', 'info')
201+
showToast('输出已清空', 'info')
203202
}
204203
205204
onMounted(async () => {

src/components/AppHeader.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
<div class="flex items-center space-x-3">
1414
<button @click="$emit('run-code')"
1515
:disabled="isRunning || !envInstalled"
16-
:class="['flex items-center space-x-2 px-3 py-1.5 rounded-md font-medium transition-all duration-200 cursor-pointer',
16+
:class="['flex items-center space-x-2 px-3 py-1.5 rounded-md font-medium transition-all duration-200',
1717
isRunning || !envInstalled
1818
? 'bg-gray-300 text-gray-500 cursor-not-allowed'
19-
: 'btn-success shadow-sm hover:shadow-md'
19+
: 'btn-success shadow-sm hover:shadow-md cursor-pointer'
2020
]">
2121
<component :is="isRunning ? Square : Play" class="w-3 h-3"/>
2222
<span>{{ isRunning ? '运行中...' : '运行代码' }}</span>
2323
</button>
2424

25-
<button @click="$emit('clear-output')" class="btn-danger px-3 py-2.5 rounded-md font-medium transition-all duration-200 cursor-pointer">
25+
<button class="btn-danger px-3 py-2.5 rounded-md font-medium transition-all duration-200"
26+
:disabled="isRunning || !envInstalled"
27+
:class="[isRunning || !envInstalled ? 'cursor-not-allowed' : 'cursor-pointer']"
28+
@click="$emit('clear-output')">
2629
<Trash2 class="w-4 h-4"/>
2730
</button>
2831
</div>

src/components/Toast.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ const props = withDefaults(defineProps<Props>(), {
7575
const emit = defineEmits<Emits>()
7676
7777
const progressWidth = ref(100)
78+
// @ts-ignore
7879
let progressTimer: NodeJS.Timeout | null = null
80+
// @ts-ignore
7981
let autoCloseTimer: NodeJS.Timeout | null = null
8082
8183
// 图标组件映射

0 commit comments

Comments
 (0)