Skip to content

Commit 985099a

Browse files
committed
feat(run): add ConsoleService for centralized console output management #379
Introduce ConsoleService to handle console output across components. It provides methods to set/get active console and print messages with proper content types. This improves logging consistency and simplifies console access in ThreadProcessor and DevInsRunConfigurationProfileState.
1 parent 46b4c10 commit 985099a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/processor/ThreadProcessor.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import cc.unitmesh.devti.language.processor.shell.ShireShellCommandRunner
88
import cc.unitmesh.devti.language.provider.http.HttpHandler
99
import cc.unitmesh.devti.language.provider.http.HttpHandlerType
1010
import cc.unitmesh.devti.language.psi.DevInFile
11+
import cc.unitmesh.devti.language.service.ConsoleService
1112
import cc.unitmesh.devti.language.utils.lookupFile
1213
import cc.unitmesh.devti.provider.RunService
1314
import cc.unitmesh.devti.util.readText
15+
import com.intellij.execution.ui.ConsoleViewContentType
1416
import com.intellij.openapi.application.ApplicationManager
1517
import com.intellij.openapi.application.ReadAction
1618
import com.intellij.openapi.project.Project
@@ -27,6 +29,9 @@ object ThreadProcessor : PatternProcessor {
2729
myProject: Project, fileName: String, variablesName: Array<String>, variableTable: MutableMap<String, Any?>,
2830
): String {
2931
val file = myProject.lookupFile(fileName) ?: return "File not found: $fileName"
32+
33+
val consoleService = ConsoleService.getInstance(myProject)
34+
consoleService.print("Executing thread for file: $fileName\n", ConsoleViewContentType.NORMAL_OUTPUT)
3035

3136
val filename = file.name.lowercase()
3237
val content = file.readText()
@@ -37,6 +42,7 @@ object ThreadProcessor : PatternProcessor {
3742
?.execute(myProject, content, variablesName, variableTable)
3843

3944
if (execute != null) {
45+
consoleService.print("cURL execution completed\n", ConsoleViewContentType.NORMAL_OUTPUT)
4046
return execute
4147
}
4248
}
@@ -45,7 +51,8 @@ object ThreadProcessor : PatternProcessor {
4551
PsiManager.getInstance(myProject).findFile(file)
4652
} ?: return "Failed to find PSI file for $fileName"
4753

48-
// console.print("Prepare for running ${configuration.name}...\n", ConsoleViewContentType.NORMAL_OUTPUT)
54+
consoleService.print("Running $fileName...\n", ConsoleViewContentType.NORMAL_OUTPUT)
55+
4956
when (psiFile) {
5057
is DevInFile -> {
5158
return when (val output = variableTable["output"]) {
@@ -55,6 +62,7 @@ object ThreadProcessor : PatternProcessor {
5562
variableTable["output"] = it
5663
executeTask(myProject, variablesName, variableTable, psiFile)
5764
} catch (e: Exception) {
65+
consoleService.print("Error: ${e.message}\n", ConsoleViewContentType.ERROR_OUTPUT)
5866
null
5967
}
6068
}

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/run/DevInsRunConfigurationProfileState.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import cc.unitmesh.devti.language.run.flow.DevInsConversationService
1212
import cc.unitmesh.devti.language.run.runner.ShireConsoleView
1313
import cc.unitmesh.devti.language.run.runner.ShireExecutionConsole
1414
import cc.unitmesh.devti.language.run.runner.ShireRunner
15+
import cc.unitmesh.devti.language.service.ConsoleService
1516
import cc.unitmesh.devti.llms.LLMProvider
1617
import cc.unitmesh.devti.llms.LlmFactory
1718
import cc.unitmesh.devti.util.AutoDevCoroutineScope
@@ -63,6 +64,8 @@ open class DevInsRunConfigurationProfileState(
6364
processHandler.addProcessListener(processAdapter)
6465

6566
console.attachToProcess(processHandler)
67+
68+
ConsoleService.getInstance(myProject).setActiveConsole(executionConsole)
6669

6770
val shireFile: DevInFile? = DevInFile.lookup(myProject, configuration.getScriptPath())
6871
if (shireFile == null) {
@@ -296,4 +299,3 @@ open class DevInsRunConfigurationProfileState(
296299
}
297300
}
298301
}
299-
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cc.unitmesh.devti.language.service
2+
3+
import cc.unitmesh.devti.language.run.runner.ShireExecutionConsole
4+
import com.intellij.execution.ui.ConsoleViewContentType
5+
import com.intellij.openapi.components.Service
6+
import com.intellij.openapi.components.service
7+
import com.intellij.openapi.project.Project
8+
9+
@Service(Service.Level.PROJECT)
10+
class ConsoleService(private val project: Project) {
11+
private var activeConsole: ShireExecutionConsole? = null
12+
13+
fun setActiveConsole(console: ShireExecutionConsole) {
14+
activeConsole = console
15+
}
16+
17+
fun getActiveConsole(): ShireExecutionConsole? = activeConsole
18+
19+
fun print(text: String, contentType: ConsoleViewContentType = ConsoleViewContentType.NORMAL_OUTPUT) {
20+
activeConsole?.print(text, contentType)
21+
}
22+
23+
companion object {
24+
@JvmStatic
25+
fun getInstance(project: Project): ConsoleService = project.service<ConsoleService>()
26+
}
27+
}

0 commit comments

Comments
 (0)