From 7e1111470c9ff64b60d5447fe32ee6986c679793 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 2 Jun 2020 17:15:54 +0800 Subject: [PATCH] feat: update console Signed-off-by: MiaoWoo --- packages/api/src/console.ts | 18 +++++++++++++----- packages/spring/src/console.ts | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/api/src/console.ts b/packages/api/src/console.ts index 0ed4e5c1..2af5c37e 100644 --- a/packages/api/src/console.ts +++ b/packages/api/src/console.ts @@ -128,19 +128,23 @@ export class MiaoScriptConsole implements Console { lineNumber } } - stack(ex: Error): string[] { + stack(ex: Error, color: boolean = true): string[] { let stack = ex.getStackTrace(); - let cache = ['§c' + ex]; + let cache = [(color ? '§c' : '') + ex]; //@ts-ignore if (stack.class) { stack = Arrays.asList(stack) } stack.forEach(trace => { - if (!trace.fileName || trace.fileName.startsWith('jar:file:')) { return } + if (!trace.fileName || trace.fileName.startsWith('jar:file:') || trace.fileName.startsWith('file:')) { return } if (trace.className.startsWith('<')) { let { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber) if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] } - cache.push(` §e->§c ${fileName}:${lineNumber} => §4${trace.methodName}`) + if (color) { + cache.push(` §e->§c ${fileName}:${lineNumber} => §4${trace.methodName}`) + } else { + cache.push(` -> ${fileName}:${lineNumber} => ${trace.methodName}`) + } } else { let className = trace.className; var fileName = trace.fileName as string; @@ -155,7 +159,11 @@ export class MiaoScriptConsole implements Console { } } } - cache.push(` §e->§c ${className}.${trace.methodName}(§4${fileName}:${lineNumber}§c)`); + if (color) { + cache.push(` §e->§c ${className}.${trace.methodName}(§4${fileName}:${lineNumber}§c)`); + } else { + cache.push(` -> ${className}.${trace.methodName}(${fileName}:${lineNumber})`); + } } }); return cache; diff --git a/packages/spring/src/console.ts b/packages/spring/src/console.ts index 94e664b1..3fbc1a49 100644 --- a/packages/spring/src/console.ts +++ b/packages/spring/src/console.ts @@ -32,8 +32,22 @@ function mcColor2ANSI(str: string) { } export class SpringConsole extends MiaoScriptConsole { + error(...args: any[]) { + this.logger.error(args.join(' ')) + } sender(sender: any, ...args: any[]) { - this.console(args.join(' ')) + sender = sender || { + sendMessage: (message: string) => console.console(message) + } + if (!sender.sendMessage) { + this.error('第一个参数未实现 sendMessage 无法发送消息!') + return + } + if (Object.prototype.toString.call(args[0]) === '[object Array]') { + args[0].forEach(line => sender.sendMessage(this.prefix + line)) + } else { + sender.sendMessage(this.prefix + args.join(' ')); + } } console(...args: string[]): void { this.logger.info(mcColor2ANSI(args.join(' ') + '§r'))