feat: 实现模块3 - TUI翻译显示区

This commit is contained in:
2026-04-06 05:11:23 +08:00
parent 6f872ff285
commit 7321951d05
4 changed files with 74 additions and 13 deletions

View File

@@ -27,13 +27,14 @@
- [x] TUI界面模块拆分计划 ✅ 已制定 - [x] TUI界面模块拆分计划 ✅ 已制定
- [x] 模块1: TUI框架搭建 ✅ 已完成 - [x] 模块1: TUI框架搭建 ✅ 已完成
- [x] 模块2: 输入组件 ✅ 已完成 - [x] 模块2: 输入组件 ✅ 已完成
- [x] 模块3: 翻译显示区 ✅ 已完成
## TUI界面实现计划 (v0.6.0) ## TUI界面实现计划 (v0.6.0)
| 步骤 | 模块 | 内容 | 状态 | | 步骤 | 模块 | 内容 | 状态 |
|------|------|------|------| |------|------|------|------|
| 1 | TUI框架搭建 | bubbletea基础App结构、运行循环 | ✅ 已完成 | | 1 | TUI框架搭建 | bubbletea基础App结构、运行循环 | ✅ 已完成 |
| 2 | 输入组件 | 文本输入框、光标、基础编辑 | ✅ 已完成 | | 2 | 输入组件 | 文本输入框、光标、基础编辑 | ✅ 已完成 |
| 3 | 翻译显示区 | 结果展示、格式化、滚动 | ⏳ 待实现 | | 3 | 翻译显示区 | 结果展示、格式化、滚动 | ✅ 已完成 |
| 4 | 状态栏/主题 | 底部状态栏、语言选择、主题配色 | ⏳ 待实现 | | 4 | 状态栏/主题 | 底部状态栏、语言选择、主题配色 | ⏳ 待实现 |
| 5 | 快捷键系统 | 退出、清空、切换语言等 | ⏳ 待实现 | | 5 | 快捷键系统 | 退出、清空、切换语言等 | ⏳ 待实现 |
| 6 | 集成翻译 | 对接现有Translator、加载动画 | ⏳ 待实现 | | 6 | 集成翻译 | 对接现有Translator、加载动画 | ⏳ 待实现 |
@@ -50,7 +51,7 @@
**变更内容**: **变更内容**:
- ✅ 模块1: TUI框架搭建 - 添加bubbletea依赖实现基础App结构 - ✅ 模块1: TUI框架搭建 - 添加bubbletea依赖实现基础App结构
- ✅ 模块2: 输入组件 - textinput组件、基础输入处理 - ✅ 模块2: 输入组件 - textinput组件、基础输入处理
- 模块3: 翻译显示区 - 待实现 - 模块3: 翻译显示区 - 结果显示区域、样式定义
- ⏳ 模块4: 状态栏/主题 - 待实现 - ⏳ 模块4: 状态栏/主题 - 待实现
- ⏳ 模块5: 快捷键系统 - 待实现 - ⏳ 模块5: 快捷键系统 - 待实现
- ⏳ 模块6: 集成翻译 - 待实现 - ⏳ 模块6: 集成翻译 - 待实现
@@ -65,7 +66,7 @@
- [TUI界面模块拆分计划](taolun.md#2026-04-06-1000-版本-060---tui界面模块拆分计划) - [TUI界面模块拆分计划](taolun.md#2026-04-06-1000-版本-060---tui界面模块拆分计划)
**下一步**: **下一步**:
- 实现模块3: 翻译显示区 - 实现模块4: 状态栏/主题
--- ---

View File

@@ -12,15 +12,21 @@ type model struct {
config *config.Config config *config.Config
translator *translator.Translator translator *translator.Translator
textInput textinput.Model textInput textinput.Model
result string
} }
var ( var (
headerStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#00D9FF")).
Bold(true)
inputStyle = lipgloss.NewStyle(). inputStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")). Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#1A1A2E")) Background(lipgloss.Color("#1A1A2E"))
focusedStyle = lipgloss.NewStyle(). resultStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")). Foreground(lipgloss.Color("#98FB98")).
Background(lipgloss.Color("#16213E")) Background(lipgloss.Color("#0D1B2A"))
helpStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#888888"))
) )
func NewApp(cfg *config.Config, t *translator.Translator) *tea.Program { 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 { func (m model) View() string {
resultBox := m.renderResult()
helpText := helpStyle.Render("\n 按 Ctrl+C 退出 | Enter 翻译")
return "\n" + return "\n" +
" YOYO翻译\n" + " " + headerStyle.Render("YOYO翻译") + "\n" +
" ─────────────────────\n\n" + " " + lipgloss.NewStyle().Foreground(lipgloss.Color("#00D9FF")).Render("─────────────────────") + "\n\n" +
" " + m.textInput.View() + "\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"
} }

View File

@@ -612,9 +612,33 @@ m.textInput.View()
### Lipgloss样式定义 ### Lipgloss样式定义
```go ```go
var style = lipgloss.NewStyle(). var (
Foreground(lipgloss.Color("#FAFAFA")). headerStyle = lipgloss.NewStyle().
Background(lipgloss.Color("#1A1A2E")) Foreground(lipgloss.Color("#00D9FF")).
Bold(true)
ti.TextStyle = style // 应用到textinput resultStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#98FB98")).
Background(lipgloss.Color("#0D1B2A"))
helpStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#888888"))
)
```
### 多区域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"
}
``` ```

View File

@@ -481,3 +481,22 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
**关联文档**: **关联文档**:
- [changelog.md#0.6.0](changelog.md#060) - [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)