优化第一阶段
This commit is contained in:
21
lib/utils/terminal.js
Normal file
21
lib/utils/terminal.js
Normal file
@ -0,0 +1,21 @@
|
||||
export function clearScreen() {
|
||||
process.stdout.write("\x1Bc");
|
||||
}
|
||||
|
||||
export function strWidth(str = "") {
|
||||
let width = 0;
|
||||
for (const char of String(str)) {
|
||||
width += char.charCodeAt(0) > 127 ? 2 : 1;
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
export function padEnd(str, width) {
|
||||
const value = String(str);
|
||||
return value + " ".repeat(Math.max(0, width - strWidth(value)));
|
||||
}
|
||||
|
||||
export function centerPad(text, totalWidth) {
|
||||
const pad = Math.max(0, Math.floor((totalWidth - strWidth(text)) / 2));
|
||||
return " ".repeat(pad);
|
||||
}
|
||||
Reference in New Issue
Block a user