Skip to content

Commit

Permalink
复用控制台
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Dec 12, 2024
1 parent f0c6ce7 commit b337d3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/console/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ export class Client extends vscode.Disposable {
}
}

static terminalHistory: { [name: string]: string[]} = {};
static terminalHistory: { [name: string]: Terminal} = {};

constructor(private onSend: (obj: Response | Request | Notify) => void) {
super(() => {
this.closeAllRequests();
if (this.terminal) {
Client.terminalHistory[this.name] = this.terminal.getHistoryStack();
this.terminal.dispose();
Client.terminalHistory[this.name]?.dispose();
Client.terminalHistory[this.name] = this.terminal;
this.terminal.disableInput();
this.terminal.print('\n⛔ 客户端已断开。下次启动游戏会复用此控制台。 ⛔\n');
}
this.treeViewManager.dispose();
Client.allClients.splice(Client.allClients.indexOf(this), 1);
Expand All @@ -95,17 +97,18 @@ export class Client extends vscode.Disposable {
public name = '默认客户端';

private createTerminal(name: string) {
Client.terminalHistory[name] ??= [];
this.terminal?.dispose();
this.terminal = new Terminal(name, async (data) => {
this.terminal = Client.terminalHistory[name] ?? new Terminal(name);
delete Client.terminalHistory[name];
this.terminal.setApplyHandler(async (data) => {
// 如果提交的数据只有空格,就忽略掉
if (data.trim() === '') {
return;
}
this.notify('command', { data: data });
});
this.terminal.multiMode = this.multiMode;
this.terminal.setHistoryStack(Client.terminalHistory[this.name] ?? []);
this.terminal.enableInput();
this.applyPrintBuffer();
}

Expand Down Expand Up @@ -252,7 +255,8 @@ export class Client extends vscode.Disposable {
}

vscode.commands.registerCommand('y3-helper.testTerminal', async () => {
let terminal = new Terminal('测试客户端', async (obj) => {
let terminal = new Terminal('测试客户端');
terminal.setApplyHandler(async (obj) => {
// await new Promise((resolve) => {
// setTimeout(resolve, 2000);
// });
Expand Down
9 changes: 7 additions & 2 deletions src/console/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,14 @@ class Pseudoterminal implements vscode.Pseudoterminal {
export class Terminal extends vscode.Disposable {
private pseudoterminal: Pseudoterminal;
private terminal: vscode.Terminal;
constructor(public name: string, onApply: (data: string) => Promise<void>) {
private _onApply?: (data: string) => Promise<void>;
constructor(public name: string) {
super(() => {
this.terminal.dispose();
});
this.pseudoterminal = new Pseudoterminal(async (data) => {
this.disableInput();
await onApply(data);
await this._onApply?.(data);
this.enableInput();
});
this.updateStartSymbol();
Expand All @@ -419,6 +420,10 @@ export class Terminal extends vscode.Disposable {
this.terminal.show();
}

setApplyHandler(handler: (data: string) => Promise<void>) {
this._onApply = handler;
}

private _multiMode = false;
set multiMode(res: boolean) {
this._multiMode = res;
Expand Down

0 comments on commit b337d3b

Please sign in to comment.