Skip to content

Commit

Permalink
控制台暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed May 23, 2024
1 parent 4d83159 commit 8303794
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/console/client.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import * as vscode from "vscode";
import * as tools from "../tools";

type Handler = (obj: { [key: string]: any }) => Promise<any>;
type Request = {
type Handler = (client: Client, params: { [key: string]: any }) => Promise<any>;
interface Request {
method: string,
id: number,
params: { [key: string]: any },
};

type Result = {
interface Result {
id: number,
result?: any,
error?: string,
};

let methods: Map<string, Handler> = new Map();


export function registerMethod(method: string, handler: Handler) {
methods.set(method, handler);
}
Expand All @@ -26,19 +25,26 @@ export class Client extends vscode.Disposable {
super(() => {
this.console.dispose();
});
const writeEmitter = new vscode.EventEmitter<string>();
this.writeEmitter;
this.console = vscode.window.createTerminal({
name: 'Y3助手控制台',
pty: {
onDidWrite: writeEmitter.event,
open: () => {
writeEmitter.fire('\x1b[31mHello world\x1b[0m');
},
onDidWrite: this.writeEmitter.event,
open: () => {},
close: () => {}
},
});
this.console.show();
}

private writeEmitter = new vscode.EventEmitter<string>();

print(msg: string) {
//把单独的 \n 替换为 \r\n,但要排除已有的 \r\n
msg = msg.replace(/(?<!\r)\n/g, '\r\n');
this.writeEmitter.fire(msg + '\r\n');
};

private console: vscode.Terminal;

async recv(obj: Request) {
Expand All @@ -47,14 +53,16 @@ export class Client extends vscode.Disposable {
if (handler) {
let id = obj.id;
try {
let result = await handler(obj);
let result = await handler(this, obj.params);
this.send({ id, result });
} catch (e) {
if (e instanceof Error) {
tools.log.error(e);
this.send({ id, error: e.message });
}
}
} else {
this.send({ id: obj.id, error: `未找到方法"${method}"` });
}
}

Expand All @@ -68,3 +76,12 @@ export class Client extends vscode.Disposable {
this._sender = sender;
}
}

interface PrintParams {
message: string;
}

registerMethod('print', async (client, params) => {
let p = params as PrintParams;
client.print(p.message);
});
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EXCELimporter } from './editorTable/EXCEL/EXCELimporter';
import { TemplateGenerator } from './editorTable/templateGenerator';
import { englishPathToChinese } from './constants';
import { NetworkServer } from './networkServer';
import * as console from './console';
import {
CSVimporter, EditorTableDataProvider, GoEditorTableSymbolProvider,
GoEditorTableDocumentSymbolProvider, FileNode,
Expand Down Expand Up @@ -731,6 +732,7 @@ class Helper {
mainMenu.init();
metaBuilder.init();
debug.init(this.context);
console.init();

this.initEditorTableWatcher();
}, 100);
Expand Down

0 comments on commit 8303794

Please sign in to comment.