-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpane_manager.go
382 lines (355 loc) · 10.3 KB
/
pane_manager.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
package tui
import (
"errors"
"fmt"
"slices"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/leg100/pug/internal/resource"
"github.com/leg100/pug/internal/tui/keys"
"golang.org/x/exp/maps"
)
type Position int
const (
// TopRightPane occupies the top right area of the terminal. Mutually
// exclusive with RightPane.
TopRightPane Position = iota
// BottomRightPane occupies the bottom right area of the terminal. Mutually
// exclusive with RightPane.
BottomRightPane
// LeftPane occupies the left side of the terminal.
LeftPane
)
// PaneManager manages the layout of the three panes that compose the Pug full screen terminal app.
type PaneManager struct {
// makers for making models for panes
makers map[Kind]Maker
// cache of previously made models
cache *Cache
// the position of the currently focused pane
focused Position
// panes tracks currently visible panes
panes map[Position]pane
// total width and height of the terminal space available to panes.
width, height int
// leftPaneWidth is the width of the left pane when sharing the terminal
// with other panes.
leftPaneWidth int
// topRightPaneHeight is the height of the top right pane.
topRightHeight int
// history tracks previously visited models for the top right pane.
history []pane
}
type pane struct {
model ChildModel
page Page
}
type tablePane interface {
PreviewCurrentRow() (Kind, resource.ID, bool)
}
// NewPaneManager constructs the pane manager with at least the explorer, which
// occupies the left pane.
func NewPaneManager(makers map[Kind]Maker) *PaneManager {
p := &PaneManager{
makers: makers,
cache: NewCache(),
panes: make(map[Position]pane),
leftPaneWidth: defaultLeftPaneWidth,
topRightHeight: defaultTopRightPaneHeight,
}
return p
}
func (p *PaneManager) Init() tea.Cmd {
return p.setPane(NavigationMsg{
Position: LeftPane,
Page: Page{Kind: ExplorerKind},
})
}
func (p *PaneManager) Update(msg tea.Msg) tea.Cmd {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Common.Back):
if p.focused != TopRightPane {
// History is only maintained for the top right pane.
break
}
if len(p.history) == 1 {
// At dawn of history; can't go further back.
return ReportError(errors.New("already at first page"))
}
// Pop current model from history
p.history = p.history[:len(p.history)-1]
// Set pane to last model
p.panes[TopRightPane] = p.history[len(p.history)-1]
// A new top right pane replaces any bottom right pane as well.
delete(p.panes, BottomRightPane)
p.updateChildSizes()
case key.Matches(msg, keys.Global.ShrinkPaneWidth):
p.updateLeftWidth(-1)
p.updateChildSizes()
case key.Matches(msg, keys.Global.GrowPaneWidth):
p.updateLeftWidth(1)
p.updateChildSizes()
case key.Matches(msg, keys.Global.ShrinkPaneHeight):
p.updateTopRightHeight(-1)
p.updateChildSizes()
case key.Matches(msg, keys.Global.GrowPaneHeight):
p.updateTopRightHeight(1)
p.updateChildSizes()
case key.Matches(msg, keys.Navigation.SwitchPane):
p.cycleFocusedPane(false)
case key.Matches(msg, keys.Navigation.SwitchPaneBack):
p.cycleFocusedPane(true)
case key.Matches(msg, keys.Global.ClosePane):
cmds = append(cmds, p.closeFocusedPane())
case key.Matches(msg, keys.Navigation.LeftPane):
p.focusPane(LeftPane)
case key.Matches(msg, keys.Navigation.TopRightPane):
p.focusPane(TopRightPane)
case key.Matches(msg, keys.Navigation.BottomRightPane):
p.focusPane(BottomRightPane)
default:
// Send remaining keys to focused pane
cmds = append(cmds, p.updateModel(p.focused, msg))
}
case tea.WindowSizeMsg:
p.width = msg.Width
p.height = msg.Height
p.updateLeftWidth(0)
p.updateTopRightHeight(0)
p.updateChildSizes()
case NavigationMsg:
cmds = append(cmds, p.setPane(msg))
default:
// Send remaining message types to cached panes.
cmds = p.cache.UpdateAll(msg)
}
// Check that if the top right pane is a table with a current row, then
// ensure the bottom left pane corresponds to that current row, e.g. if the
// top right pane is a tasks table, then the bottom right pane shows the
// output for the current task row.
if pane, ok := p.panes[TopRightPane]; ok {
if table, ok := pane.model.(tablePane); ok {
if kind, id, ok := table.PreviewCurrentRow(); ok {
cmd := p.setPane(NavigationMsg{
Page: Page{Kind: kind, ID: id},
Position: BottomRightPane,
DisableFocus: true,
})
cmds = append(cmds, cmd)
}
}
}
return tea.Batch(cmds...)
}
// FocusedModel retrieves the model of the focused pane.
func (p *PaneManager) FocusedModel() ChildModel {
return p.panes[p.focused].model
}
// cycleFocusedPane makes the next pane the focused pane. If last is true then the
// previous pane is made the focused pane.
func (p *PaneManager) cycleFocusedPane(last bool) {
positions := maps.Keys(p.panes)
slices.Sort(positions)
var focusedIndex int
for i, pos := range positions {
if pos == p.focused {
focusedIndex = i
}
}
var newFocusedIndex int
if last {
newFocusedIndex = focusedIndex - 1
if newFocusedIndex < 0 {
newFocusedIndex = len(positions) + newFocusedIndex
}
} else {
newFocusedIndex = (focusedIndex + 1) % len(positions)
}
p.focusPane(positions[newFocusedIndex])
}
func (p *PaneManager) closeFocusedPane() tea.Cmd {
if len(p.panes) == 1 {
return ReportError(errors.New("cannot close last pane"))
}
delete(p.panes, p.focused)
p.updateChildSizes()
p.cycleFocusedPane(false)
return nil
}
func (p *PaneManager) updateLeftWidth(delta int) {
if _, ok := p.panes[LeftPane]; !ok {
// There is no vertical split to adjust
return
}
p.leftPaneWidth = clamp(p.leftPaneWidth+delta, minPaneWidth, p.width-minPaneWidth)
}
func (p *PaneManager) updateTopRightHeight(delta int) {
if _, ok := p.panes[TopRightPane]; !ok {
// There is no horizontal split to adjust
return
} else if _, ok := p.panes[BottomRightPane]; !ok {
// There is no horizontal split to adjust
return
}
switch p.focused {
case BottomRightPane:
delta = -delta
}
p.topRightHeight = clamp(p.topRightHeight+delta, minPaneHeight, p.height-minPaneHeight)
}
func (p *PaneManager) updateChildSizes() {
for position := range p.panes {
p.updateModel(position, tea.WindowSizeMsg{
Width: p.paneWidth(position) - 2, // -2 for borders
Height: p.paneHeight(position) - 2, // -2 for borders
})
}
}
func (p *PaneManager) updateModel(position Position, msg tea.Msg) tea.Cmd {
return p.panes[position].model.Update(msg)
}
func (m *PaneManager) setPane(msg NavigationMsg) (cmd tea.Cmd) {
if pane, ok := m.panes[msg.Position]; ok && pane.page == msg.Page {
// Pane is already showing requested page, so just bring it into focus.
if !msg.DisableFocus {
m.focusPane(msg.Position)
}
return nil
}
model := m.cache.Get(msg.Page)
if model == nil {
maker, ok := m.makers[msg.Page.Kind]
if !ok {
return ReportError(fmt.Errorf("no maker could be found for %s", msg.Page.Kind))
}
var err error
model, err = maker.Make(msg.Page.ID, 0, 0)
if err != nil {
return ReportError(fmt.Errorf("making page of kind %s with id %s: %w", msg.Page.Kind, msg.Page.ID, err))
}
m.cache.Put(msg.Page, model)
cmd = model.Init()
}
m.panes[msg.Position] = pane{
model: model,
page: msg.Page,
}
if msg.Position == TopRightPane {
// A new top right pane replaces any bottom right pane as well.
delete(m.panes, BottomRightPane)
// Track the models for the top right pane, so that the user can go back
// to previous models.
m.history = append(m.history, m.panes[TopRightPane])
}
m.updateChildSizes()
if !msg.DisableFocus {
m.focusPane(msg.Position)
}
return cmd
}
func (m *PaneManager) focusPane(position Position) {
if _, ok := m.panes[position]; !ok {
// There is no pane to focus at requested position
return
}
m.focused = position
}
func (m *PaneManager) paneWidth(position Position) int {
switch position {
case LeftPane:
if len(m.panes) > 1 {
return m.leftPaneWidth
}
default:
if _, ok := m.panes[LeftPane]; ok {
return max(minPaneWidth, m.width-m.leftPaneWidth)
}
}
return m.width
}
func (m *PaneManager) paneHeight(position Position) int {
switch position {
case TopRightPane:
if _, ok := m.panes[BottomRightPane]; ok {
return m.topRightHeight
}
case BottomRightPane:
if _, ok := m.panes[TopRightPane]; ok {
return m.height - m.topRightHeight
}
}
return m.height
}
func (m *PaneManager) View() string {
return lipgloss.JoinHorizontal(lipgloss.Top,
removeEmptyStrings(
m.renderPane(LeftPane),
lipgloss.JoinVertical(lipgloss.Top,
removeEmptyStrings(
m.renderPane(TopRightPane),
m.renderPane(BottomRightPane),
)...,
),
)...,
)
}
func (m *PaneManager) renderPane(position Position) string {
if _, ok := m.panes[position]; !ok {
return ""
}
model := m.panes[position].model
isFocused := position == m.focused
renderedPane := lipgloss.NewStyle().
Width(m.paneWidth(position) - 2). // -2 for border
Height(m.paneHeight(position) - 2). // -2 for border
MaxWidth(m.paneWidth(position) - 2). // -2 for border
Render(model.View())
// Optionally, the pane model can embed text in its borders.
borderTexts := make(map[BorderPosition]string)
if textInBorder, ok := model.(interface {
BorderText() map[BorderPosition]string
}); ok {
borderTexts = textInBorder.BorderText()
}
if !isFocused {
switch position {
case LeftPane:
borderTexts[TopRightBorder] = keys.Navigation.LeftPane.Keys()[0]
case TopRightPane:
borderTexts[TopRightBorder] = keys.Navigation.TopRightPane.Keys()[0]
case BottomRightPane:
borderTexts[TopRightBorder] = keys.Navigation.BottomRightPane.Keys()[0]
}
}
return borderize(renderedPane, isFocused, borderTexts)
}
func (m *PaneManager) HelpBindings() (bindings []key.Binding) {
if m.focused == TopRightPane {
// Only the top right pane has the ability to "go back"
bindings = append(bindings, keys.Common.Back)
}
if model, ok := m.FocusedModel().(ModelHelpBindings); ok {
bindings = append(bindings, model.HelpBindings()...)
}
return bindings
}
func removeEmptyStrings(strs ...string) []string {
n := 0
for _, s := range strs {
if s != "" {
strs[n] = s
n++
}
}
return strs[:n]
}
func clamp(v, low, high int) int {
if high < low {
low, high = high, low
}
return min(high, max(low, v))
}