2024-12-25 17:16:11 +08:00
|
|
|
export class Chunk {
|
|
|
|
/**
|
|
|
|
* 显示的值
|
|
|
|
*/
|
|
|
|
value: string = "";
|
|
|
|
/**
|
|
|
|
* 是否换行
|
|
|
|
*/
|
|
|
|
private newline: boolean = false;
|
|
|
|
/**
|
|
|
|
* 显示的样式
|
|
|
|
*/
|
|
|
|
style: string[] = [];
|
|
|
|
constructor(v: string, newline: boolean = false) {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.value = v;
|
|
|
|
this.newline = newline;
|
2024-12-25 17:16:11 +08:00
|
|
|
}
|
|
|
|
color(c: string) {
|
|
|
|
this.style.push(`color:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
background(c: string) {
|
|
|
|
this.style.push(`background:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
padding(c: string) {
|
|
|
|
this.style.push(`padding:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
fontwight(c: string) {
|
|
|
|
this.style.push(`font-weight:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
bold() {
|
|
|
|
return this.fontwight("bold");
|
|
|
|
}
|
|
|
|
margin(c: string) {
|
|
|
|
this.style.push(`margin:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
marginLeft(c: string) {
|
|
|
|
this.style.push(`margin-left:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
marginRight(c: string) {
|
|
|
|
this.style.push(`margin-right:${c}`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
toValue() {
|
2024-12-27 14:14:38 +08:00
|
|
|
return `%c${this.value}${this.newline ? "\n" : ""}`;
|
2024-12-25 17:16:11 +08:00
|
|
|
}
|
|
|
|
toStyle() {
|
2024-12-27 14:14:38 +08:00
|
|
|
return this.style.join(";");
|
2024-12-25 17:16:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-15 19:59:51 +08:00
|
|
|
export class Terminal {
|
2024-12-24 21:09:13 +08:00
|
|
|
/**
|
|
|
|
* 标签
|
|
|
|
*/
|
2024-12-27 14:14:38 +08:00
|
|
|
tag = "terminal";
|
2024-12-24 21:23:04 +08:00
|
|
|
/**
|
|
|
|
* 子标签
|
|
|
|
*/
|
|
|
|
subTag = "";
|
2024-12-24 21:09:13 +08:00
|
|
|
/**
|
|
|
|
* 标签的颜色
|
|
|
|
*/
|
2024-12-27 14:14:38 +08:00
|
|
|
tagColor = "blue";
|
2024-12-24 21:09:13 +08:00
|
|
|
/**
|
|
|
|
* 标签的背景色
|
|
|
|
*/
|
2024-12-27 14:14:38 +08:00
|
|
|
tagBackground = "yellow";
|
2024-12-24 21:09:13 +08:00
|
|
|
/**
|
|
|
|
* 日志文本的颜色
|
|
|
|
*/
|
2024-12-27 14:14:38 +08:00
|
|
|
txtColor = "black";
|
2024-12-25 17:16:11 +08:00
|
|
|
private chunks: Chunk[] = [];
|
|
|
|
constructor(tag: string) {
|
2024-12-15 19:59:51 +08:00
|
|
|
this.tag = tag;
|
|
|
|
}
|
2024-12-24 17:31:26 +08:00
|
|
|
init(): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "black";
|
2024-12-24 21:23:04 +08:00
|
|
|
this.subTag = "init";
|
2024-12-25 17:16:11 +08:00
|
|
|
return this.log();
|
|
|
|
}
|
|
|
|
|
|
|
|
public log(message: string = "", newline: boolean = false): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
const txt = new Chunk(message).color(this.txtColor).background("#e6e6e6").marginLeft("5px");
|
2024-12-25 17:16:11 +08:00
|
|
|
return this.doChunk(newline, [txt]);
|
|
|
|
}
|
2024-12-27 14:14:38 +08:00
|
|
|
public chunkMessage(chunk: Chunk[]) {
|
2024-12-25 17:16:11 +08:00
|
|
|
this.subTag = "message";
|
2024-12-27 14:14:38 +08:00
|
|
|
return this.doChunk(false, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
public chunkSend(chunk: Chunk[]) {
|
|
|
|
this.subTag = "send ";
|
|
|
|
return this.doChunk(false, chunk);
|
2024-12-25 17:16:11 +08:00
|
|
|
}
|
|
|
|
private doChunk(newline: boolean = false, chunks: Chunk[]) {
|
|
|
|
this.chunks = [];
|
2024-12-27 14:14:38 +08:00
|
|
|
const tag = new Chunk(this.tag).color(this.tagColor).background(this.tagBackground).padding("0 4px");
|
2024-12-25 17:16:11 +08:00
|
|
|
this.chunks.push(tag);
|
|
|
|
|
2024-12-27 14:14:38 +08:00
|
|
|
const subTag = new Chunk(this.subTag, newline).color(this.tagBackground).background(this.tagColor).padding("0 3px");
|
2024-12-25 17:16:11 +08:00
|
|
|
this.chunks.push(subTag);
|
|
|
|
|
|
|
|
chunks.forEach((c) => {
|
|
|
|
this.chunks.push(c);
|
2024-12-27 14:14:38 +08:00
|
|
|
});
|
2024-12-25 17:16:11 +08:00
|
|
|
|
2024-12-27 14:14:38 +08:00
|
|
|
let head = "*";
|
2024-12-25 17:16:11 +08:00
|
|
|
for (let i = 0; i < this.chunks.length; i++) {
|
|
|
|
const chunk = this.chunks[i];
|
|
|
|
head += chunk.toValue();
|
|
|
|
}
|
|
|
|
const ret = [head];
|
|
|
|
this.chunks.forEach((chunk) => {
|
|
|
|
ret.push(chunk.toStyle());
|
2024-12-27 14:14:38 +08:00
|
|
|
});
|
2024-12-25 17:16:11 +08:00
|
|
|
this.reset();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
private reset() {
|
|
|
|
this.subTag = "";
|
2024-12-15 19:59:51 +08:00
|
|
|
}
|
2024-12-24 17:31:26 +08:00
|
|
|
public blue(message: string): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "blue";
|
2024-12-24 21:23:04 +08:00
|
|
|
this.subTag = "";
|
2024-12-24 17:31:26 +08:00
|
|
|
return this.log(message);
|
|
|
|
}
|
|
|
|
public green(message: string): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "green";
|
2024-12-24 21:23:04 +08:00
|
|
|
this.subTag = "";
|
2024-12-24 17:31:26 +08:00
|
|
|
return this.log(message);
|
|
|
|
}
|
|
|
|
public red(message: string): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "red";
|
2024-12-24 21:23:04 +08:00
|
|
|
this.subTag = "";
|
2024-12-24 17:31:26 +08:00
|
|
|
return this.log(message);
|
|
|
|
}
|
2024-12-27 14:14:38 +08:00
|
|
|
send(msg: string) {
|
|
|
|
this.txtColor = "black";
|
|
|
|
this.subTag = "send";
|
|
|
|
return this.log(`${msg}`);
|
|
|
|
}
|
2024-12-24 21:09:13 +08:00
|
|
|
message(msg: string): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "black";
|
|
|
|
this.subTag = "message";
|
2024-12-24 21:23:04 +08:00
|
|
|
return this.log(`${msg}`);
|
2024-12-24 21:09:13 +08:00
|
|
|
}
|
2024-12-24 17:31:26 +08:00
|
|
|
connect(msg: string): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "black";
|
|
|
|
this.subTag = "connect";
|
2024-12-24 21:23:04 +08:00
|
|
|
return this.log(`${msg}`);
|
2024-12-24 17:31:26 +08:00
|
|
|
}
|
|
|
|
disconnect(msg: string): string[] {
|
2024-12-27 14:14:38 +08:00
|
|
|
this.txtColor = "black";
|
|
|
|
this.subTag = "disconnect";
|
2024-12-24 21:23:04 +08:00
|
|
|
return this.log(`${msg}`);
|
2024-12-15 19:59:51 +08:00
|
|
|
}
|
|
|
|
}
|