22 <div class =" h-screen flex flex-col bg-gray-50" >
33 <AppHeader :is-running =" isRunning"
44 :env-installed =" envInfo.installed"
5+ :supported-languages =" supportedLanguages"
6+ :current-language =" currentLanguage"
57 @run-code =" runCode"
68 @clear-output =" clearOutput"
9+ @language-change =" handleLanguageChange"
710 @show-settings =" showSettings = true" >
811 </AppHeader >
912
1013 <div class =" flex-1 flex overflow-hidden" >
1114 <!-- 代码编辑器 -->
1215 <div class =" flex-1 flex flex-col" >
1316 <div class =" bg-gray-100 px-4 py-2 border-b border-gray-200 flex items-center justify-between" >
14- <h2 class =" text-sm font-medium text-gray-700" >Python 代码编辑器</h2 >
17+ <h2 class =" text-sm font-medium text-gray-700" >{{ getLanguageDisplayName(currentLanguage) }} 代码编辑器</h2 >
1518 <div class =" text-xs text-gray-500" >
1619 <strong >{{ code.length }}</strong > 字符, <strong >{{ code.split('\n').length }}</strong > 行
1720 </div >
1821 </div >
19- <CodeEditor v-model =" code"
20- language =" python"
21- class =" flex-1" >
22- </CodeEditor >
22+ <CodeEditor v-model =" code" class =" flex-1" :language =" currentLanguage" />
2323 </div >
2424
2525 <!-- 输出 -->
3535 </div >
3636
3737 <!-- 状态栏 -->
38- <StatusBar :env-info =" envInfo"
39- :execution-time =" lastExecutionTime"
40- :code-length =" code.length" >
41- </StatusBar >
38+ <StatusBar :env-info =" envInfo" :execution-time =" lastExecutionTime" :code-length =" code.length" />
4239
4340 <!-- 通知信息 -->
4441 <Toast v-if =" toast.show"
@@ -87,7 +84,15 @@ interface EnvInfo
8784 language: string
8885}
8986
90- const code = ref (` # Welcome to CodeForge!
87+ interface Language
88+ {
89+ name: string
90+ value: string
91+ }
92+
93+ // 代码模板
94+ const codeTemplates: Record <string , string > = {
95+ python: ` # Welcome to CodeForge!
9196# Write your Python code here and click Run to execute
9297
9398print("Hello, CodeForge!")
@@ -102,14 +107,52 @@ print(f"The result of {x} + {y} = {result}")
102107numbers = [1, 2, 3, 4, 5]
103108squared = [n**2 for n in numbers]
104109print(f"Original: {numbers}")
105- print(f"Squared: {squared}") ` )
110+ print(f"Squared: {squared}") ` ,
111+
112+ python2: ` # Welcome to CodeForge - Python 2!
113+ # Write your Python 2 code here and click Run to execute
114+
115+ print "Hello, CodeForge from Python 2!"
116+
117+ # Example: Simple calculation
118+ x = 10
119+ y = 20
120+ result = x + y
121+ print "The result of %d + %d = %d" % (x, y, result)
122+
123+ # Example: List operations
124+ numbers = [1, 2, 3, 4, 5]
125+ squared = [n**2 for n in numbers]
126+ print "Original:", numbers
127+ print "Squared:", squared ` ,
128+
129+ python3: ` # Welcome to CodeForge - Python 3!
130+ # Write your Python 3 code here and click Run to execute
106131
132+ print("Hello, CodeForge from Python 3!")
133+
134+ # Example: Simple calculation
135+ x = 10
136+ y = 20
137+ result = x + y
138+ print(f"The result of {x} + {y} = {result}")
139+
140+ # Example: List operations
141+ numbers = [1, 2, 3, 4, 5]
142+ squared = [n**2 for n in numbers]
143+ print(f"Original: {numbers}")
144+ print(f"Squared: {squared}") `
145+ }
146+
147+ const code = ref (' ' )
148+ const currentLanguage = ref (' python' )
107149const output = ref (' ' )
108150const isRunning = ref (false )
109151const isSuccess = ref (false )
110152const lastExecutionTime = ref (0 )
111153const activeTab = ref (' output' )
112154const showSettings = ref (false )
155+ const supportedLanguages = ref <Language []>([])
113156
114157const envInfo = ref <EnvInfo >({
115158 installed: false ,
@@ -126,15 +169,17 @@ const toast = ref({
126169
127170const showToast = (message : string , type : ' success' | ' error' | ' info' = ' success' ) => {
128171 toast .value = { show: true , message , type }
129- setTimeout (() => {
130- toast .value .show = false
131- }, 3000 )
172+ }
173+
174+ const getLanguageDisplayName = (languageValue : string ) => {
175+ const language = supportedLanguages .value .find (lang => lang .value === languageValue )
176+ return language ? language .name : languageValue
132177}
133178
134179const refreshEnvInfo = async () => {
135180 try {
136181 const info: LanguageInfo = await invoke (' get_info' , {
137- language: ' python2 '
182+ language: currentLanguage . value
138183 })
139184
140185 envInfo .value = {
@@ -150,11 +195,48 @@ const refreshEnvInfo = async () => {
150195 installed: false ,
151196 version: ' Error' ,
152197 path: ' Error' ,
153- language: ' python2 '
198+ language: currentLanguage . value
154199 }
155200 }
156201}
157202
203+ const getSupportedLanguages = async () => {
204+ try {
205+ const languages = await invoke <Language []>(' get_supported_languages' )
206+ supportedLanguages .value = languages .map ((language ) => ({
207+ name: language .name ,
208+ value: language .value
209+ }))
210+
211+ // 设置默认语言
212+ if (supportedLanguages .value .length > 0 && ! currentLanguage .value ) {
213+ currentLanguage .value = supportedLanguages .value [0 ].value
214+ }
215+ }
216+ catch (error ) {
217+ console .error (' Error getting supported languages:' , error )
218+ supportedLanguages .value = []
219+ }
220+ }
221+
222+ const handleLanguageChange = async (newLanguage : string ) => {
223+ currentLanguage .value = newLanguage
224+
225+ // 更新代码模板
226+ code .value = codeTemplates [newLanguage ] || ` # ${ getLanguageDisplayName (newLanguage ) } Code
227+ # Write your code here...
228+
229+ print("Hello from ${ getLanguageDisplayName (newLanguage ) }!") `
230+
231+ // 清空输出
232+ output .value = ' '
233+
234+ // 刷新环境信息
235+ await refreshEnvInfo ()
236+
237+ showToast (` 已切换到 ${ getLanguageDisplayName (newLanguage ) } ` , ' info' )
238+ }
239+
158240const runCode = async () => {
159241 if (! envInfo .value .installed ) {
160242 showToast (` ${ envInfo .value .language } 环境未安装 ` , ' error' )
@@ -168,7 +250,7 @@ const runCode = async () => {
168250 const result: ExecutionResult = await invoke (' execute_code' , {
169251 request: {
170252 code: code .value ,
171- language: ' python2 '
253+ language: currentLanguage . value
172254 }
173255 })
174256
@@ -202,6 +284,17 @@ const clearOutput = () => {
202284}
203285
204286onMounted (async () => {
287+ await getSupportedLanguages ()
288+
289+ // 设置初始代码模板
290+ if (supportedLanguages .value .length > 0 ) {
291+ currentLanguage .value = supportedLanguages .value [0 ].value
292+ code .value = codeTemplates [currentLanguage .value ] || codeTemplates .python
293+ }
294+ else {
295+ code .value = codeTemplates .python
296+ }
297+
205298 await refreshEnvInfo ()
206299})
207300 </script >
0 commit comments