-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.go
235 lines (195 loc) · 5.14 KB
/
tui.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var tui *tea.Program
type (
errMsg error
logMsg Log
)
type LogType int
const (
Info LogType = iota
Warning
Error
)
const (
spacebar = " "
help = "Ctrl+x • Copy last msg | ctrl+s • Toggle mouse | ctrl+p • Multiline prompt | @cb • Paste clipboard"
)
type Log struct {
Msg string
Type LogType
}
type model struct {
viewport viewport.Model
textarea textarea.Model
spinner spinner.Model
lastMsg string
waiting bool
mouse bool
messages []string
err error
}
type KeyMap struct {
PageDown key.Binding
PageUp key.Binding
HalfPageUp key.Binding
HalfPageDown key.Binding
Down key.Binding
Up key.Binding
}
var (
BoldStyle = lipgloss.NewStyle().Bold(true)
InfoLogStyle = BoldStyle.Copy().
Foreground(lipgloss.Color("#a6da95"))
WarningLogStyle = BoldStyle.Copy().
Foreground(lipgloss.Color("#eed49f"))
ErrorLogStyle = BoldStyle.Copy().
Foreground(lipgloss.Color("#ed8796"))
UserStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#c6a0f6"))
FadedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#999999"))
HelpStyle = FadedStyle.Copy().Italic(true).Padding(0, 1).Margin(0, 1)
ContainerStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("#c6a0f6")).Padding(1).Margin(1)
)
func initialModel() model {
ta := textarea.New()
ta.Placeholder = "Talk with Clyde here"
ta.Focus()
ta.Prompt = UserStyle.Render("❯ ")
ta.CharLimit = 2000
ta.SetWidth(30)
ta.SetHeight(1)
ta.FocusedStyle.CursorLine = ta.FocusedStyle.CursorLine.Copy().UnsetBackground()
ta.ShowLineNumbers = false
ta.KeyMap.InsertNewline.SetEnabled(false)
vp := viewport.New(30, 3)
vp.SetContent("Type a prompt and press Enter to ask Clyde AI.")
vp.MouseWheelEnabled = true
vp.KeyMap = viewport.KeyMap(KeyMap{
Up: key.NewBinding(
key.WithKeys("up"),
key.WithHelp("↑", "up"),
),
Down: key.NewBinding(
key.WithKeys("down"),
key.WithHelp("↓", "down"),
),
})
sp := spinner.New()
sp.Spinner = spinner.Dot
sp.Style = UserStyle
return model{
textarea: ta,
viewport: vp,
spinner: sp,
messages: []string{},
lastMsg: "",
err: nil,
waiting: false,
mouse: true,
}
}
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
spCmd tea.Cmd
)
m.textarea, tiCmd = m.textarea.Update(msg)
m.viewport, vpCmd = m.viewport.Update(msg)
m.spinner, spCmd = m.spinner.Update(msg)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.viewport.Height = msg.Height - 10
m.viewport.Width = msg.Width - 6
m.textarea.SetWidth(msg.Width - 6)
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlS:
if m.mouse {
m.mouse = false
return m, tea.Sequence(tea.DisableMouse, getLogCmd("Disabled mouse scroll/clicks", Info))
} else {
m.mouse = true
return m, tea.Sequence(tea.EnableMouseCellMotion, getLogCmd("Enabled mouse scroll/clicks", Info))
}
case tea.KeyCtrlX:
WriteClipboard(m.lastMsg)
return m, getLogCmd(fmt.Sprintf("Copied %d characters!", len(m.lastMsg)), Info)
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
case tea.KeyEnter:
prompt := m.textarea.Value()
go AskClyde(prompt, "")
m.waiting = true
m.messages = append(m.messages, UserStyle.Render(prompt))
m.viewport.SetContent(strings.Join(m.messages, "\n"))
m.textarea.Reset()
m.viewport.GotoBottom()
return m, tea.Batch(tiCmd, vpCmd, m.spinner.Tick)
}
case errMsg:
m.err = msg
return m, nil
case DiscordMessage:
parsed, response := FormatClydeResponse(msg.Content)
m.lastMsg = parsed
m.waiting = false
m.messages = append(m.messages, response)
m.viewport.SetContent(strings.Join(m.messages, "\n"))
m.viewport.GotoBottom()
return m, nil
case logMsg:
switch msg.Type {
case Info:
m.messages = append(m.messages, InfoLogStyle.Render(msg.Msg))
case Warning:
m.messages = append(m.messages, WarningLogStyle.Render(msg.Msg))
case Error:
m.messages = append(m.messages, ErrorLogStyle.Render(msg.Msg))
}
m.viewport.SetContent(strings.Join(m.messages, "\n"))
m.viewport.GotoBottom()
return m, nil
}
return m, tea.Batch(tiCmd, vpCmd, spCmd)
}
func (m model) View() string {
var bottomView string
if m.waiting {
bottomView = m.spinner.View() + FadedStyle.Render(" Waiting for Clyde...")
} else {
bottomView = m.textarea.View()
}
view := ContainerStyle.Render(
fmt.Sprintf(
"%s\n\n%s",
m.viewport.View(),
lipgloss.NewStyle().Width(m.viewport.Width).Render(bottomView),
),
)
return view + "\n" + HelpStyle.Render(help)
}
func RunTUI() {
var m = initialModel()
tui = tea.NewProgram(m, tea.WithMouseCellMotion())
if _, err := tui.Run(); err != nil {
panic(err)
}
}
func getLogCmd(msg string, msgType LogType) tea.Cmd {
return func() tea.Msg {
return logMsg{Msg: msg, Type: msgType}
}
}