-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathskeleton.go
539 lines (445 loc) · 13.4 KB
/
skeleton.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package skeleton
import (
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// Skeleton is a helper for rendering the Skeleton of the terminal.
type Skeleton struct {
// termReady is control terminal is ready or not, it responsible for the terminal size
termReady bool
// termSizeNotEnoughToHandleHeaders is control terminal size is enough to handle headers
termSizeNotEnoughToHandleHeaders bool
// termSizeNotEnoughToHandleWidgets is control terminal size is enough to handle widgets
termSizeNotEnoughToHandleWidgets bool
// currentTab is hold the current tab index
currentTab int
// viewport is hold the viewport, it responsible for the terminal size
viewport *viewport.Model
// header is hold the header
header *header
// widget is hold the widget
widget *widget
// KeyMap responsible for the key bindings
KeyMap *keyMap
// pages are hold the pages
pages []tea.Model
// properties are hold the properties of the Skeleton
properties *skeletonProperties
updater *Updater
}
// NewSkeleton returns a new Skeleton.
func NewSkeleton() *Skeleton {
return &Skeleton{
properties: defaultSkeletonProperties(),
viewport: newTerminalViewport(),
header: newHeader(),
widget: newWidget(),
KeyMap: newKeyMap(),
updater: NewUpdater(),
}
}
// skeletonProperties are hold the properties of the Skeleton.
type skeletonProperties struct {
borderColor string
pagePosition lipgloss.Position
}
// defaultSkeletonProperties returns the default properties of the Skeleton.
func defaultSkeletonProperties() *skeletonProperties {
return &skeletonProperties{
borderColor: "39",
pagePosition: lipgloss.Center,
}
}
func (s *Skeleton) TriggerUpdate() {
s.updater.Update()
}
func (s *Skeleton) TriggerUpdateWithMsg(msg tea.Msg) {
s.updater.UpdateWithMsg(msg)
}
// SetBorderColor sets the border color of the Skeleton.
func (s *Skeleton) SetBorderColor(color string) *Skeleton {
s.header.SetBorderColor(color)
s.widget.SetBorderColor(color)
s.properties.borderColor = color
s.updater.Update()
return s
}
// GetBorderColor returns the border color of the Skeleton.
func (s *Skeleton) GetBorderColor() string {
return s.properties.borderColor
}
// GetWidgetBorderColor returns the border color of the Widget.
func (s *Skeleton) GetWidgetBorderColor() string {
return s.widget.GetBorderColor()
}
// SetPagePosition sets the position of the page.
func (s *Skeleton) SetPagePosition(position lipgloss.Position) *Skeleton {
s.properties.pagePosition = position
s.updater.Update()
return s
}
// GetPagePosition returns the position of the page.
func (s *Skeleton) GetPagePosition() lipgloss.Position {
return s.properties.pagePosition
}
// SetInactiveTabTextColor sets the idle tab color of the Skeleton.
func (s *Skeleton) SetInactiveTabTextColor(color string) *Skeleton {
s.header.SetInactiveTabTextColor(color)
s.updater.Update()
return s
}
// SetInactiveTabBorderColor sets the idle tab border color of the Skeleton.
func (s *Skeleton) SetInactiveTabBorderColor(color string) *Skeleton {
s.header.SetInactiveTabBorderColor(color)
s.updater.Update()
return s
}
// SetActiveTabTextColor sets the active tab color of the Skeleton.
func (s *Skeleton) SetActiveTabTextColor(color string) *Skeleton {
s.header.SetActiveTabTextColor(color)
s.updater.Update()
return s
}
// SetActiveTabBorderColor sets the active tab border color of the Skeleton.
func (s *Skeleton) SetActiveTabBorderColor(color string) *Skeleton {
s.header.SetActiveTabBorderColor(color)
s.updater.Update()
return s
}
// SetWidgetBorderColor sets the border color of the Widget.
func (s *Skeleton) SetWidgetBorderColor(color string) *Skeleton {
s.widget.SetWidgetBorderColor(color)
s.updater.Update()
return s
}
// SetTabLeftPadding sets the left padding of the Skeleton.
func (s *Skeleton) SetTabLeftPadding(padding int) *Skeleton {
s.header.SetLeftPadding(padding)
s.updater.Update()
return s
}
// SetTabRightPadding sets the right padding of the Skeleton.
func (s *Skeleton) SetTabRightPadding(padding int) *Skeleton {
s.header.SetRightPadding(padding)
s.updater.Update()
return s
}
// SetWidgetLeftPadding sets the left padding of the Skeleton.
func (s *Skeleton) SetWidgetLeftPadding(padding int) *Skeleton {
s.widget.SetLeftPadding(padding)
s.updater.Update()
return s
}
// SetWidgetRightPadding sets the right padding of the Skeleton.
func (s *Skeleton) SetWidgetRightPadding(padding int) *Skeleton {
s.widget.SetRightPadding(padding)
s.updater.Update()
return s
}
// LockTabs locks the tabs (headers). It prevents switching tabs. It is useful when you want to prevent switching tabs.
func (s *Skeleton) LockTabs() *Skeleton {
for _, header := range s.header.headers {
s.LockTab(header.key)
}
s.updater.Update()
return s
}
// UnlockTabs unlocks all tabs (both general and individual locks)
func (s *Skeleton) UnlockTabs() *Skeleton {
s.header.SetLockTabs(false)
// Clear all individual tab locks
for _, header := range s.header.headers {
s.UnlockTab(header.key)
}
s.updater.Update()
return s
}
// IsTabsLocked returns the tabs (headers) are locked or not.
func (s *Skeleton) IsTabsLocked() bool {
return s.header.GetLockTabs()
}
// AddPageMsg adds a new page to the Skeleton.
type AddPageMsg struct {
// Key is unique key of the page, it is used to identify the page
Key string
// Title is the title of the page, it is used to show the title on the header
Title string
// Page is the page model, it is used to show the content of the page
Page tea.Model
}
// AddPage adds a new page to the Skeleton.
func (s *Skeleton) AddPage(key string, title string, page tea.Model) *Skeleton {
// do not add if key already exists
for _, hdr := range s.header.headers {
if hdr.key == key {
return s
}
}
s.header.AddCommonHeader(key, title)
s.pages = append(s.pages, page)
s.updater.UpdateWithMsg(AddPageMsg{
Key: key,
Title: title,
Page: page,
})
return s
}
// UpdatePageTitle updates the title of the page by the given key.
func (s *Skeleton) UpdatePageTitle(key string, title string) *Skeleton {
s.header.UpdateCommonHeader(key, title)
s.updater.Update()
return s
}
// DeletePageMsg is sent when a page is deleted
type DeletePageMsg struct {
// Key is unique key of the page to be deleted
Key string
}
// DeletePage deletes the page by the given key.
func (s *Skeleton) DeletePage(key string) *Skeleton {
s.updater.UpdateWithMsg(DeletePageMsg{Key: key})
return s
}
func (s *Skeleton) deleteMsg(key string) {
if len(s.pages) == 1 {
// skeleton should have at least one page
return
}
// if active tab is about deleting tab, switch to the first tab
if s.GetActivePage() == key {
s.currentTab = 0
s.header.SetCurrentTab(0)
}
var pages []tea.Model
for i := range s.pages {
if s.header.headers[i].key != key {
pages = append(pages, s.pages[i])
}
}
s.header.DeleteCommonHeader(key)
s.pages = pages
}
// AddWidget adds a new widget to the Skeleton.
func (s *Skeleton) AddWidget(key string, value string) *Skeleton {
s.widget.addNewWidget(key, value)
s.updater.Update()
return s
}
// UpdateWidgetValue updates the Value content by the given key.
// Adds the widget if it doesn't exist.
func (s *Skeleton) UpdateWidgetValue(key string, value string) *Skeleton {
// if widget not exists, add it
if s.widget.GetWidget(key) == nil {
s.AddWidget(key, value)
}
s.widget.updateWidgetContent(key, value)
s.updater.Update()
return s
}
// DeleteWidget deletes the Value by the given key.
func (s *Skeleton) DeleteWidget(key string) *Skeleton {
s.widget.deleteWidget(key)
s.updater.Update()
return s
}
// DeleteAllWidgets deletes all the widgets.
func (s *Skeleton) DeleteAllWidgets() *Skeleton {
s.widget.DeleteAllWidgets()
s.updater.Update()
return s
}
// SetActivePage sets the active page by the given key.
func (s *Skeleton) SetActivePage(key string) *Skeleton {
for i, header := range s.header.headers {
if header.key == key {
s.currentTab = i
s.header.SetCurrentTab(i)
s.updater.Update()
break
}
}
return s
}
// GetActivePage returns the active page key.
func (s *Skeleton) GetActivePage() string {
return s.header.headers[s.currentTab].key
}
// IAMActivePage is a message to trigger the update of the active page.
type IAMActivePage struct{}
// IAMActivePageCmd returns the IAMActivePage command.
func (s *Skeleton) IAMActivePageCmd() tea.Cmd {
return func() tea.Msg {
return IAMActivePage{}
}
}
func (s *Skeleton) switchPage(cmds []tea.Cmd, position string) []tea.Cmd {
if s.IsTabsLocked() {
return cmds
}
currentTab := s.currentTab
switch position {
case "left":
// Start from current position and move left until we find an unlocked tab
for nextTab := currentTab - 1; nextTab >= 0; nextTab-- {
if !s.IsTabLocked(s.header.headers[nextTab].key) {
s.currentTab = nextTab
s.header.SetCurrentTab(nextTab)
return append(cmds, s.IAMActivePageCmd())
}
}
case "right":
// Start from current position and move right until we find an unlocked tab
for nextTab := currentTab + 1; nextTab < len(s.pages); nextTab++ {
if !s.IsTabLocked(s.header.headers[nextTab].key) {
s.currentTab = nextTab
s.header.SetCurrentTab(nextTab)
return append(cmds, s.IAMActivePageCmd())
}
}
}
return cmds
}
func (s *Skeleton) updateSkeleton(msg tea.Msg) []tea.Cmd {
var cmds []tea.Cmd
var cmd tea.Cmd
s.header, cmd = s.header.Update(msg)
cmds = append(cmds, cmd)
s.widget, cmd = s.widget.Update(msg)
cmds = append(cmds, cmd)
s.pages[s.currentTab], cmd = s.pages[s.currentTab].Update(msg)
cmds = append(cmds, cmd)
return cmds
}
func (s *Skeleton) Init() tea.Cmd {
if len(s.pages) == 0 {
panic("skeleton: no pages added, please add at least one page")
}
return tea.Batch(s.updater.Listen(), s.header.Init(), s.widget.Init())
}
func (s *Skeleton) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
s.currentTab = s.header.GetCurrentTab()
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !s.termReady {
if msg.Width > 0 && msg.Height > 0 {
s.termReady = true
}
}
s.viewport.Width = msg.Width
s.viewport.Height = msg.Height
return s, tea.Batch(s.updateSkeleton(msg)...)
case tea.KeyMsg:
var cmds []tea.Cmd
switch {
case key.Matches(msg, s.KeyMap.Quit):
return s, tea.Quit
case key.Matches(msg, s.KeyMap.SwitchTabLeft):
cmds = s.switchPage(cmds, "left")
case key.Matches(msg, s.KeyMap.SwitchTabRight):
cmds = s.switchPage(cmds, "right")
}
cmds = append(cmds, s.updateSkeleton(msg)...)
return s, tea.Batch(cmds...)
case AddPageMsg:
cmds := s.updateSkeleton(msg)
cmds = append(cmds, msg.Page.Init(), s.updater.Listen())
return s, tea.Batch(cmds...)
case UpdateMsg:
cmds := s.updateSkeleton(msg)
cmds = append(cmds, s.updater.Listen())
return s, tea.Batch(cmds...)
case HeaderSizeMsg:
s.termSizeNotEnoughToHandleHeaders = msg.NotEnoughToHandleHeaders
return s, nil
case WidgetSizeMsg:
s.termSizeNotEnoughToHandleWidgets = msg.NotEnoughToHandleWidgets
return s, nil
case DeletePageMsg:
s.deleteMsg(msg.Key)
cmds := s.updateSkeleton(msg)
cmds = append(cmds, s.IAMActivePageCmd())
cmds = append(cmds, s.updater.Listen())
return s, tea.Batch(cmds...)
default:
cmds := s.updateSkeleton(msg)
cmds = append(cmds, s.updater.Listen())
return s, tea.Batch(cmds...)
}
}
func (s *Skeleton) View() string {
if !s.termReady {
return "setting up terminal..."
}
if !s.termSizeNotEnoughToHandleHeaders {
return "terminal size is not enough to show headers"
}
if !s.termSizeNotEnoughToHandleWidgets {
return "terminal size is not enough to show widgets"
}
// Calculate available height for body
headerHeight := lipgloss.Height(s.header.View())
widgetHeight := 0
if len(s.widget.widgets) > 0 {
widgetHeight = lipgloss.Height(s.widget.View())
}
bodyHeight := s.viewport.Height - headerHeight - widgetHeight
// Style for the body content
base := lipgloss.NewStyle().
BorderForeground(lipgloss.Color(s.properties.borderColor)).
Align(s.properties.pagePosition).
Border(lipgloss.RoundedBorder()).
BorderTop(false).BorderBottom(false).
Width(s.viewport.Width - 2).
MaxHeight(bodyHeight)
// Get body content
body := s.pages[s.currentTab].View()
// Add padding if content is shorter than available height
if lipgloss.Height(body) < bodyHeight {
body += strings.Repeat("\n", bodyHeight-lipgloss.Height(body))
}
return lipgloss.JoinVertical(lipgloss.Top,
s.header.View(),
base.Render(body),
s.widget.View())
}
// LockTab locks a specific tab by its key
func (s *Skeleton) LockTab(key string) *Skeleton {
s.header.LockTab(key)
s.updater.Update()
return s
}
// UnlockTab unlocks a specific tab by its key
func (s *Skeleton) UnlockTab(key string) *Skeleton {
s.header.UnlockTab(key)
s.updater.Update()
return s
}
// IsTabLocked checks if a specific tab is locked
func (s *Skeleton) IsTabLocked(key string) bool {
return s.header.IsTabLocked(key)
}
// LockTabsToTheRight locks all tabs to the right of the current tab
func (s *Skeleton) LockTabsToTheRight() *Skeleton {
if s.currentTab >= len(s.header.headers)-1 {
return s // No tabs to the right
}
for i := s.currentTab + 1; i < len(s.header.headers); i++ {
s.LockTab(s.header.headers[i].key)
}
s.updater.Update()
return s
}
// LockTabsToTheLeft locks all tabs to the left of the current tab
func (s *Skeleton) LockTabsToTheLeft() *Skeleton {
if s.currentTab <= 0 {
return s // No tabs to the left
}
for i := 0; i < s.currentTab; i++ {
s.LockTab(s.header.headers[i].key)
}
s.updater.Update()
return s
}