Skip to content

Commit 40af31e

Browse files
committed
feat(terminal): enhance terminal execution states and update UI accordingly
- Add new execution states: READY, EXECUTING, SUCCESS, FAILED, TERMINATED - Update UI to reflect execution states with appropriate colors and messages - Refactor setResultStatus to handle new states - Add new color constants for running and warning states - Improve error handling and UI updates for different execution outcomes
1 parent 14c2fe7 commit 40af31e

File tree

4 files changed

+83
-20
lines changed

4 files changed

+83
-20
lines changed

core/src/main/kotlin/cc/unitmesh/devti/AutoDevColors.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ object AutoDevColors {
3939
val EXECUTION_SUCCESS_BACKGROUND = JBColor(Color(233, 255, 233), Color(0, 77, 0))
4040
val EXECUTION_ERROR_BACKGROUND = JBColor(Color(255, 233, 233), Color(77, 0, 0))
4141
val EXECUTION_SUCCESS_BORDER = JBColor(Color(0, 128, 0), Color(0, 100, 0))
42+
val EXECUTION_RUNNING_BORDER = JBColor(Color(0, 0, 255), Color(0, 0, 200))
43+
val EXECUTION_WARNING_BORDER = JBColor(Color(255, 165, 0), Color(200, 100, 0))
4244
val EXECUTION_ERROR_BORDER = JBColor(Color(128, 0, 0), Color(100, 0, 0))
4345

4446
// Loading panel colors

exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalExecuteAction.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import cc.unitmesh.devti.sketch.run.ProcessExecutor
77
import cc.unitmesh.devti.sketch.run.UIUpdatingWriter
88
import cc.unitmesh.devti.util.AutoDevCoroutineScope
99
import com.intellij.icons.AllIcons
10+
import com.intellij.openapi.actionSystem.ActionUpdateThread
1011
import com.intellij.openapi.actionSystem.AnAction
1112
import com.intellij.openapi.actionSystem.AnActionEvent
1213
import com.intellij.openapi.application.ApplicationManager
@@ -17,6 +18,7 @@ import org.jetbrains.ide.PooledThreadExecutor
1718
class TerminalExecuteAction(
1819
private val sketch: TerminalLangSketch
1920
) : AnAction("Execute", AutoDevBundle.message("sketch.terminal.execute"), AutoDevIcons.RUN) {
21+
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT
2022

2123
override fun update(e: AnActionEvent) {
2224
super.update(e)
@@ -42,7 +44,7 @@ class TerminalExecuteAction(
4244

4345
// 更新UI以反映停止状态
4446
sketch.resultSketch.updateViewText(sketch.lastExecutionResults + "\n\n[执行已手动停止]", true)
45-
sketch.setResultStatus(false, "执行已手动停止")
47+
sketch.setResultStatus(TerminalExecutionState.TERMINATED)
4648

4749
// 更新工具栏
4850
sketch.actionGroup.update(e)
@@ -80,7 +82,7 @@ class TerminalExecuteAction(
8082

8183
sketch.resultSketch.updateViewText("", true)
8284
stdWriter.setExecuting(true)
83-
sketch.setResultStatus(false)
85+
sketch.setResultStatus(TerminalExecutionState.EXECUTING)
8486

8587
sketch.currentExecutionJob = AutoDevCoroutineScope.Companion.scope(sketch.project).launch {
8688
val executor = sketch.project.getService(ProcessExecutor::class.java)
@@ -105,22 +107,25 @@ class TerminalExecuteAction(
105107
sketch.toolbar.updateActionsImmediately()
106108

107109
val success = exitCode == 0
108-
sketch.setResultStatus(success, if (!success) "Process exited with code $exitCode" else null)
110+
sketch.setResultStatus(
111+
if (success) TerminalExecutionState.SUCCESS else TerminalExecutionState.FAILED,
112+
if (!success) "进程退出码 $exitCode" else null
113+
)
109114
}
110115
} catch (ex: Exception) {
111-
AutoDevNotifications.notify(sketch.project, "Error executing command: ${ex.message}")
116+
AutoDevNotifications.notify(sketch.project, "执行命令时出错: ${ex.message}")
112117
ApplicationManager.getApplication().invokeLater {
113118
stdWriter.setExecuting(false)
114-
// Clear the running icon.
119+
// 清除运行图标
115120
sketch.titleLabel.icon = null
116121
sketch.isExecuting = false
117122

118123
// 更新工具栏以显示执行按钮
119124
sketch.actionGroup.update(e)
120125
sketch.toolbar.updateActionsImmediately()
121126

122-
sketch.resultSketch.updateViewText("${stdWriter.getContent()}\nError: ${ex.message}", true)
123-
sketch.setResultStatus(false, ex.message)
127+
sketch.resultSketch.updateViewText("${stdWriter.getContent()}\n错误: ${ex.message}", true)
128+
sketch.setResultStatus(TerminalExecutionState.FAILED, ex.message)
124129
}
125130
}
126131
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cc.unitmesh.terminal.sketch
2+
3+
/**
4+
* 终端执行命令的状态枚举
5+
*/
6+
enum class TerminalExecutionState {
7+
/**
8+
* 准备执行(初始状态)
9+
*/
10+
READY,
11+
12+
/**
13+
* 正在执行中
14+
*/
15+
EXECUTING,
16+
17+
/**
18+
* 执行成功
19+
*/
20+
SUCCESS,
21+
22+
/**
23+
* 执行失败
24+
*/
25+
FAILED,
26+
27+
/**
28+
* 执行被手动终止
29+
*/
30+
TERMINATED
31+
}

exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalSketchProvider.kt

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
6161
this.component.border = JBUI.Borders.empty()
6262
}
6363

64-
// 将内部变量公开,以便 TerminalExecuteAction 可以访问
6564
var lastExecutionResults: String = ""
6665
var hasExecutionResults: Boolean = false
6766

68-
// 将这些变量从私有变成公开
6967
var isExecuting = false
7068
var currentExecutionJob: Job? = null
7169

@@ -105,6 +103,8 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
105103
private var resizableTerminalPanel: ResizableTerminalPanel
106104
private var isTerminalPanelVisible = false
107105

106+
var currentExecutionState: TerminalExecutionState = TerminalExecutionState.READY
107+
108108
init {
109109
val projectDir = project.guessProjectDir()?.path
110110

@@ -137,27 +137,52 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
137137
terminalWidget!!.addMessageFilter(FrontendWebViewServerFilter(project, mainPanel!!))
138138
}
139139

140-
// 将 setResultStatus 从 private 改为 public
141-
fun setResultStatus(success: Boolean, errorMessage: String? = null) {
140+
/**
141+
* 设置终端执行结果的状态
142+
*
143+
* @param state 执行状态
144+
* @param message 附加消息(如错误信息)
145+
*/
146+
fun setResultStatus(state: TerminalExecutionState, message: String? = null) {
142147
ApplicationManager.getApplication().invokeLater {
143-
when {
144-
success -> {
148+
when (state) {
149+
TerminalExecutionState.READY -> {
150+
resultPanel.background = UIUtil.getPanelBackground()
151+
resultPanel.border = JBUI.Borders.emptyTop(1)
152+
collapsibleResultPanel.setTitle("准备执行")
153+
}
154+
155+
TerminalExecutionState.EXECUTING -> {
156+
resultPanel.background = UIUtil.getPanelBackground()
157+
resultPanel.border = LineBorder(AutoDevColors.EXECUTION_RUNNING_BORDER, 1)
158+
collapsibleResultPanel.setTitle("⏳ 正在执行...")
159+
}
160+
161+
TerminalExecutionState.SUCCESS -> {
145162
resultPanel.background = AutoDevColors.EXECUTION_SUCCESS_BACKGROUND
146163
resultPanel.border = LineBorder(AutoDevColors.EXECUTION_SUCCESS_BORDER, 1)
147-
collapsibleResultPanel.setTitle("Execution Successful")
164+
collapsibleResultPanel.setTitle("执行成功")
148165
}
149-
150-
else -> {
166+
167+
TerminalExecutionState.FAILED -> {
151168
resultPanel.background = AutoDevColors.EXECUTION_ERROR_BACKGROUND
152169
resultPanel.border = LineBorder(AutoDevColors.EXECUTION_ERROR_BORDER, 1)
153-
val errorText = errorMessage?.let { ": $it" } ?: ""
154-
collapsibleResultPanel.setTitle("❌ Execution Failed$errorText")
170+
val errorText = message?.let { ": $it" } ?: ""
171+
collapsibleResultPanel.setTitle("❌ 执行失败$errorText")
172+
}
173+
174+
TerminalExecutionState.TERMINATED -> {
175+
resultPanel.background = UIUtil.getPanelBackground()
176+
resultPanel.border = LineBorder(AutoDevColors.EXECUTION_WARNING_BORDER, 1)
177+
collapsibleResultPanel.setTitle("⚠️ 执行已终止")
155178
}
156179
}
180+
181+
currentExecutionState = state
157182
resultPanel.repaint()
158183
}
159184
}
160-
185+
161186
private fun toggleTerminalAction() {
162187
resizableTerminalPanel.isVisible = !resizableTerminalPanel.isVisible
163188
resizableTerminalPanel.revalidate()
@@ -289,7 +314,7 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
289314
"⚠️ WARNING: $reason\nThe command was not auto-executed for safety reasons.\nPlease review and run manually if you're sure.",
290315
true
291316
)
292-
setResultStatus(false, reason)
317+
setResultStatus(TerminalExecutionState.TERMINATED, reason)
293318
collapsibleResultPanel.expand()
294319
}
295320
return

0 commit comments

Comments
 (0)