How can an OS process running in a ConsoleView displayed in a ToolWindow receive a Ctrl-C from the user?

I have successfully created an action in the main editor's right-click menu that opens a new tool window and displays the live output of a running process — in this simple example, the "cat" command. I have tested and confirmed that input typed into the window is sent to "cat" when I hit Enter (the confirmation is that "cat" echoes each line back).

My action, which is attached, follows the example attached to the last post at:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206797345-Creating-a-new-message-area-to-show-messages

But control C does not work. What am I missing in my code that would allow not just normal characters typed into the window to be received by the process, but also allow a Ctrl-C pressed while the window is selected to send a signal to the process? Thanks for any help!

0
1 comment
Avatar
Permanently deleted user

How do I add an attachment? I don't see a button for it. This text field is not honoring the indentation properly, but here's the Java as a code block:

package com.example.cat_plugin;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.filters.TextConsoleBuilder;
import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;

import java.util.ArrayList;
import java.util.List;

public class CatAction extends AnAction {
private static ConsoleView view = null;
private static ToolWindow window = null;

@Override
public void actionPerformed(AnActionEvent e) {
Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
if (project == null)
return;

String cwd = project.getBaseDir().getPath();
List<String> cmd = new ArrayList<>();
cmd.add("/bin/cat");

OSProcessHandler handler;
try {
handler = new OSProcessHandler(
new GeneralCommandLine(cmd).withWorkDirectory(cwd)
);
} catch(ExecutionException ex) {
ex.printStackTrace();
return;
}

if (view == null) {
TextConsoleBuilderFactory factory = TextConsoleBuilderFactory.getInstance();
TextConsoleBuilder builder = factory.createBuilder(project);
view = builder.getConsole();
}

view.attachToProcess(handler);
handler.startNotify();

if (window == null) {
ToolWindowManager manager = ToolWindowManager.getInstance(project);
window = manager.registerToolWindow("Cat console", true, ToolWindowAnchor.BOTTOM);
final ContentManager contentManager = window.getContentManager();
Content content = contentManager
.getFactory()
.createContent(view.getComponent(), "", false);
contentManager.addContent(content);
window.show(() -> {});
}
}
}
0

Please sign in to leave a comment.