feat: Logo模块化与渐变色统一

- 修复 --help/-h/-?/--version 在交互模式下无响应的问题
- 新增 internal/logo/logo.go 统一管理logo展示
- 新增 build.sh 自动注入git版本号
- TUI头部与CLI使用统一logo模块
- 移除TUI头部的 [Ctrl+C 退出] 显示
- 统一版本号格式: ( v1.1.1-dirty )
This commit is contained in:
2026-04-08 01:08:47 +08:00
parent 9acbc834a4
commit a9b7a69224
7 changed files with 237 additions and 67 deletions

79
internal/logo/logo.go Normal file
View File

@@ -0,0 +1,79 @@
package logo
import (
"fmt"
"strings"
)
var version string
func SetVersion(v string) { version = v }
func GetVersion() string { return version }
func GradientText(text string, startColor, endColor string) string {
startR, startG, startB := parseHexColor(startColor)
endR, endG, endB := parseHexColor(endColor)
lines := strings.Split(text, "\n")
if len(lines) == 0 {
return text
}
var result []string
for _, line := range lines {
if len(line) == 0 {
result = append(result, line)
continue
}
var coloredLine string
for i, char := range line {
ratio := float64(i) / float64(len(line)-1)
r := int(float64(startR) + float64(endR-startR)*ratio)
g := int(float64(startG) + float64(endG-startG)*ratio)
b := int(float64(startB) + float64(endB-startB)*ratio)
coloredLine += fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, string(char))
}
result = append(result, coloredLine)
}
return strings.Join(result, "\n")
}
func parseHexColor(hex string) (int, int, int) {
hex = strings.TrimPrefix(hex, "#")
if len(hex) != 6 {
return 0, 0, 0
}
var r, g, b int
fmt.Sscanf(hex, "%02x%02x%02x", &r, &g, &b)
return r, g, b
}
func GetLogoPattern() string {
return " _ _ _____ _____ " + "\n" +
"( \\/ ( _ ( _ ) " + "\n" +
" \\ / )(_)( )(_)( " + "\n" +
" (__)(_____(_____("
}
func GetVersionSuffix() string {
v := GetVersion()
if v != "" {
return " (" + v + " )"
}
return " ( )"
}
func PrintLogoWithVersion() {
logoPattern := " _ _ _____ _____\n" +
"( \\/ ( _ ( _ )\n" +
" \\ / )(_)( )(_)( \n" +
" (__)(_____(_____("
patternWithVersion := logoPattern + GetVersionSuffix()
colored := GradientText(patternWithVersion, "#B413DC", "#00C8C8")
fmt.Println(colored)
}

View File

@@ -13,17 +13,12 @@ import (
"charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/titor/fanyi/internal/config"
"github.com/titor/fanyi/internal/content"
"github.com/titor/fanyi/internal/logo"
"github.com/titor/fanyi/internal/translator"
)
var supportedLangs = []string{"zh-CN", "en-US", "ja", "ko", "zh-TW", "es", "fr", "de"}
var logoPattern = "l_ _ _____ _____ " + "\n" +
"( \\/ ( _ ( _ ) " + "\n" +
" \\ / )(_)( )(_)( " + "\n" +
" (__)(_____(_____((() [v" + content.Version + "]"
type translateMsg struct {
result string
tokens int
@@ -312,59 +307,18 @@ func (m model) View() tea.View {
return v
}
func gradientText(text string, startColor, endColor string) string {
startR, startG, startB := parseHexColor(startColor)
endR, endG, endB := parseHexColor(endColor)
lines := strings.Split(text, "\n")
if len(lines) == 0 {
return text
}
var result []string
for _, line := range lines {
if len(line) == 0 {
result = append(result, line)
continue
}
var coloredLine string
for i, char := range line {
ratio := float64(i) / float64(len(line)-1)
r := int(float64(startR) + float64(endR-startR)*ratio)
g := int(float64(startG) + float64(endG-startG)*ratio)
b := int(float64(startB) + float64(endB-startB)*ratio)
coloredLine += fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, string(char))
}
result = append(result, coloredLine)
}
return strings.Join(result, "\n")
}
func parseHexColor(hex string) (int, int, int) {
hex = strings.TrimPrefix(hex, "#")
r, _ := strconv.ParseInt(hex[0:2], 16, 64)
g, _ := strconv.ParseInt(hex[2:4], 16, 64)
b, _ := strconv.ParseInt(hex[4:6], 16, 64)
return int(r), int(g), int(b)
}
func (m model) renderHeader() string {
title := gradientText(logoPattern, "#8B5CF6", "#EC4899")
title := logo.GradientText(logo.GetLogoPattern(), "#B413DC", "#00C8C8")
titleWithVersion := title + logo.GetVersionSuffix()
width := m.width - 4
if width < 20 {
width = 60
}
right := lipgloss.NewStyle().
Foreground(lipgloss.Color("#6B7280")).
Render("[Ctrl+C 退出]")
return lipgloss.NewStyle().
Width(width).
Render(title + strings.Repeat(" ", width-29-len(right)-1) + right)
Render(titleWithVersion)
}
func (m model) renderInputArea() string {