-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.go
227 lines (191 loc) · 5.06 KB
/
settings.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
package settings
import (
"database/sql"
"fmt"
"log"
"strconv"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/tearingItUp786/chatgpt-tui/util"
)
const (
viewMode = -1
modelMode = iota
maxTokensMode
frequencyMode
)
// set up a text input model that only renders if it is not viewMode
// based on the mode we are in, that is the column we will update
// in the sql database. After a successful save, we are going to
// go back to view mode.
type Model struct {
terminalWidth int
isFocused bool
mode int
settings Settings
textInput textinput.Model
list lipgloss.Style
}
var settingsService *SettingsService
var listHeader = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
MarginLeft(2)
var listItemHeading = lipgloss.NewStyle().
PaddingLeft(2).
Foreground(lipgloss.Color(util.Pink100))
var listItemSpan = lipgloss.NewStyle().
Foreground(lipgloss.Color("#fff"))
func (m Model) Init() tea.Cmd {
return nil
}
func listItemRenderer(heading string, value string) string {
headingEl := listItemHeading.Render
spanEl := listItemSpan.Render
return headingEl(" "+heading, spanEl(value))
}
func (m Model) View() string {
editForm := ""
if m.mode != viewMode {
editForm = m.textInput.View()
}
return m.list.Render(
lipgloss.JoinVertical(lipgloss.Left,
listHeader.Render("Settings"),
lipgloss.NewStyle().Height(5).Render(
lipgloss.JoinVertical(lipgloss.Left,
listItemRenderer("model", m.settings.Model),
listItemRenderer("frequency", fmt.Sprint(m.settings.Frequency)),
listItemRenderer("max_tokens", fmt.Sprint((m.settings.MaxTokens))),
),
),
editForm,
),
)
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case util.OurWindowResize:
width := m.terminalWidth - msg.Width - 5
m.list.Width(width)
case util.FocusEvent:
m.isFocused = msg.IsFocused
m.mode = viewMode
borderColor := util.NormalTabBorderColor
if m.isFocused {
borderColor = util.ActiveTabBorderColor
}
m.list.BorderForeground(borderColor)
return m, nil
case tea.WindowSizeMsg:
m.terminalWidth = msg.Width
return m, nil
case tea.KeyMsg:
// in order to do proper event bubbling, we don't actually want to handle
// any keyboard events, unless we're the focused pane.
if !m.isFocused {
return m, nil
}
if m.mode == viewMode {
cmd = m.handleViewMode(msg)
cmds = append(cmds, cmd)
} else {
cmd = m.handleEditMode(msg)
cmds = append(cmds, cmd)
}
}
return m, tea.Batch(cmds...)
}
func (m *Model) handleViewMode(msg tea.KeyMsg) tea.Cmd {
var cmd tea.Cmd
switch msg.Type {
case tea.KeyRunes:
key := string(msg.Runes)
if key == "m" || key == "f" || key == "t" {
ti := textinput.New()
ti.PromptStyle = lipgloss.NewStyle().PaddingLeft(2)
m.textInput = ti
switch key {
case "m":
m.mode = modelMode
m.textInput.CharLimit = 100
m.textInput.Placeholder = "Enter model"
case "f":
m.mode = frequencyMode
m.textInput.Placeholder = "Enter Frequency Number"
// the validate function will not allow us to type in any characters
// that don't pass validation. So here, we are ensuring that we do not allow
// the user to type in any non numeric characters. We can extend this further,
// maybe...
m.textInput.Validate = func(str string) error {
if _, err := strconv.ParseFloat(str, 64); err == nil {
log.Printf("'%s' is a floating-point number.\n", str)
} else {
log.Printf("'%s' is not a floating-point number.\n", str)
return err
}
return nil
}
case "t":
m.textInput.Placeholder = "Enter Max Tokens"
m.mode = maxTokensMode
}
m.textInput.Focus()
}
}
return cmd
}
func (m *Model) handleEditMode(msg tea.KeyMsg) tea.Cmd {
var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
if msg.String() == "enter" {
inputValue := m.textInput.Value()
if inputValue == "" {
return cmd
}
switch m.mode {
case modelMode:
m.settings.Model = inputValue
case frequencyMode:
newFreq, err := strconv.Atoi(inputValue)
if err != nil {
cmd = util.MakeErrorMsg("Invalid frequency")
}
m.settings.Frequency = newFreq
case maxTokensMode:
newTokens, err := strconv.Atoi(inputValue)
if err != nil {
cmd = util.MakeErrorMsg("Invalid Tokens")
}
m.settings.MaxTokens = newTokens
}
newSettings, err := settingsService.UpdateSettings(m.settings)
if err != nil {
return util.MakeErrorMsg(err.Error())
}
m.settings = newSettings
m.mode = viewMode
cmd = MakeSettingsUpdateMsg(m.settings)
}
return cmd
}
func New(db *sql.DB) Model {
settingsService = NewSettingsService(db)
settings, err := settingsService.GetSettings()
if err != nil {
panic(err)
}
list := lipgloss.NewStyle().
Border(lipgloss.ThickBorder(), true).
BorderForeground(util.NormalTabBorderColor).
Height(8)
return Model{
terminalWidth: 20,
mode: viewMode,
settings: settings,
list: list,
}
}