优化tui交互

This commit is contained in:
user123
2026-01-29 15:01:13 +08:00
parent 321abc59cd
commit 59839ad70a
4 changed files with 764 additions and 608 deletions

Binary file not shown.

View File

@@ -1,68 +1,151 @@
package style
import "github.com/charmbracelet/lipgloss"
import (
"fmt"
var (
// Colors
ColorPrimary = lipgloss.Color("#7D56F4") // Purple
ColorSecondary = lipgloss.Color("#04B575") // Green
ColorError = lipgloss.Color("#FF4C4C") // Red
ColorWarning = lipgloss.Color("#FFD700") // Gold
ColorSubtle = lipgloss.Color("#626262") // Gray
ColorText = lipgloss.Color("#FAFAFA") // White
// Styles
AppStyle = lipgloss.NewStyle().
Padding(1, 2)
HeaderStyle = lipgloss.NewStyle().
Foreground(ColorPrimary).
Bold(true).
PaddingBottom(1)
StepStyle = lipgloss.NewStyle().
Foreground(ColorText)
SuccessStyle = lipgloss.NewStyle().
Foreground(ColorSecondary).
Bold(true)
ErrorStyle = lipgloss.NewStyle().
Foreground(ColorError).
Bold(true)
WarningStyle = lipgloss.NewStyle().
Foreground(ColorWarning)
SubtleStyle = lipgloss.NewStyle().
Foreground(ColorSubtle)
CmdStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#00FFFF")).
Padding(0, 1)
HighlightStyle = lipgloss.NewStyle().
Foreground(ColorSecondary).
Bold(true)
"github.com/charmbracelet/lipgloss"
)
func RenderStep(prefix string, msg string, status string) string {
var statusStyle lipgloss.Style
switch status {
case "pending":
statusStyle = SubtleStyle
case "running":
statusStyle = lipgloss.NewStyle().Foreground(ColorPrimary)
case "done":
statusStyle = SuccessStyle
case "error":
statusStyle = ErrorStyle
default:
statusStyle = StepStyle
}
// Colors Palette - Modern & Professional
var (
ColorPrimary = lipgloss.Color("#7D56F4") // Purple
ColorSecondary = lipgloss.Color("#04B575") // Green
ColorError = lipgloss.Color("#FF3B30") // Red
ColorWarning = lipgloss.Color("#FFCC00") // Yellow
ColorSubtle = lipgloss.Color("#666666") // Grey
ColorText = lipgloss.Color("#E0E0E0") // White-ish
ColorHighlight = lipgloss.Color("#2A2A2A") // Dark Grey
ColorPanel = lipgloss.Color("#1E1E1E") // Panel BG
ColorBorder = lipgloss.Color("#333333") // Border
)
return lipgloss.JoinHorizontal(lipgloss.Left,
statusStyle.Width(3).Render(prefix),
statusStyle.Render(msg),
// Base App Style
var AppStyle = lipgloss.NewStyle().
Padding(1, 2)
// Headers
var HeaderStyle = lipgloss.NewStyle().
Foreground(ColorPrimary).
Bold(true).
PaddingBottom(1)
var SubHeaderStyle = lipgloss.NewStyle().
Foreground(ColorText).
Bold(true).
PaddingBottom(1)
var TitleStyle = lipgloss.NewStyle().
Foreground(ColorPrimary).
Bold(true).
Padding(0, 1).
Border(lipgloss.RoundedBorder()).
BorderForeground(ColorPrimary)
// Status Colors
var SuccessStyle = lipgloss.NewStyle().Foreground(ColorSecondary).Bold(true)
var ErrorStyle = lipgloss.NewStyle().Foreground(ColorError).Bold(true)
var WarningStyle = lipgloss.NewStyle().Foreground(ColorWarning)
var SubtleStyle = lipgloss.NewStyle().Foreground(ColorSubtle)
// Panels
var PanelStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(ColorBorder).
Padding(1, 2)
var FocusedPanelStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(ColorPrimary).
Padding(1, 2)
var WizardPanelStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(ColorPrimary).
Padding(1, 4).
Width(80)
// Menu Styles
var MenuNormalStyle = lipgloss.NewStyle().
Foreground(ColorText).
PaddingLeft(2)
var MenuSelectedStyle = lipgloss.NewStyle().
Foreground(ColorSecondary).
Background(ColorHighlight).
PaddingLeft(1).
Bold(true).
Border(lipgloss.NormalBorder(), false, false, false, true).
BorderForeground(ColorSecondary)
// Input & Form Styles
var KeyStyle = lipgloss.NewStyle().
Foreground(ColorPrimary).
Bold(true)
var DescriptionStyle = lipgloss.NewStyle().
Foreground(ColorSubtle).
Italic(true)
var InputHelpStyle = lipgloss.NewStyle().
Foreground(ColorSubtle).
MarginBottom(1)
var StepStyle = lipgloss.NewStyle().
Foreground(ColorWarning).
Bold(true)
var InputStyle = lipgloss.NewStyle().
Foreground(ColorText).
Border(lipgloss.RoundedBorder()).
BorderForeground(ColorSubtle).
Padding(0, 1)
var InputFocusedStyle = lipgloss.NewStyle().
Foreground(ColorText).
Border(lipgloss.RoundedBorder()).
BorderForeground(ColorPrimary).
Padding(0, 1)
// Badges
var BadgeBase = lipgloss.NewStyle().
Bold(true)
var BadgeInfo = BadgeBase.
Foreground(ColorPrimary)
var BadgeSuccess = BadgeBase.
Foreground(ColorSecondary)
var BadgeWarning = BadgeBase.
Foreground(ColorWarning)
var BadgeError = BadgeBase.
Foreground(ColorError)
// Helpers
func Badge(text, status string) string {
switch status {
case "success":
return BadgeSuccess.Render(text)
case "warning":
return BadgeWarning.Render(text)
case "error":
return BadgeError.Render(text)
default:
return BadgeInfo.Render(text)
}
}
func Checkbox(label string, checked bool) string {
if checked {
return fmt.Sprintf("[%s] %s", SuccessStyle.Render("x"), label)
}
return fmt.Sprintf("[ ] %s", label)
}
func RenderStep(step, total int, title string) string {
return fmt.Sprintf("%s %s",
StepStyle.Render(fmt.Sprintf("STEP %d/%d", step, total)),
SubHeaderStyle.Render(title),
)
}

View File

@@ -293,39 +293,78 @@ func ConfigureNpmMirror() error {
// InstallNode 下载并安装 Node.js MSI
func InstallNode() error {
// 0. Check existing installation
_, ok := CheckNode()
if ok {
// Version is >= 22, skip install
return nil
}
// If version exists but is old (ok=false, verStr not empty), we update.
// If verStr is empty, we install fresh.
msiUrl := "https://nodejs.org/dist/v24.13.0/node-v24.13.0-x64.msi"
tempDir := os.TempDir()
msiPath := filepath.Join(tempDir, "node-v24.13.0-x64.msi")
// 1. 下载 MSI
fmt.Printf("正在下载 Node.js: %s\n", msiUrl)
resp, err := http.Get(msiUrl)
if err != nil {
return fmt.Errorf("下载失败: %v", err)
}
defer resp.Body.Close()
// Check if file already exists and has size (basic cache check)
if info, err := os.Stat(msiPath); err == nil && info.Size() > 10000000 {
// Assume valid if > 10MB, skip download
} else {
fmt.Printf("正在下载 Node.js: %s\n", msiUrl)
resp, err := http.Get(msiUrl)
if err != nil {
return fmt.Errorf("下载失败: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("下载失败,状态码: %d", resp.StatusCode)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("下载失败,状态码: %d", resp.StatusCode)
}
out, err := os.Create(msiPath)
if err != nil {
return fmt.Errorf("创建文件失败: %v", err)
}
defer out.Close()
out, err := os.Create(msiPath)
if err != nil {
return fmt.Errorf("创建文件失败: %v", err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("写入文件失败: %v", err)
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("写入文件失败: %v", err)
}
}
// 2. 安装 MSI (静默安装)
// msiexec /i <file> /qn
fmt.Println("正在安装 Node.js...")
installCmd := exec.Command("msiexec", "/i", msiPath, "/qn")
if err := installCmd.Run(); err != nil {
return fmt.Errorf("安装失败: %v", err)
// Error 1619: This installation package could not be opened.
// Error 1603: Fatal error during installation.
// Error 1618: Another installation is already in progress.
fmt.Println("正在安装 Node.js (可能需要管理员权限)...")
// Retry loop for 1618 (Another installation in progress)
for i := 0; i < 3; i++ {
installCmd := exec.Command("msiexec", "/i", msiPath, "/qn")
output, err := installCmd.CombinedOutput()
if err == nil {
break
}
outStr := string(output)
if strings.Contains(outStr, "1618") {
time.Sleep(5 * time.Second)
continue
}
// If error is 1619, maybe file is corrupted, delete and retry download?
// For now just return error but with better message
if strings.Contains(outStr, "1619") {
return fmt.Errorf("安装包损坏或无法打开 (Error 1619). 请尝试手动下载安装: %s", msiUrl)
}
if i == 2 {
return fmt.Errorf("安装失败: %v, Output: %s", err, outStr)
}
time.Sleep(2 * time.Second)
}
// 3. 刷新环境变量 (当前进程无法立即生效,但后续调用 getNpmPath 会尝试绝对路径)

File diff suppressed because it is too large Load Diff