-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (109 loc) · 2.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fluent_ai/gemini"
"fmt"
"log"
"os"
"strings"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/joho/godotenv"
)
func init() {
erro := godotenv.Load(".env")
if erro != nil {
fmt.Println("Error loading .env file:", erro)
os.Exit(1)
}
}
func main() {
arguments := os.Args
var idiomaSaida string
if len(arguments) > 1 {
idiomaSaida = arguments[1]
} else {
idiomaSaida = "português brasileiro"
}
p := tea.NewProgram(initialModel(idiomaSaida))
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}
type (
errMsg error
)
type model struct {
viewport viewport.Model
messages []string
idiomaSaida string
textarea textarea.Model
senderStyle lipgloss.Style
err error
}
func initialModel(idioma string) model {
ta := textarea.New()
ta.Placeholder = "Insira o texto a ser traduzido aqui..."
ta.Focus()
ta.Prompt = "┃ "
ta.CharLimit = 1000
ta.SetWidth(70)
ta.SetHeight(4)
ta.FocusedStyle.CursorLine = lipgloss.NewStyle().Foreground(lipgloss.Color("5"))
ta.ShowLineNumbers = false
vp := viewport.New(70, 2)
vp.SetContent(`Digite abaixo a frase ou texto que você deseja traduzir.`)
ta.KeyMap.InsertNewline.SetEnabled(false)
return model{
textarea: ta,
messages: []string{},
idiomaSaida: idioma,
viewport: vp,
senderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("5")),
err: nil,
}
}
func (m model) Init() tea.Cmd {
return textarea.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
tiCmd tea.Cmd
vpCmd tea.Cmd
)
m.textarea, tiCmd = m.textarea.Update(msg)
m.viewport, vpCmd = m.viewport.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
fmt.Println(m.textarea.Value())
return m, tea.Quit
case tea.KeyEnter:
geminiAnswer := gemini.GeneratePrompt(m.textarea.Value(), m.idiomaSaida)
m.messages = append(m.messages, m.senderStyle.Render("\nGemini: ")+geminiAnswer)
m.viewport.SetContent(strings.Join(m.messages, "\n"))
var height int
if len(geminiAnswer) <= 100 {
height = 3
} else {
height = len(geminiAnswer) / 45
}
m.viewport.Height = height
m.textarea.Reset()
m.viewport.GotoBottom()
}
case errMsg:
m.err = msg
return m, nil
}
return m, tea.Batch(tiCmd, vpCmd)
}
func (m model) View() string {
return fmt.Sprintf(
"%s\n\n%s",
m.viewport.View(),
m.textarea.View(),
) + "\n\n"
}