-
Notifications
You must be signed in to change notification settings - Fork 0
/
customise.go
282 lines (260 loc) · 5.55 KB
/
customise.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package customise
import (
"fmt"
"log/slog"
"os"
"strings"
"github.com/rusq/rbubbles/display"
"github.com/rusq/rbubbles/filemgr"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Model struct {
Items []Item
nameColSz int
Cursor string
width int
editing bool
edittype Type
finishing bool
st display.State
err error
Style Styles
fields
}
type Styles struct {
Normal lipgloss.Style
Selected lipgloss.Style
Description lipgloss.Style
}
type fields struct {
textarea textarea.Model
textinput textinput.Model
radio RadioButton
filemgr filemgr.Model
}
var (
defStyle = lipgloss.NewStyle()
)
func NewModel(items []Item) Model {
maxNameLen := 0
for i := range items {
if l := len(items[i].Name()); maxNameLen < l {
maxNameLen = l
}
}
return Model{
Items: items,
nameColSz: maxNameLen,
Cursor: "",
Style: Styles{
Normal: defStyle,
Selected: defStyle.Copy().Foreground(lipgloss.Color("0")).Background(lipgloss.Color("7")),
Description: defStyle.Copy().Faint(true),
},
fields: fields{
textarea: textarea.New(),
textinput: textinput.New(),
radio: RadioButton{},
filemgr: filemgr.New(os.DirFS("."), ".", 0, "*"),
},
}
}
func (m Model) Init() tea.Cmd {
slog.Debug("customise.init")
return tea.Batch(m.filemgr.Init())
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if m.editing {
return m.procMsgEdit(msg)
}
return m.procMsgView(msg)
}
func (m Model) procMsgView(msg tea.Msg) (Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.st.SetMax(msg.Height)
m.filemgr.Height = 10
case tea.KeyMsg:
switch msg.String() {
case "j", "down":
m.st.Down(len(m.Items))
case "k", "up":
m.st.Up()
case "home":
m.st.Home(len(m.Items))
case "end":
m.st.End(len(m.Items), len(m.Items))
case " ":
if m.Items[m.st.Cursor].Type() != TCheckbox {
break
}
fallthrough
case "enter", "f4":
item := m.Items[m.st.Cursor]
m.edittype = item.Type()
switch m.edittype {
case TMultiline:
m.textarea.Reset()
m.textarea.SetValue(item.Value())
m.textarea.Focus()
m.editing = true
case TText:
m.textinput.Reset()
m.textinput.SetValue(item.Value())
m.textinput.Focus()
m.editing = true
case TRadio:
m.radio.SetValues(item.AllowedValues(), item.Value())
m.editing = true
case TFileExisting:
m.editing = true
m.filemgr.Focus()
cmds = append(cmds, m.filemgr.Init())
m.filemgr.Select(item.Value())
case TCheckbox:
if item.Value() == sTrue {
item.Set(sFalse)
} else {
item.Set(sTrue)
}
}
}
}
return m, tea.Batch(cmds...)
}
func (m Model) procMsgEdit(msg tea.Msg) (Model, tea.Cmd) {
OUTER:
switch msg := msg.(type) {
case filemgr.WMSelected:
if !msg.IsDir {
m.Items[m.st.Cursor].Set(msg.Filepath)
m.editing = false
}
case tea.KeyMsg:
switch msg.String() {
case "enter":
// we only process enter for non-multiline modes
switch m.edittype {
case TMultiline, TFileExisting:
break OUTER
}
fallthrough
case "esc":
m.editing = false
var val string
switch m.edittype {
case TText:
m.textinput.Blur()
val = m.textinput.Value()
case TMultiline:
m.textarea.Blur()
val = m.textarea.Value()
case TRadio:
val = m.radio.Value()
case TFileExisting:
if m.filemgr.Selected == "" {
val = m.Items[m.st.Cursor].Value()
} else {
val = m.filemgr.Selected
}
m.filemgr.Blur()
}
m.Items[m.st.Cursor].Set(val)
}
}
var cmds []tea.Cmd
var cmd tea.Cmd
switch m.edittype {
case TText:
m.textinput, cmd = m.textinput.Update(msg)
case TMultiline:
m.textarea, cmd = m.textarea.Update(msg)
case TRadio:
m.radio, cmd = m.radio.Update(msg)
case TFileExisting:
m.filemgr, cmd = m.filemgr.Update(msg)
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
if m.finishing {
return ""
}
if m.err != nil {
return m.err.Error()
}
if m.editing && m.edittype != TCheckbox {
return m.editView()
} else {
return m.selectView()
}
}
func (m Model) selectView() string {
if len(m.Items) == 0 {
return "No items to show."
}
var (
empty = strings.Repeat(" ", len(m.Cursor))
)
var buf strings.Builder
for i, item := range m.Items {
cursor := empty
if m.st.IsSelected(i) {
cursor = m.Cursor
}
value := item.Value()
if len(value) == 0 {
value = "<empty>"
}
var val string
switch item.Type() {
case TMultiline, TText, TFileExisting:
val = display.Trunc(value, m.width)
case TCheckbox:
if value == sTrue {
val = "[x]"
} else {
val = "[ ]"
}
case TRadio:
val = "[" + display.Trunc(value, m.width-4) + " ↓]"
}
style := m.Style.Normal
if m.st.IsSelected(i) {
style = m.Style.Selected
}
fmt.Fprintf(&buf,
style.Render("%s%*s %v")+"\n",
cursor,
-m.nameColSz,
item.Name(),
val,
)
}
// description
fmt.Fprint(&buf, "\n"+m.Style.Description.Render(m.Items[m.st.Cursor].Description()))
return buf.String()
}
func (m Model) editView() string {
item := m.Items[m.st.Cursor]
var v string
switch m.edittype {
case TText:
v = m.textinput.View()
case TMultiline:
v = m.textarea.View()
case TRadio:
v = m.radio.View()
case TFileExisting:
v = m.filemgr.View()
default:
return "INTERNAL ERROR"
}
return "--[" + item.Name() + "]------\n" + v + "\n" + m.Style.Description.Render(item.Description())
}