-
Notifications
You must be signed in to change notification settings - Fork 20
/
main_model.go
161 lines (127 loc) · 3.05 KB
/
main_model.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
package tui
import (
"cmp"
"slices"
"sync"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/Zxilly/go-size-analyzer/internal/result"
)
var _ tea.Model = (*mainModel)(nil)
type mainModel struct {
width int
height int
baseItems wrappers
current *wrapper // nil means root
fileName string
leftTable table.Model
rightDetail detailModel
help help.Model
focus focusState
parents []table.Model
}
func (m mainModel) currentSelection() *wrapper {
l := m.currentList()
if m.leftTable.Cursor() < 0 || m.leftTable.Cursor() >= len(l) {
panic("cursor out of range")
}
return &l[m.leftTable.Cursor()]
}
func (m mainModel) currentList() wrappers {
if m.current == nil {
return m.baseItems
}
return m.current.children()
}
func (m mainModel) getKeyMap() help.KeyMap {
mainKeys := []key.Binding{DefaultKeyMap.Switch, DefaultKeyMap.Exit}
if m.currentSelection().hasChildren() {
mainKeys = append(mainKeys, DefaultKeyMap.Enter)
}
if m.current != nil {
mainKeys = append(mainKeys, DefaultKeyMap.Backward)
}
ret := DynamicKeyMap{
Short: mainKeys,
Long: [][]key.Binding{mainKeys},
}
switch m.focus {
case focusedMain:
ret.Short = append(ret.Short, tableKeyMap()...)
ret.Long = append(ret.Long, tableKeyMap())
case focusedDetail:
ret.Short = append(ret.Short, m.rightDetail.KeyMap()...)
ret.Long = append(ret.Long, m.rightDetail.KeyMap())
}
return ret
}
func (m mainModel) nextFocus() focusState {
switch m.focus {
case focusedMain:
return focusedDetail
case focusedDetail:
return focusedMain
default:
panic("invalid focus state")
}
}
var (
rootCache wrappers
rootCacheOnce = &sync.Once{}
)
func buildRootItems(r *result.Result) wrappers {
rootCacheOnce.Do(func() {
ret := make([]wrapper, 0)
for _, p := range r.Packages {
ret = append(ret, newWrapper(p))
}
for _, s := range r.Sections {
ret = append(ret, newWrapper(s))
}
slices.SortFunc(ret, func(a, b wrapper) int {
return -cmp.Compare(a.size(), b.size())
})
rootCache = ret
})
return rootCache
}
func newLeftTable(width int, rows []table.Row) table.Model {
return table.New(
table.WithColumns(getTableColumns(width)),
table.WithRows(rows),
table.WithFocused(true),
)
}
func newMainModel(r *result.Result, width, height int) mainModel {
baseItems := buildRootItems(r)
m := mainModel{
baseItems: baseItems,
current: nil,
fileName: r.Name,
rightDetail: newDetailModel(0, 0),
leftTable: newLeftTable(0, baseItems.ToRows()),
help: help.New(),
focus: focusedMain,
parents: make([]table.Model, 0),
width: width,
height: height,
}
m.rightDetail.viewPort.SetContent(m.currentSelection().Description())
return m
}
func (mainModel) Init() tea.Cmd {
return nil
}
func (m mainModel) title() string {
if m.current == nil {
return m.fileName
}
switch {
case m.current.file != nil:
return m.current.parent.Title() + "/" + m.current.Title()
default:
return m.current.Title()
}
}