feat: 实现模块2 - TUI输入组件 (textinput)

This commit is contained in:
2026-04-06 05:10:00 +08:00
parent 1787464f52
commit 6f872ff285
4 changed files with 112 additions and 6 deletions

View File

@@ -1,7 +1,9 @@
package tui
import (
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/titor/fanyi/internal/config"
"github.com/titor/fanyi/internal/translator"
)
@@ -9,12 +11,29 @@ import (
type model struct {
config *config.Config
translator *translator.Translator
textInput textinput.Model
}
var (
inputStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#1A1A2E"))
focusedStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#16213E"))
)
func NewApp(cfg *config.Config, t *translator.Translator) *tea.Program {
ti := textinput.New()
ti.Placeholder = "输入要翻译的文本..."
ti.Focus()
ti.Prompt = "> "
ti.TextStyle = inputStyle
return tea.NewProgram(model{
config: cfg,
translator: t,
textInput: ti,
})
}
@@ -23,9 +42,26 @@ func (m model) Init() tea.Cmd {
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
// 回车键处理,后续模块会添加翻译逻辑
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
}
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m model) View() string {
return "YOYO翻译 TUI\n\n(基础框架搭建中...)\n"
return "\n" +
" YOYO翻译\n" +
" ─────────────────────\n\n" +
" " + m.textInput.View() + "\n\n" +
" 按 Ctrl+C 退出\n"
}