85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
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"
|
|
)
|
|
|
|
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"))
|
|
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 {
|
|
ti := textinput.New()
|
|
ti.Placeholder = "输入要翻译的文本..."
|
|
ti.Focus()
|
|
ti.Prompt = "> "
|
|
ti.TextStyle = inputStyle
|
|
|
|
return tea.NewProgram(model{
|
|
config: cfg,
|
|
translator: t,
|
|
textInput: ti,
|
|
})
|
|
}
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
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 {
|
|
resultBox := m.renderResult()
|
|
helpText := helpStyle.Render("\n 按 Ctrl+C 退出 | Enter 翻译")
|
|
|
|
return "\n" +
|
|
" " + headerStyle.Render("YOYO翻译") + "\n" +
|
|
" " + lipgloss.NewStyle().Foreground(lipgloss.Color("#00D9FF")).Render("─────────────────────") + "\n\n" +
|
|
" " + m.textInput.View() + "\n\n" +
|
|
resultBox +
|
|
helpText
|
|
}
|
|
|
|
func (m model) renderResult() string {
|
|
if m.result == "" {
|
|
return " " + helpStyle.Render("翻译结果将显示在这里...") + "\n"
|
|
}
|
|
return " " + resultStyle.Render(m.result) + "\n"
|
|
}
|