IntelliJ 插件:获取操作系统命令的输出

问题描述 投票:0回答:2

我正在尝试构建一个 IntelliJ 插件来执行操作系统命令,解析输出,然后创建解析输出的菜单。

但是,我坚持如何从

OSProcessHandler
获取输出并将其解析为字符串,然后再将其显示为视图的一部分。

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

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
        if (project == null)
            return;
        GeneralCommandLine generalCommandLine = new GeneralCommandLine(getAccounts());
        generalCommandLine.setCharset(Charset.forName("UTF-8"));
        //generalCommandLine.setWorkDirectory(project.getBasePath());

        ProcessHandler processHandler = null;
        try {
            processHandler = new OSProcessHandler(generalCommandLine);
        } catch (ExecutionException ex) {
            throw new RuntimeException(ex);
        }
        if (view == null) {
            TextConsoleBuilderFactory factory = TextConsoleBuilderFactory.getInstance();
            TextConsoleBuilder builder = factory.createBuilder(project);
            view = builder.getConsole();
        }
        view.attachToProcess(processHandler);
        processHandler.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(() -> {});

    }}

    private ArrayList<String> getAccounts(){
        ArrayList<String> cmds = new ArrayList<>();
        cmds.add("/usr/local/bin/getaccountscmd");
        cmds.add("ls");
        System.out.println(cmds.toString());
        return cmds;
    }

}
java intellij-idea jetbrains-ide intellij-plugin
2个回答
0
投票

您可以尝试添加进程监听器:

        processHandler.addProcessListener(new ProcessAdapter() {
            @Override
            public void onTextAvailable(@org.jetbrains.annotations.NotNull ProcessEvent event, @org.jetbrains.annotations.NotNull Key outputType) {
                // grab your output from the event here
            }
        });

© www.soinside.com 2019 - 2024. All rights reserved.