所以我想我会自己回答这个问题,因为我终于能够完成控制台。它仍然是一个工作原型,但我想随着你不断添加东西,你可以越来越多地清理代码。就我目前的目的而言,这就是它的工作方式。
如果您想要简短版本,那么我基本上模仿了ProcessConsole
Eclipse 提供的版本,因为这正是我所需要的:我可以在其中连接进程的控制台,但由于它ProcessConsole
是内部的,我喜欢避免扩展这些类。
以下是我用来实现与控制台交互的类的概述。我不会给出关于在哪里MyConsole
创建的借口。基本上,DebugUITools.getConsole(myProcess)
我没有使用 ,而是使用了自己的myProcess.getConsole()
方法。MyProcess
延伸RuntimeProcess
。
class MyConsole extends IOConsole {
private IOConsoleInputStream fInput;
private IOConsoleOutputStream fOutput;
private IStreamsProxy fStreamsProxy;
private ConsoleHistory history;
//This is to remember the caret position after the prompt
private int caretAtPrompt;
/* in the console so when you need to replace the command on up and down
* arrow keys you have the position.
* I just did a caretAtPrompt += String.Length wherever string was
* appended to the console. Mainly in the streamlistener and
* InputJob unless you specifically output something to the output
* stream.
*/
//In the constructor you assign all the above fields. Below are some
//to point out.
//fInput = getInputStream();
// fStreamsProxy = process.getStreamsProxy();
// fOutput = newOutputStream();
//We must override the following method to get access to the caret
@Override
public IPageBookViewPage createPage(IConsoleView view) {
return new MyConsolePage(this, view);
}
//After this I followed the ProcessConsole and added the
//InputJob and StreamListener
//defined in there.
}
class MyConsolePage extends TextConsolePage {
//Not much in this class, just override the createViewer
// to return MyConsoleViewer
}
class MyConsoleViewer extends TextConsoleViewer {
//This is the most important class and most of the work is done here
//Again I basically copied everything from IOConsoleViewer and then
//updated whatever I needed
//I added a VerifyKeyListener for the up and down arrow
//keys for the console history
MyConsoleViewer (Composite parent, MyConsole console) {
//I have omitted a lot of code as it was too much to put up,
//just highlighted a few
getTextWidget().addVerifyKeyListener(new MyKeyChecker());
}
class MyKeyChecker implements VerifyKeyListener {...}
}
这是 的代码ProcessConsole
。
这是 的代码IOConsoleViewer
。
我创建的ConsoleHistory
类只有一个双向链接的字符串列表来保存用户输入的所有命令。创建一个非常简单的类。
一旦您查看了 Eclipse 类 (ProcessConsole
和IOConsoleViewer
),它实际上都是不言自明的。我在这里没有放太多代码,因为有很多。但希望这能给我一些方向,因为当我开始时我完全迷失了。
我很乐意回答问题,如果有人问,我会添加更具体的代码。