-
Notifications
You must be signed in to change notification settings - Fork 20
/
update.go
131 lines (107 loc) · 3.12 KB
/
update.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
package tui
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
func (m mainModel) pushParent(cursor table.Model) mainModel {
m.parents = append(m.parents, cursor)
return m
}
func (m mainModel) popParent() (mainModel, table.Model) {
old := m.parents[len(m.parents)-1]
m.parents = m.parents[:len(m.parents)-1]
return m, old
}
func (m mainModel) updateDetail() mainModel {
m.rightDetail.viewPort.SetContent(m.currentSelection().Description())
return m
}
func handleKeyEvent(m mainModel, msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, DefaultKeyMap.Switch):
m.focus = m.nextFocus()
return m, nil
case key.Matches(msg, DefaultKeyMap.Backward):
if m.current != nil && m.focus == focusedMain {
m.current = m.current.parent
var parent table.Model
m, parent = m.popParent()
m.leftTable = parent
m = m.updateDetail()
m, _ = handleWindowSizeEvent(m, m.width, m.height)
}
return m, nil
case key.Matches(msg, DefaultKeyMap.Enter):
if m.currentSelection().hasChildren() && m.focus == focusedMain {
m = m.pushParent(m.leftTable)
m.current = m.currentSelection()
m.leftTable = newLeftTable(m.width, m.current.children().ToRows())
m = m.updateDetail()
m, _ = handleWindowSizeEvent(m, m.width, m.height)
}
case key.Matches(msg, DefaultKeyMap.Exit):
return m, tea.Quit
}
var cmd tea.Cmd
switch m.focus {
case focusedMain:
m.leftTable, cmd = m.leftTable.Update(msg)
m = m.updateDetail()
case focusedDetail:
m.rightDetail, cmd = m.rightDetail.Update(msg)
}
return m, cmd
}
func tableHandleMouseEvent(t table.Model, msg tea.MouseMsg) (table.Model, tea.Cmd) {
switch msg.Button {
case tea.MouseButtonWheelUp:
t.MoveUp(3)
case tea.MouseButtonWheelDown:
t.MoveDown(3)
default:
return t, nil
}
return t, nil
}
func handleMouseEvent(m mainModel, msg tea.MouseMsg) (mainModel, tea.Cmd) {
if msg.Action != tea.MouseActionPress {
return m, nil
}
var cmd tea.Cmd
switch m.focus {
case focusedMain:
m.leftTable, cmd = tableHandleMouseEvent(m.leftTable, msg)
m = m.updateDetail()
case focusedDetail:
m.rightDetail, cmd = m.rightDetail.Update(msg)
}
return m, cmd
}
func handleWindowSizeEvent(m mainModel, width, height int) (mainModel, tea.Cmd) {
m.width = width
m.height = height
m.leftTable.SetWidth(width / 2)
m.leftTable.SetColumns(getTableColumns(width))
m.help.Width = width
helpHeight := lipgloss.Height(m.help.View(m.getKeyMap()))
const headerHeight = 1
const nameHeight = 1
// update the table height accordingly
m.leftTable.SetHeight(height - helpHeight - headerHeight - nameHeight - 1)
m.rightDetail.viewPort.Height = height - helpHeight - nameHeight - 2
m.rightDetail.viewPort.Width = width - width/2 - 1
return m, tea.ClearScreen
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
return handleWindowSizeEvent(m, msg.Width, msg.Height)
case tea.KeyMsg:
return handleKeyEvent(m, msg)
case tea.MouseMsg:
return handleMouseEvent(m, msg)
}
return m, nil
}