-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0.js
136 lines (112 loc) · 4.25 KB
/
0.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const width = 100;
const textwidth = 80;
async function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const wrapText = (text, maxWidth) => {
const lines = [];
let currentLine = '';
let currentWidth = 0;
for (let char of text) {
const charWidth = getTextWidth(char);
if (currentWidth + charWidth > maxWidth) {
lines.push(currentLine);
currentLine = char;
currentWidth = charWidth;
} else {
currentLine += char;
currentWidth += charWidth;
}
}
if (currentLine) {
lines.push(currentLine);
}
return lines;
};
const centerTextWithBorders = (text) => {
const wrappedLines = wrapText(text, textwidth);
return wrappedLines.map(line => {
const lineWidth = getTextWidth(line);
if (width <= lineWidth + 2) {
return `|${line}|`;
}
const totalSpaces = width - lineWidth - 2;
const leftSpaces = Math.floor(totalSpaces / 2);
const rightSpaces = totalSpaces - leftSpaces;
return `|${' '.repeat(leftSpaces)}${line}${' '.repeat(rightSpaces)}|`;
}).join('\n');
};
const getTextWidth = (text) => {
const cleanText = text.replace(/\x1b\[\d{1,2}m/g, '');
let width = 0;
for (const char of cleanText) {
if (char.match(/[\u4e00-\u9fa5\u3000-\u303F\uFF00-\uFFEF\u3040-\u30FF]/)) {
width += 2;
} else {
width += 1;
}
}
return width;
};
const colors = {
red: '\x1b[31m',
green: '\x1b[32m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
reset: '\x1b[0m'
};
const printDivider = (width) => {
return `|${'-'.repeat(width - 2)}|`;
};
const centerTextmrhuang_ascii = (text, width) => {
const lines = text.split('\n');
return lines.map(line => {
const lineWidth = getTextWidth(line);
if (width <= lineWidth + 2) {
return `|${line}|`;
}
const totalSpaces = width - lineWidth - 2;
const leftSpaces = Math.floor(totalSpaces / 2);
const rightSpaces = totalSpaces - leftSpaces;
return `|${' '.repeat(leftSpaces)}${line}${' '.repeat(rightSpaces)}|`;
}).join('\n');
};
async function countdown(waitTime) {
return new Promise(resolve => {
let index = waitTime;
const timer = setInterval(() => {
index--;
const countdownText = `请等待: ${colors.green}${index}${colors.reset} 秒`;
const centeredText = centerTextWithBorders(countdownText, width);
process.stdout.write(`${colors.blue}${centeredText}${colors.reset}\r`);
if (index === 0) {
clearInterval(timer);
const endText = '倒计时结束';
const centeredEndText = centerTextWithBorders(endText, width);
console.log(`${colors.blue}${centeredEndText}${colors.reset}`);
resolve();
}
}, 1000);
});
}
const mrhuang_ascii = `
███╗ ███╗██████╗ ██╗ ██╗██╗ ██╗ █████╗ ███╗ ██╗ ██████╗
████╗ ████║██╔══██╗ ██║ ██║██║ ██║██╔══██╗████╗ ██║██╔════╝
██╔████╔██║██████╔╝ ███████║██║ ██║███████║██╔██╗ ██║██║ ███╗
██║╚██╔╝██║██╔══██╗ ██╔══██║██║ ██║██╔══██║██║╚██╗██║██║ ██║
██║ ╚═╝ ██║██║ ██║██╗ ██║ ██║╚██████╔╝██║ ██║██║ ╚████║╚██████╔╝
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝
`;
module.exports = {
width,
textwidth,
getRandomNumber,
wrapText,
centerTextWithBorders,
getTextWidth,
colors,
printDivider,
centerTextmrhuang_ascii,
countdown,
mrhuang_ascii
};