-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
367 lines (314 loc) Β· 7.55 KB
/
app.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package ui
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lusingander/topi/internal/topi"
)
var (
headerStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("65")).
Padding(0, 1)
footerStyle = lipgloss.NewStyle()
statusbarFileNameStyle = lipgloss.NewStyle().
Background(lipgloss.Color("65")).
Padding(0, 1)
statusbarSpaceColorStyle = lipgloss.NewStyle().
Background(lipgloss.Color("237"))
statusbarLowerStyle = lipgloss.NewStyle()
)
type page interface {
crumb() string
}
type menuPage struct{}
func (menuPage) crumb() string { return topi.AppName }
type infoPage struct{}
func (infoPage) crumb() string { return "info" }
type tagPage struct{}
func (tagPage) crumb() string { return "tags" }
type tagPathsPage struct {
tag string
}
func (p tagPathsPage) crumb() string { return p.tag }
type pathPage struct{}
func (pathPage) crumb() string { return "paths" }
type operationPage struct {
operationId string
}
func (p operationPage) crumb() string { return p.operationId } // fixme
type helpMenuPage struct{}
func (helpMenuPage) crumb() string { return "help" }
type helpPage struct{}
func (helpPage) crumb() string { return "help" }
type aboutPage struct{}
func (aboutPage) crumb() string { return "about" }
type creditsPage struct{}
func (creditsPage) crumb() string { return "credits" }
type pageStack struct {
stack []page
}
func (s pageStack) crumbs() []string {
ret := make([]string, len(s.stack))
for i, p := range s.stack {
ret[i] = p.crumb()
}
return ret
}
func newPageStack(p page) *pageStack {
return &pageStack{
stack: []page{p},
}
}
func (s *pageStack) pushPage(p page) {
s.stack = append(s.stack, p)
}
func (s *pageStack) popPage() page {
l := len(s.stack)
if l <= 1 {
return nil
}
p := s.stack[l-1]
s.stack = s.stack[:l-1]
return p
}
func (s *pageStack) currentPage() page {
return s.stack[len(s.stack)-1]
}
type model struct {
doc *topi.Document
*pageStack
menuPage menuPageModel
infoPage infoPageModel
tagPage tagPageModel
tagPathsPage tagPathsPageModel
pathPage pathPageModel
operationPage operationPageModel
helpMenuPage helpMenuPageModel
helpPage helpPageModel
aboutPage aboutPageModel
creditsPage creditsPageModel
width, height int
}
var _ tea.Model = (*model)(nil)
func newModel(doc *topi.Document) model {
startPage := menuPage{}
return model{
doc: doc,
pageStack: newPageStack(startPage),
infoPage: newInfoPageModel(doc),
menuPage: newMenuPageModel(),
tagPage: newTagPageModel(doc),
tagPathsPage: newTagPathsPageModel(doc),
pathPage: newPathPageModel(doc),
operationPage: newOperationPageModel(doc),
helpMenuPage: newHelpMenuPageModel(),
helpPage: newHelpPageModel(),
aboutPage: newAboutPageModel(),
creditsPage: newCreditsPageModel(),
}
}
func (m *model) SetSize(w, h int) {
m.width, m.height = w, h
t, r, b, l := baseStyle.GetMargin()
w = w - r - l
h = h - t - b
h = h - 3 // :(
m.menuPage.SetSize(w, h)
m.infoPage.SetSize(w, h)
m.tagPage.SetSize(w, h)
m.tagPathsPage.SetSize(w, h)
m.pathPage.SetSize(w, h)
m.operationPage.SetSize(w, h)
m.helpMenuPage.SetSize(w, h)
m.helpPage.SetSize(w, h)
m.aboutPage.SetSize(w, h)
m.creditsPage.SetSize(w, h)
}
func (m *model) toggleHelp() {
switch m.currentPage().(type) {
case helpPage:
m.popPage()
default:
m.pushPage(helpPage{})
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "?":
return m, toggleHelp
}
case tea.WindowSizeMsg:
m.SetSize(msg.Width, msg.Height)
case toggleHelpMsg:
m.toggleHelp()
case selectInfoMenuMsg:
m.pushPage(infoPage{})
case selectTagMenuMsg:
m.pushPage(tagPage{})
case selectPathMenuMsg:
m.pushPage(pathPage{})
case selectHelpMenuMsg:
m.pushPage(helpMenuPage{})
case selectHelpHelpMenuMsg:
m.pushPage(helpPage{})
case selectAboutMenuMsg:
m.pushPage(aboutPage{})
case selectCreditsMenuMsg:
m.pushPage(creditsPage{})
case selectTagMsg:
m.pushPage(tagPathsPage(msg))
case selectOperationMsg:
m.pushPage(operationPage(msg))
case goBackMsg:
m.popPage()
}
switch m.currentPage().(type) {
case menuPage:
m.menuPage, cmd = m.menuPage.Update(msg)
return m, cmd
case infoPage:
m.infoPage, cmd = m.infoPage.Update(msg)
return m, cmd
case tagPage:
m.tagPage, cmd = m.tagPage.Update(msg)
return m, cmd
case tagPathsPage:
m.tagPathsPage, cmd = m.tagPathsPage.Update(msg)
return m, cmd
case pathPage:
m.pathPage, cmd = m.pathPage.Update(msg)
return m, cmd
case operationPage:
m.operationPage, cmd = m.operationPage.Update(msg)
return m, cmd
case helpMenuPage:
m.helpMenuPage, cmd = m.helpMenuPage.Update(msg)
return m, cmd
case helpPage:
m.helpPage, cmd = m.helpPage.Update(msg)
return m, cmd
case aboutPage:
m.aboutPage, cmd = m.aboutPage.Update(msg)
return m, cmd
case creditsPage:
m.creditsPage, cmd = m.creditsPage.Update(msg)
return m, cmd
default:
return m, nil
}
}
func (m model) View() string {
header := m.appHeader()
content := baseStyle.Render(m.content())
footer := m.appFooter()
return lipgloss.JoinVertical(lipgloss.Top, header, content, footer)
}
func (m model) content() string {
switch m.currentPage().(type) {
case menuPage:
return m.menuPage.View()
case infoPage:
return m.infoPage.View()
case tagPage:
return m.tagPage.View()
case tagPathsPage:
return m.tagPathsPage.View()
case pathPage:
return m.pathPage.View()
case operationPage:
return m.operationPage.View()
case helpMenuPage:
return m.helpMenuPage.View()
case helpPage:
return m.helpPage.View()
case aboutPage:
return m.aboutPage.View()
case creditsPage:
return m.creditsPage.View()
default:
return "error... :("
}
}
func (m model) appHeader() string {
bd := strings.Join(m.crumbs(), " > ")
return headerStyle.Render(bd)
}
func (m model) appFooter() string {
w := m.width
if w == 0 {
return ""
}
name := statusbarFileNameStyle.Render(m.doc.Meta.FileName)
statusbarInfo := m.statusbarInfoString()
sw := w - lipgloss.Width(name) - lipgloss.Width(statusbarInfo)
spaces := statusbarSpaceColorStyle.Render(strings.Repeat(" ", sw))
u := name + spaces + statusbarInfo
statusMessage := m.statusMessageString()
l := statusbarLowerStyle.Render(statusMessage)
return footerStyle.Render(u + "\n" + l)
}
func (m model) statusbarInfoString() string {
switch m.currentPage().(type) {
case menuPage:
return ""
case infoPage:
return ""
case tagPage:
return m.tagPage.statusbarInfoString()
case tagPathsPage:
return m.tagPathsPage.statusbarInfoString()
case pathPage:
return m.pathPage.statusbarInfoString()
case operationPage:
return ""
case helpMenuPage:
return ""
case helpPage:
return ""
case aboutPage:
return ""
case creditsPage:
return ""
default:
return "error... :("
}
}
func (m model) statusMessageString() string {
switch m.currentPage().(type) {
case menuPage:
return ""
case infoPage:
return ""
case tagPage:
return m.tagPage.statusMessageString()
case tagPathsPage:
return m.tagPathsPage.statusMessageString()
case pathPage:
return m.pathPage.statusMessageString()
case operationPage:
return ""
case helpMenuPage:
return ""
case helpPage:
return ""
case aboutPage:
return ""
case creditsPage:
return ""
default:
return "error... :("
}
}
func Start(doc *topi.Document) error {
m := newModel(doc)
p := tea.NewProgram(m, tea.WithAltScreen())
return p.Start()
}