Skip to content

Commit fafd148

Browse files
committed
feat: 完善控制台打印的日志
1 parent f13959a commit fafd148

File tree

3 files changed

+79
-65
lines changed

3 files changed

+79
-65
lines changed

src/formatter/request-formatter.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class RequestFormatter {
2121
const requestContext = scriptContext.requestContext;
2222
const language = getLanguage(getGlobalConfig().language);
2323

24-
// 把参数以表格的形式打印
25-
// const data = this.convertParamsToTableData(language, requestContext.params);
26-
// console.table(data);
27-
28-
// 示例数据
2924
const data = [
3025
// TODO 2025-01-08 01:28:26 国际化
3126
["名称", "值", "备注"],
@@ -65,7 +60,8 @@ class RequestFormatter {
6560
};
6661

6762
// 打印表格
68-
printStyledTable(data, styles);
63+
const title = language.console.titleRequest;
64+
printStyledTable(data, styles, title);
6965

7066
}
7167

src/formatter/response-formatter.js

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const {fillToLength} = require("../utils/string-util");
33
const {genFormatArray} = require("../utils/log-util");
44
const {getGlobalConfig} = require("../config/config");
55
const {highlightJSON} = require("./json-formatter");
6+
const {getUserCodeLocation} = require("../utils/code-util");
7+
const {printStyledTable} = require("./table-formatter");
68

79
/**
810
* 用于对响应进行格式化
@@ -15,63 +17,39 @@ class ResponseFormatter {
1517
*/
1618
format(scriptContext) {
1719

20+
const codeLocation = getUserCodeLocation();
21+
1822
const responseContext = scriptContext.responseContext;
23+
const requestContext = scriptContext.requestContext;
1924
// const language = getLanguage(getGlobalConfig().language);
2025
const language = chinese;
2126

22-
// 提示词:
23-
// 我需要写一些console.log的样式打印在控制台上,接下来会陆陆续续给你一些写样式的任务,球球你帮帮我
24-
25-
const valueStyle = `color: black; background: #E50000; font-size: 12px; font-weight: bold;`;
26-
const normalStyle = `color: black; background: #FF6766; font-size: 12px;`;
27-
28-
const titleStyle = 'font-weight: bold; font-size: 16px; background: green; color: white; padding: 5px; border-radius: 3px;';
29-
30-
const length = 200;
31-
32-
// const msgs = [];
33-
// msgs.push()
34-
// msgs.push("Time: " + new Date().toLocaleString() + "\n");
35-
// msgs.push(`Request ID: ${scriptContext.requestId} \n`);
36-
// msgs.push(`${scriptContext.responseContext.toHumanReadable(0)}`);
37-
// return msgs.join("\n");
38-
39-
const message = [
40-
normalStyle, fillToLength(`-------------------------------------------------- Script Hook Captured Response --------------------------------------------------`, length) + "\n",
41-
normalStyle, fillToLength(`${language.console.time}: ${new Date().toLocaleString()}`, length) + "\n",
42-
normalStyle, fillToLength(`${language.console.requestId}: ${scriptContext.requestId}`, length) + "\n",
43-
// normalStyle, fillToLength(`${language.console.isJsonpRequest}: ${scriptContext.isJsonp()}`, length) + "\n",
44-
// normalStyle, fillToLength(`${language.console.hostname}: ${requestContext.hostname}`, length) + "\n",
45-
// normalStyle, fillToLength(`${language.console.path}: ${requestContext.path}`, length) + "\n",
46-
// normalStyle, fillToLength((() => {
47-
// const paramTitle = `${language.console.param}(${requestContext.params.length})`;
48-
// return paramTitle;
49-
// })(), length) + "\n",
50-
// normalStyle, fillToLength(`${language.console.hash}: ${requestContext.hash}`, length) + "\n",
51-
52-
// (() => {
53-
//
54-
// let paramTitle = `${indentSpace}`;
55-
// if (!this.params.length) {
56-
// paramTitle += " do not have param.";
57-
// }
58-
// msgs.push(paramTitle);
59-
// for (let param of this.params) {
60-
// msgs.push(param.toHumanReadable(indent + 4));
61-
// }
62-
//
63-
//
64-
// if (this.hash) {
65-
// msgs.push()
66-
// }
67-
//
68-
// return msgs.join("\n\n");
69-
// })()
70-
27+
const data = [
28+
// TODO 2025-01-08 01:28:26 国际化
29+
["名称", "值", "备注"],
30+
[language.console.time, new Date().toLocaleString(), ""],
31+
[language.console.requestId, scriptContext.requestId, ""],
32+
[language.console.isJsonpRequest, scriptContext.isJsonp(), ""],
33+
[language.console.hostname, requestContext.hostname, ""],
34+
[language.console.path, requestContext.path, ""],
35+
[language.console.hash, requestContext.hash, ""],
36+
[language.console.codeLocation, codeLocation, ""],
37+
// [language.console.param, requestContext.params.length],
7138
];
72-
console.log(genFormatArray(message), ...message);
39+
// 示例样式
40+
const styles = {
41+
borderColor: '#000',
42+
cellBackgroundColor: '#f0f0f0',
43+
fontSize: '14px',
44+
fontColor: '#333'
45+
};
46+
47+
// 打印表格
48+
const title = language.console.titleResponse;
49+
printStyledTable(data, styles, title);
50+
const msgs = highlightJSON(responseContext.jsonpCallbackArguments);
51+
console.log(msgs);
7352

74-
highlightJSON(responseContext.jsonpCallbackArguments);
7553

7654
}
7755

src/formatter/table-formatter.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
function printStyledTable(data, styles) {
2-
const { borderColor, cellBackgroundColor, fontSize, fontColor } = styles;
1+
function printStyledTable(data, styles, title = '') {
2+
const { borderColor, cellBackgroundColor, fontSize, fontColor, titleFontSize = '20px' } = styles;
3+
4+
// 计算字符串的实际宽度(中文字符宽度为2,英文字符宽度为1)
5+
function getStringWidth(str) {
6+
let width = 0;
7+
for (const char of str) {
8+
width += /[\u4e00-\u9fa5]/.test(char) ? 2 : 1; // 判断是否为中文字符
9+
}
10+
return width;
11+
}
312

413
// 计算每列的最大宽度
514
let colWidths = data[0].map((_, colIndex) =>
6-
Math.max(...data.map(row => String(row[colIndex]).length))
15+
Math.max(...data.map(row => getStringWidth(String(row[colIndex]))))
716
);
817

18+
// 计算表格的总宽度
19+
const totalWidth = colWidths.reduce((sum, width) => sum + width + 3, 0) - 1;
20+
921
// 定义表格的样式
1022
const tableStyle = `
1123
padding: 5px;
@@ -17,26 +29,54 @@ function printStyledTable(data, styles) {
1729
white-space: pre; // 保留空格和换行
1830
`;
1931

32+
// 定义标题的样式(背景色和边框与表格一致)
33+
const titleStyle = `
34+
font-size: ${titleFontSize};
35+
font-weight: bold;
36+
text-align: center;
37+
background-color: ${cellBackgroundColor};
38+
border: 1px solid ${borderColor};
39+
padding: 5px;
40+
`;
41+
42+
// 根据实际宽度填充字符串
43+
function padString(str, width) {
44+
const strWidth = getStringWidth(str);
45+
if (strWidth >= width) return str; // 如果字符串宽度已经足够,直接返回
46+
const padding = ' '.repeat(width - strWidth); // 计算需要填充的空格
47+
return str + padding;
48+
}
49+
2050
// 构建表格内容
2151
let tableContent = '';
2252

53+
// 如果有标题,添加标题行
54+
if (title) {
55+
const titlePadding = Math.floor((totalWidth - getStringWidth(title)) / 2);
56+
const paddedTitle = padString(title, totalWidth);
57+
tableContent += '%c' + paddedTitle + '\n';
58+
}
59+
2360
// 表头
2461
tableContent += data[0]
25-
.map((cell, index) => cell.padEnd(colWidths[index]))
62+
.map((cell, index) => padString(String(cell), colWidths[index]))
2663
.join(' │ ');
2764

2865
// 表格内容
2966
for (let i = 1; i < data.length; i++) {
3067
tableContent += '\n' + data[i]
31-
.map((cell, index) => String(cell).padEnd(colWidths[index]))
68+
.map((cell, index) => padString(String(cell), colWidths[index]))
3269
.join(' │ ');
3370
}
3471

3572
// 打印表格
36-
console.log('%c' + tableContent, tableStyle);
73+
if (title) {
74+
console.log('%c' + tableContent, titleStyle, tableStyle);
75+
} else {
76+
console.log('%c' + tableContent, tableStyle);
77+
}
3778
}
3879

39-
4080
module.exports = {
4181
printStyledTable
42-
}
82+
};

0 commit comments

Comments
 (0)