更新功能实现

This commit is contained in:
yinsx
2025-12-22 09:43:46 +08:00
parent 29a7a1e626
commit dd99e932b4
8 changed files with 78 additions and 68 deletions

View File

@ -1,6 +1,5 @@
import color from "picocolors";
import boxen from "boxen";
import figlet from "figlet";
import { gridSelect } from "./grid.js";
import * as convertTool from "./convert/index.js";
import * as ktx2Tool from "./ktx2/index.js";
@ -20,11 +19,7 @@ let poemConfig = {
borderColor: "cyan",
};
let titleConfig = {
text: "Zguiy Tool Box",
font: "Standard",
color: "magenta",
};
let updateInfo = null;
const tools = [
{ name: "格式转换", desc: "支持多种模型格式转换", tool: convertTool },
@ -47,10 +42,8 @@ export function setPoemStyle(style) {
Object.assign(poemConfig, style);
}
export function setTitle(text, font = "Standard", titleColor = "magenta") {
titleConfig.text = text;
titleConfig.font = font;
titleConfig.color = titleColor;
export function setUpdateInfo(info) {
updateInfo = info;
}
function renderPoem() {
@ -67,17 +60,21 @@ function renderPoem() {
});
}
function renderTitle() {
const art = figlet.textSync(titleConfig.text, { font: titleConfig.font });
const termWidth = process.stdout.columns || 80;
return art.split("\n").map(line => {
const pad = Math.max(0, Math.floor((termWidth - line.length) / 2));
return " ".repeat(pad) + color[titleConfig.color](line);
}).join("\n");
function renderUpdateInfo(selected) {
if (!updateInfo) return "";
const btn = selected ? color.cyan("[ 立即更新 ]") :color.white(" 立即更新 ") ;
const msg = `📦 发现新版本: ${color.red(updateInfo.current)}${color.green(updateInfo.latest)} ${btn}`;
return "\n" + boxen(color.yellow(msg), {
padding: { top: 0, bottom: 0, left: 2, right: 2 },
borderStyle: "round",
borderColor: "green",
textAlignment: "center",
float: "center",
});
}
function renderHeader() {
return renderPoem() + "\n\n" + renderTitle();
function renderHeader(onUpdate) {
return renderPoem() + renderUpdateInfo(onUpdate);
}
export async function showMainMenu() {
@ -86,7 +83,8 @@ export async function showMainMenu() {
cols: 3,
colWidth: 24,
renderHeader: renderHeader,
updateInfo: updateInfo,
});
}
export { tools, poemConfig, titleConfig };
export { tools, poemConfig };