From 7321951d05836cffa649156f8028a81026f7459c Mon Sep 17 00:00:00 2001 From: titor Date: Mon, 6 Apr 2026 05:11:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=A8=A1=E5=9D=973?= =?UTF-8?q?=20-=20TUI=E7=BF=BB=E8=AF=91=E6=98=BE=E7=A4=BA=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 7 ++++--- internal/tui/app.go | 29 +++++++++++++++++++++++------ memory.md | 32 ++++++++++++++++++++++++++++---- taolun.md | 19 +++++++++++++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/changelog.md b/changelog.md index 69d05cc..2411563 100644 --- a/changelog.md +++ b/changelog.md @@ -27,13 +27,14 @@ - [x] TUI界面模块拆分计划 ✅ 已制定 - [x] 模块1: TUI框架搭建 ✅ 已完成 - [x] 模块2: 输入组件 ✅ 已完成 +- [x] 模块3: 翻译显示区 ✅ 已完成 ## TUI界面实现计划 (v0.6.0) | 步骤 | 模块 | 内容 | 状态 | |------|------|------|------| | 1 | TUI框架搭建 | bubbletea基础App结构、运行循环 | ✅ 已完成 | | 2 | 输入组件 | 文本输入框、光标、基础编辑 | ✅ 已完成 | -| 3 | 翻译显示区 | 结果展示、格式化、滚动 | ⏳ 待实现 | +| 3 | 翻译显示区 | 结果展示、格式化、滚动 | ✅ 已完成 | | 4 | 状态栏/主题 | 底部状态栏、语言选择、主题配色 | ⏳ 待实现 | | 5 | 快捷键系统 | 退出、清空、切换语言等 | ⏳ 待实现 | | 6 | 集成翻译 | 对接现有Translator、加载动画 | ⏳ 待实现 | @@ -50,7 +51,7 @@ **变更内容**: - ✅ 模块1: TUI框架搭建 - 添加bubbletea依赖,实现基础App结构 - ✅ 模块2: 输入组件 - textinput组件、基础输入处理 -- ⏳ 模块3: 翻译显示区 - 待实现 +- ✅ 模块3: 翻译显示区 - 结果显示区域、样式定义 - ⏳ 模块4: 状态栏/主题 - 待实现 - ⏳ 模块5: 快捷键系统 - 待实现 - ⏳ 模块6: 集成翻译 - 待实现 @@ -65,7 +66,7 @@ - [TUI界面模块拆分计划](taolun.md#2026-04-06-1000-版本-060---tui界面模块拆分计划) **下一步**: -- 实现模块3: 翻译显示区 +- 实现模块4: 状态栏/主题 --- diff --git a/internal/tui/app.go b/internal/tui/app.go index a6feeb6..aebf394 100644 --- a/internal/tui/app.go +++ b/internal/tui/app.go @@ -12,15 +12,21 @@ type model struct { config *config.Config translator *translator.Translator textInput textinput.Model + result string } var ( + headerStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#00D9FF")). + Bold(true) inputStyle = lipgloss.NewStyle(). Foreground(lipgloss.Color("#FAFAFA")). Background(lipgloss.Color("#1A1A2E")) - focusedStyle = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#FAFAFA")). - Background(lipgloss.Color("#16213E")) + resultStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#98FB98")). + Background(lipgloss.Color("#0D1B2A")) + helpStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#888888")) ) func NewApp(cfg *config.Config, t *translator.Translator) *tea.Program { @@ -59,9 +65,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m model) View() string { + resultBox := m.renderResult() + helpText := helpStyle.Render("\n 按 Ctrl+C 退出 | Enter 翻译") + return "\n" + - " YOYO翻译\n" + - " ─────────────────────\n\n" + + " " + headerStyle.Render("YOYO翻译") + "\n" + + " " + lipgloss.NewStyle().Foreground(lipgloss.Color("#00D9FF")).Render("─────────────────────") + "\n\n" + " " + m.textInput.View() + "\n\n" + - " 按 Ctrl+C 退出\n" + resultBox + + helpText +} + +func (m model) renderResult() string { + if m.result == "" { + return " " + helpStyle.Render("翻译结果将显示在这里...") + "\n" + } + return " " + resultStyle.Render(m.result) + "\n" } diff --git a/memory.md b/memory.md index 795b761..a45bd23 100644 --- a/memory.md +++ b/memory.md @@ -612,9 +612,33 @@ m.textInput.View() ### Lipgloss样式定义 ```go -var style = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#FAFAFA")). - Background(lipgloss.Color("#1A1A2E")) +var ( + headerStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#00D9FF")). + Bold(true) + resultStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#98FB98")). + Background(lipgloss.Color("#0D1B2A")) + helpStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#888888")) +) +``` -ti.TextStyle = style // 应用到textinput +### 多区域View渲染 +```go +func (m model) View() string { + return "\n" + + " " + headerStyle.Render("YOYO翻译") + "\n" + + " " + divider + "\n\n" + + " " + m.textInput.View() + "\n\n" + + m.renderResult() + + helpText +} + +func (m model) renderResult() string { + if m.result == "" { + return " " + helpStyle.Render("翻译结果将显示在这里...") + "\n" + } + return " " + resultStyle.Render(m.result) + "\n" +} ``` \ No newline at end of file diff --git a/taolun.md b/taolun.md index 4dfe52c..c7a2156 100644 --- a/taolun.md +++ b/taolun.md @@ -479,5 +479,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { **下一步**: 实现模块3: 翻译显示区 +**关联文档**: +- [changelog.md#0.6.0](changelog.md#060) + +--- + +### [2026-04-06 11:30] 版本 0.6.0 - 模块3: 翻译显示区 (已完成) +**原因**: 添加翻译结果显示区域 +**分析**: +- 需要定义结果展示区域 +- 需要为不同区域定义不同样式 + +**解决方案**: +1. 添加result字段到model +2. 定义headerStyle、resultStyle、helpStyle +3. 实现renderResult()辅助方法 +4. View中组合各个区域 + +**下一步**: 实现模块4: 状态栏/主题 + **关联文档**: - [changelog.md#0.6.0](changelog.md#060) \ No newline at end of file