それで、ようやくコンソールを完成させることができたので、自分で答えようと思いました。これはまだ機能するプロトタイプですが、何かを追加し続けると、コードをどんどんクリーンアップできると思います。私の現在の目的では、これがどのように機能したかです。
短いバージョンが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
クラスには、ユーザーが入力したすべてのコマンドを保存するための二重にリンクされた文字列リストがありました。作成する非常に単純なクラス。
ProcessConsole
Eclipse のクラス (および)を見れば、IOConsoleViewer
実際にはすべて一目瞭然です。かなりの量があるため、ここには多くのコードを入れていません。しかし、私が始めたときは完全に迷っていたので、これが何らかの方向性を示してくれることを願っています.
ただし、質問に答えて、誰かが尋ねた場合はより具体的なコードを追加して喜んでいます。