Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 9c27495

Browse files
committed
新增 颜色渲染
1 parent 8b9a784 commit 9c27495

File tree

2 files changed

+157
-2
lines changed

2 files changed

+157
-2
lines changed

src/app/service/terminal_color.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
Copyright (C) 2022 Suwings(https://github.com/Suwings)
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
According to the GPL, it is forbidden to delete all copyright notices,
15+
and if you modify the source code, you must open source the
16+
modified source code.
17+
18+
版权所有 (C) 2022 Suwings(https://github.com/Suwings)
19+
20+
本程序为自由软件,你可以依据 GPL 的条款(第三版或者更高),再分发和/或修改它。
21+
该程序以具有实际用途为目的发布,但是并不包含任何担保,
22+
也不包含基于特定商用或健康用途的默认担保。具体细节请查看 GPL 协议。
23+
24+
根据协议,您必须保留所有版权声明,如果修改源码则必须开源修改后的源码。
25+
前往 https://mcsmanager.com/ 申请闭源开发授权或了解更多。
26+
*/
27+
28+
// 用于 Terminal 视图中终端组件的文字颜色渲染
29+
export function encodeConsoleColor(text) {
30+
const term = {};
31+
term.TERM_NULL = "\x1B[0m";
32+
term.TERM_TEXT_RED = "\x1B[1;0;31m";
33+
term.TERM_TEXT_GREEN = "\x1B[1;0;32m";
34+
term.TERM_TEXT_YELLOW = "\x1B[1;0;33m";
35+
term.TERM_TEXT_BLUE = "\x1B[1;1;34m";
36+
term.TERM_TEXT_FUCHSIA = "\x1B[1;0;35m";
37+
term.TERM_TEXT_CYAN = "\x1B[1;0;36m";
38+
term.TERM_TEXT_WHITE = "\x1B[1;0;37m";
39+
term.TERM_TEXT_B = "\x1B[1m";
40+
41+
// 基本颜色
42+
text = text.replace(/([A-Za-z _§&;\-\\.]{1,}:)/gim, "§6$1§r");
43+
text = text.replace(/INFO/gm, term.TERM_TEXT_GREEN + "INFO" + term.TERM_NULL);
44+
text = text.replace(/(\d{2,}:\d{2,}:\d{2,})/gm, term.TERM_TEXT_CYAN + "$1" + term.TERM_NULL);
45+
46+
// Minecraft 原生颜色替代解析
47+
text = text.replace(/§0/gm, term.TERM_TEXT_WHITE);
48+
text = text.replace(/§1/gm, term.TERM_TEXT_CYAN);
49+
text = text.replace(/§2/gm, term.TERM_TEXT_GREEN);
50+
text = text.replace(/§3/gm, term.TERM_TEXT_CYAN);
51+
text = text.replace(/§4/gm, term.TERM_TEXT_RED);
52+
text = text.replace(/§5/gm, term.TERM_TEXT_FUCHSIA);
53+
text = text.replace(/§6/gm, term.TERM_TEXT_YELLOW);
54+
text = text.replace(/§7/gm, term.TERM_TEXT_WHITE);
55+
text = text.replace(/§8/gm, term.TERM_TEXT_WHITE);
56+
text = text.replace(/§9/gm, term.TERM_TEXT_CYAN);
57+
text = text.replace(/§a/gm, term.TERM_TEXT_GREEN);
58+
text = text.replace(/§b/gm, term.TERM_TEXT_CYAN);
59+
text = text.replace(/§c/gm, term.TERM_TEXT_RED);
60+
text = text.replace(/§d/gm, term.TERM_TEXT_RED);
61+
text = text.replace(/§e/gm, term.TERM_TEXT_YELLOW);
62+
text = text.replace(/§f/gm, term.TERM_TEXT_WHITE);
63+
// 基于&符号
64+
text = text.replace(/&0/gm, term.TERM_TEXT_WHITE);
65+
text = text.replace(/&1/gm, term.TERM_TEXT_CYAN);
66+
text = text.replace(/&2/gm, term.TERM_TEXT_GREEN);
67+
text = text.replace(/&3/gm, term.TERM_TEXT_CYAN);
68+
text = text.replace(/&4/gm, term.TERM_TEXT_RED);
69+
text = text.replace(/&5/gm, term.TERM_TEXT_FUCHSIA);
70+
text = text.replace(/&6/gm, term.TERM_TEXT_YELLOW);
71+
text = text.replace(/&7/gm, term.TERM_TEXT_WHITE);
72+
text = text.replace(/&8/gm, term.TERM_TEXT_WHITE);
73+
text = text.replace(/&9/gm, term.TERM_TEXT_CYAN);
74+
text = text.replace(/&a/gm, term.TERM_TEXT_GREEN);
75+
text = text.replace(/&b/gm, term.TERM_TEXT_CYAN);
76+
text = text.replace(/&c/gm, term.TERM_TEXT_RED);
77+
text = text.replace(/&d/gm, term.TERM_TEXT_RED);
78+
text = text.replace(/&e/gm, term.TERM_TEXT_YELLOW);
79+
text = text.replace(/&f/gm, term.TERM_TEXT_WHITE);
80+
// 字体格式
81+
text = text.replace(/§r/gm, term.TERM_NULL);
82+
text = text.replace(/&r/gm, term.TERM_NULL);
83+
text = text.replace(/§k/gm, "\x1B[1m");
84+
text = text.replace(/&k/gm, "\x1B[1m");
85+
text = text.replace(/§l/gm, "\x1B[1m");
86+
text = text.replace(/&l/gm, "\x1B[1m");
87+
text = text.replace(/§m/gm, "\x1B[2m");
88+
text = text.replace(/&m/gm, "\x1B[2m");
89+
text = text.replace(/§n/gm, "\x1B[4m");
90+
text = text.replace(/&n/gm, "\x1B[4m");
91+
text = text.replace(/§o/gm, "\x1B[3m");
92+
text = text.replace(/&o/gm, "\x1B[3m");
93+
94+
// 特殊文本替换
95+
var RegExpStringArr = [
96+
//蓝色
97+
[
98+
"Unknown command",
99+
"Loading libraries, please wait...",
100+
"Loading",
101+
"Loaded",
102+
"\\d{1,3}%",
103+
"true",
104+
"false",
105+
"plugin.yml"
106+
],
107+
//绿色
108+
[
109+
"/help",
110+
"left the game",
111+
"Enabling",
112+
"Server thread",
113+
"Saving chunks for level",
114+
"----",
115+
"UUID",
116+
"Starting minecraft server version",
117+
"Timings Reset",
118+
"\\(",
119+
"\\)",
120+
"\\{",
121+
"\\}",
122+
"<",
123+
">",
124+
"Preparing start region for level"
125+
],
126+
//红色
127+
["WARN", "EULA", "Error", "Invalid", "Stopping the server", "Caused by", "Stopping"],
128+
//黄色
129+
["Starting Minecraft server on", "world_the_end", "world_nether", "Done", "MCSMANAGER"]
130+
];
131+
for (var k in RegExpStringArr) {
132+
for (var y in RegExpStringArr[k]) {
133+
var reg = new RegExp("(" + RegExpStringArr[k][y].replace(/ /gim, " ") + ")", "igm");
134+
if (k == 0)
135+
//蓝色
136+
text = text.replace(reg, term.TERM_TEXT_BLUE + "$1" + term.TERM_NULL);
137+
if (k == 1)
138+
//绿色
139+
text = text.replace(reg, term.TERM_TEXT_GREEN + "$1" + term.TERM_NULL);
140+
if (k == 2)
141+
//红色
142+
text = text.replace(reg, term.TERM_TEXT_RED + "$1" + term.TERM_NULL);
143+
if (k == 3)
144+
//黄色
145+
text = text.replace(reg, term.TERM_TEXT_YELLOW + "$1" + term.TERM_NULL);
146+
}
147+
}
148+
// 行结尾符号替换
149+
text = text.replace(/\r\n/gm, term.TERM_NULL + "\r\n");
150+
return text;
151+
}

src/app/views/Terminal.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ import {
484484
} from "../service/common";
485485
import router from "../router";
486486
import { parseforwardAddress, request } from "../service/protocol";
487+
import { encodeConsoleColor } from "../service/terminal_color";
487488
import { ElNotification } from "element-plus";
488489
import { statusCodeToText, typeTextToReadableText } from "../service/instance_tools";
489490
import { initTerminalWindow, textToTermText } from "../service/term";
@@ -602,12 +603,15 @@ export default {
602603
603604
// 监听输出流
604605
this.socket.on("instance/stdout", (packet) => {
605-
this.term.write(textToTermText(packet.data.text));
606+
if (this.instanceInfo?.config?.terminalOption?.haveColor) {
607+
this.term.write(encodeConsoleColor(packet.data.text));
608+
} else {
609+
this.term.write(textToTermText(packet.data.text));
610+
}
606611
});
607612
// 监听实例详细信息
608613
this.socket.on("stream/detail", (packet) => {
609614
this.instanceInfo = packet.data;
610-
console.log("[WS->Daemon] 实例信息", this.instanceInfo);
611615
});
612616
// 断开事件
613617
this.socket.on("disconnect", () => {

0 commit comments

Comments
 (0)