Terminal listener declared in plugin.xml not working

Answered

I am trying to listen to events in the terminal, I tried to listen to TerminalWidgetListener (also tried JBTerminalWidgetListener) it does not work in either applicationListner nor projectListener, I want to get access to the terminalWidget object upon tab closed/project, how can I achieve it? 

4 comments
Comment actions Permalink

Nope, TerminalWidgetListener doesn't work via in applicationListener (and it cannot work in projectListener, because JBTerminalWidget doesn't depend on a project). Could you please clarify your use case?

Currently, to achieve this goal, you can use the following code (it depends on Terminal plugin):

ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID);
if (window != null) {
window.getContentManager().addContentManagerListener(new ContentManagerListener() {
@Override
public void contentAdded(@NotNull ContentManagerEvent event) {
JBTerminalWidget widget = TerminalView.getWidgetByContent(event.getContent());
if (widget != null) {
widget.addListener(new TerminalWidgetListener() {
@Override
public void allSessionsClosed(TerminalWidget widget) {
// process terminal process terminated
}
});
}
}
});
}
0
Comment actions Permalink

I understand.

my use case is that I want to get the typed command, it does not necessary has to be when the project is closed, it can also be after the 'enter' pressed and the command is running.

I though of taking it from 
jediTermWidget.getTerminalTextBuffer().getHistoryBuffer()
and assumed the default terminal is used
I can take it with considering the actual terminal in use, is there a common api I can get the history commands from?

0
Comment actions Permalink

The typed shell command is provided by org.jetbrains.plugins.terminal.ShellTerminalWidget#getTypedShellCommand. Unfortunately, it's not 100% correct, because it's guessed from current terminal text. However, it's correct pretty often.

Also related: com.intellij.terminal.TerminalShellCommandHandler (`com.intellij.terminal.shellCommandHandler` extension point). It allows to implement smart command execution (https://blog.jetbrains.com/idea/2020/07/run-ide-features-from-the-terminal/). If you don't need to implement smart command execution, you can still implement com.intellij.terminal.TerminalShellCommandHandler#matches (and return `false`) to be notified about changes in typed commands.

jediTermWidget.getTerminalTextBuffer().getHistoryBuffer() is a bit different - it contains raw text that has been pushed out of the visible area of the terminal - it won't provide history of typed commands.

0
Comment actions Permalink

great, I will try that.

Thank you very much.

0

Please sign in to leave a comment.