-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
242 lines (217 loc) · 5.65 KB
/
settings.go
File metadata and controls
242 lines (217 loc) · 5.65 KB
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
236
237
238
239
240
241
242
package main
import (
"fmt"
"strings"
"charm.land/bubbles/v2/key"
"charm.land/lipgloss/v2"
tea "charm.land/bubbletea/v2"
)
type settingsScreen struct {
cursor int
saved bool
dirty bool
errMsg string
diskState Settings // snapshot of settings on disk when screen was created
}
func newSettingsScreen() settingsScreen {
disk := LoadSettings()
return settingsScreen{
diskState: disk,
dirty: !settingsEqual(appSettings, disk),
}
}
func (s settingsScreen) init() tea.Cmd { return nil }
func (s settingsScreen) update(msg tea.Msg) (screen, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "up", "k":
if s.cursor > 0 {
s.cursor--
}
case "down", "j":
if s.cursor < len(settingDefs)-1 {
s.cursor++
}
case "enter", "space", "right", "l":
s.cycleForward()
case "left", "h":
s.cycleBackward()
case "s":
if err := SaveSettings(appSettings); err != nil {
s.errMsg = "Save failed: " + err.Error()
} else {
s.saved = true
s.dirty = false
s.diskState = appSettings
s.errMsg = ""
}
case "r":
appSettings = DefaultSettings()
s.saved = false
s.dirty = !settingsEqual(appSettings, s.diskState)
s.errMsg = ""
}
case tea.MouseClickMsg:
if msg.Button == tea.MouseLeft {
contentY := msg.Y - 9 // header + tab + title offset
if contentY >= 0 && contentY < len(settingDefs) {
s.cursor = contentY
s.cycleForward()
}
}
}
return s, nil
}
func (s *settingsScreen) cycleForward() {
def := settingDefs[s.cursor]
switch def.stype {
case settingToggle:
cur := appSettings.getValue(def.key)
if cur == "true" {
appSettings.setValue(def.key, "false")
} else {
appSettings.setValue(def.key, "true")
}
case settingChoice:
cur := appSettings.getValue(def.key)
idx := 0
for i, c := range def.choices {
if c == cur {
idx = i
break
}
}
idx = (idx + 1) % len(def.choices)
appSettings.setValue(def.key, def.choices[idx])
}
s.saved = false
s.dirty = !settingsEqual(appSettings, s.diskState)
cache.invalidate()
}
func (s *settingsScreen) cycleBackward() {
def := settingDefs[s.cursor]
switch def.stype {
case settingToggle:
s.cycleForward() // toggle is the same either direction
return
case settingChoice:
cur := appSettings.getValue(def.key)
idx := 0
for i, c := range def.choices {
if c == cur {
idx = i
break
}
}
idx--
if idx < 0 {
idx = len(def.choices) - 1
}
appSettings.setValue(def.key, def.choices[idx])
}
s.saved = false
s.dirty = !settingsEqual(appSettings, s.diskState)
cache.invalidate()
}
func (s settingsScreen) view(width, height int) string {
panelWidth := width - 4
// Build settings list.
var lines []string
for i, def := range settingDefs {
cursor := cursorBlankStr
labelStyle := itemStyle
if i == s.cursor {
cursor = cursorStr
labelStyle = itemActiveStyle
}
val := appSettings.getValue(def.key)
valDisplay := renderSettingValue(def, val)
label := labelStyle.Render(fmt.Sprintf("%-24s", def.label))
desc := itemDescStyle.Render(def.desc)
lines = append(lines, fmt.Sprintf("%s%s %s %s", cursor, label, valDisplay, desc))
}
// Settings panel.
title := "Settings"
if s.dirty {
title = "Settings (unsaved)"
}
content := strings.Join(lines, "\n")
panel := renderTitledPanel(title, content, panelWidth, len(lines), accent)
// Detail panel for focused setting.
detail := s.renderDetailPanel(panelWidth, height > 0 && height < 28)
// Status line.
var status string
if s.errMsg != "" {
status = " " + errorStyle.Render(s.errMsg)
} else if s.saved {
status = " " + successStyle.Render("Settings saved!")
}
// Config path.
configLine := " " + helpStyle.Render("Config: "+configPath())
var b strings.Builder
b.WriteString(panel + "\n")
b.WriteString(detail + "\n")
b.WriteString(configLine + "\n")
if status != "" {
b.WriteString(status + "\n")
}
return b.String()
}
func (s settingsScreen) helpKeys() []key.Binding {
return []key.Binding{keyUp, keyDown, keyCycle, keySave, keyReset, keyTabs}
}
func renderSettingValue(def settingDef, val string) string {
switch def.stype {
case settingToggle:
if val == "true" {
return lipgloss.NewStyle().Bold(true).Foreground(success).Render("● ON ")
}
return lipgloss.NewStyle().Foreground(dim).Render("○ OFF")
case settingChoice:
matched := false
var parts []string
for _, c := range def.choices {
display := def.choiceLabel(c)
if c == val {
matched = true
parts = append(parts, lipgloss.NewStyle().Bold(true).Foreground(accent).Render("["+display+"]"))
} else {
parts = append(parts, helpStyle.Render(" "+display+" "))
}
}
if !matched && val != "" {
parts = append(parts, lipgloss.NewStyle().Bold(true).Foreground(warning).Render("[v"+val+"]"))
}
return strings.Join(parts, "")
}
return val
}
func (s settingsScreen) renderDetailPanel(panelWidth int, compact bool) string {
def := settingDefs[s.cursor]
val := appSettings.getValue(def.key)
var lines []string
lines = append(lines, infoStyle.Render("Current: "+def.choiceLabel(valOrOnOff(def, val))))
if hint := strings.TrimSpace(def.currentHint(val)); hint != "" {
lines = append(lines, itemStyle.Render(hint))
}
if !compact {
if detail := strings.TrimSpace(def.detail); detail != "" {
lines = append(lines, "")
for _, dl := range strings.Split(detail, "\n") {
lines = append(lines, helpStyle.Render(dl))
}
}
}
content := strings.Join(lines, "\n")
return renderTitledPanel(def.label, content, panelWidth, len(lines), secondary)
}
func valOrOnOff(def settingDef, val string) string {
if def.stype == settingToggle {
if val == "true" {
return "on"
}
return "off"
}
return val
}