Files
NapCatQQ/packages/napcat-webui-frontend/src/utils/terminal.ts
T
手瓜一十雪 4360775eff refactor: 整体重构 (#1381)
* feat: pnpm new

* Refactor build and release workflows, update dependencies

Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
2025-11-13 15:39:42 +08:00

98 lines
2.4 KiB
TypeScript

import { LogLevel } from '@/const/enum';
export const gradientText = (
text: string,
startColor: [number, number, number] = [255, 0, 0],
endColor: [number, number, number] = [0, 0, 255],
bold: boolean = false,
italic: boolean = false,
underline: boolean = false
) => {
const steps = text.length;
const colorStep = startColor.map(
(start, index) => (endColor[index] - start) / steps
);
let coloredText = '';
for (let i = 0; i < steps; i++) {
const color = startColor.map((start, index) =>
Math.round(start + colorStep[index] * i)
);
coloredText += `\x1b[38;2;${color[0]};${color[1]};${color[2]}m${text[i]}`;
}
// 添加样式
if (bold) {
coloredText = `\x1b[1m${coloredText}`;
}
if (italic) {
coloredText = `\x1b[3m${coloredText}`;
}
if (underline) {
coloredText = `\x1b[4m${coloredText}`;
}
return coloredText + '\x1b[0m'; // 重置颜色和样式
};
export const logColor = {
[LogLevel.DEBUG]: 'green',
[LogLevel.INFO]: 'black',
[LogLevel.WARN]: 'yellow',
[LogLevel.ERROR]: 'red',
[LogLevel.FATAL]: 'red',
} as const;
export const colorizeLogLevel = (content: string) => {
const logLevel = content.match(/\[[a-zA-Z]+\]/) || [];
let _content = content;
const level =
(logLevel?.[0]?.replace('[', '').replace(']', '') as LogLevel) ??
LogLevel.INFO;
const color = logColor[level];
switch (color) {
case 'green':
_content = `\x1b[32m${_content}\x1b[0m`;
break;
case 'black':
_content = `\x1b[30m${_content}\x1b[0m`;
break;
case 'yellow':
_content = `\x1b[33m${_content}\x1b[0m`;
break;
case 'red':
_content = `\x1b[31m${_content}\x1b[0m`;
break;
default:
_content = `\x1b[30m${_content}\x1b[0m`;
}
return {
content: _content,
level,
};
};
export const colorizeLogLevelWithTag = (content: string, level: LogLevel) => {
let _content = content;
switch (level) {
case LogLevel.DEBUG:
_content = `\x1b[32m[DEBUG] ${content}\x1b[0m`;
break;
case LogLevel.INFO:
_content = `\x1b[30m[INFO] ${content}\x1b[0m`;
break;
case LogLevel.WARN:
_content = `\x1b[33m[WARN] ${content}\x1b[0m`;
break;
case LogLevel.ERROR:
_content = `\x1b[31m[ERROR] ${content}\x1b[0m`;
break;
case LogLevel.FATAL:
_content = `\x1b[31m[FATAL] ${content}\x1b[0m`;
break;
default:
_content = `\x1b[30m${content}\x1b[0m`;
}
return _content;
};