terminal: extract CommandFinishedEvent

GitOrigin-RevId: 4b38203500d9037de569561668a0b91f58aa9c5d
This commit is contained in:
Sergey Simonchik
2024-02-16 19:07:52 +01:00
committed by intellij-monorepo-bot
parent dd88db22db
commit d3bfb7956f
7 changed files with 20 additions and 19 deletions

View File

@@ -69,8 +69,8 @@ class BlockTerminalController(
finishCommandBlock(exitCode = 0)
}
override fun commandFinished(command: String?, exitCode: Int, duration: Long?) {
finishCommandBlock(exitCode)
override fun commandFinished(event: CommandFinishedEvent) {
finishCommandBlock(event.exitCode)
}
private fun finishCommandBlock(exitCode: Int) {

View File

@@ -142,7 +142,7 @@ class BlockTerminalView(
}
})
session.commandManager.addListener(object: ShellCommandListener {
override fun commandFinished(command: String?, exitCode: Int, duration: Long?) {
override fun commandFinished(event: CommandFinishedEvent) {
SaveAndSyncHandler.getInstance().scheduleRefresh()
}
}, this)

View File

@@ -31,7 +31,7 @@ internal class ShellCommandExecutionManager(private val session: BlockTerminalSe
init {
commandManager.addListener(object : ShellCommandListener {
override fun commandFinished(command: String?, exitCode: Int, duration: Long?) {
override fun commandFinished(event: CommandFinishedEvent) {
lock.withLock { withoutLock ->
if (!isCommandRunning) {
LOG.warn("Received command_finished event, but command wasn't started")

View File

@@ -10,7 +10,9 @@ import org.jetbrains.annotations.NonNls
import org.jetbrains.plugins.terminal.TerminalUtil
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.TimeUnit
import kotlin.time.Duration
import kotlin.time.TimeMark
import kotlin.time.TimeSource
class ShellCommandManager(private val session: BlockTerminalSession) {
private val listeners: CopyOnWriteArrayList<ShellCommandListener> = CopyOnWriteArrayList()
@@ -55,7 +57,7 @@ class ShellCommandManager(private val session: BlockTerminalSession) {
private fun processCommandStartedEvent(event: List<String>) {
val command = Param.COMMAND.getDecodedValue(event.getOrNull(1))
val currentDirectory = Param.CURRENT_DIRECTORY.getDecodedValue(event.getOrNull(2))
val startedCommand = StartedCommand(System.nanoTime(), currentDirectory, command)
val startedCommand = StartedCommand(command, currentDirectory, TimeSource.Monotonic.markNow())
this.startedCommand = startedCommand
fireCommandStarted(startedCommand)
}
@@ -150,10 +152,10 @@ class ShellCommandManager(private val session: BlockTerminalSession) {
LOG.info("Shell event: received command_finished without preceding command_started - skipping")
}
else {
debug { "Shell event: command_finished - $startedCommand, exit code: $exitCode" }
val event = CommandFinishedEvent(startedCommand.command, exitCode, startedCommand.commandStarted.elapsedNow())
debug { "Shell event: command_finished - $event" }
for (listener in listeners) {
val duration = startedCommand.commandStartedNano.let { TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - it) }
listener.commandFinished(startedCommand.command, exitCode, duration)
listener.commandFinished(event)
}
}
clearTerminal()
@@ -260,7 +262,7 @@ interface ShellCommandListener {
fun commandStarted(command: String) {}
/** Fired after command is executed and before prompt is printed */
fun commandFinished(command: String?, exitCode: Int, duration: Long?) {}
fun commandFinished(event: CommandFinishedEvent) {}
fun promptStateUpdated(newState: TerminalPromptState) {}
@@ -271,8 +273,6 @@ interface ShellCommandListener {
fun clearInvoked() {}
}
private class StartedCommand(val commandStartedNano: Long, val currentDirectory: String, val command: String) {
override fun toString(): String {
return "command: $command, currentDirectory: $currentDirectory"
}
}
data class CommandFinishedEvent(val command: String, val exitCode: Int, val duration: Duration)
private data class StartedCommand(val command: String, val currentDirectory: String, val commandStarted: TimeMark)

View File

@@ -52,7 +52,7 @@ class TerminalOutputController(
val disposable = Disposer.newDisposable()
// clear all blocks when command is finished and then remove listener
session.addCommandListener(object : ShellCommandListener {
override fun commandFinished(command: String?, exitCode: Int, duration: Long?) {
override fun commandFinished(event: CommandFinishedEvent) {
invokeLater {
outputModel.clearBlocks()
}

View File

@@ -11,6 +11,7 @@ import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.terminal.completion.ShellEnvironment
import com.intellij.terminal.completion.ShellRuntimeDataProvider
import org.jetbrains.plugins.terminal.exp.BlockTerminalSession
import org.jetbrains.plugins.terminal.exp.CommandFinishedEvent
import org.jetbrains.plugins.terminal.exp.ShellCommandListener
import java.time.Duration
@@ -33,7 +34,7 @@ class IJShellRuntimeDataProvider(
// For example, the current directory can change and the file cache is no more valid.
// Or some aliases added and the cached shell env is no more valid.
session.addCommandListener(object : ShellCommandListener {
override fun commandFinished(command: String?, exitCode: Int, duration: Long?) {
override fun commandFinished(event: CommandFinishedEvent) {
clearCaches()
}
})

View File

@@ -101,10 +101,10 @@ object TerminalSessionTestUtil {
}, disposable)
val result: CompletableFuture<CommandResult> = CompletableFuture()
session.commandManager.addListener(object : ShellCommandListener {
override fun commandFinished(command: String?, exitCode: Int, duration: Long?) {
override fun commandFinished(event: CommandFinishedEvent) {
val (text, commandEndMarkerFound) = scraper.scrapeOutput()
Assert.assertEquals(session.commandBlockIntegration.commandEndMarker != null, commandEndMarkerFound)
result.complete(CommandResult(exitCode, text))
result.complete(CommandResult(event.exitCode, text))
Disposer.dispose(disposable)
}
}, disposable)