summaryrefslogtreecommitdiffstats
path: root/runtime/ui/components/layers_primative.go
blob: 0c3dce06f9c6dd69e4dd178098e5d021b9459aa3 (plain)
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
package components

import (
	"fmt"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
	"github.com/sirupsen/logrus"
	"github.com/wagoodman/dive/runtime/ui/format"
	"github.com/wagoodman/dive/runtime/ui/viewmodels"
	"go.uber.org/zap"
)

type LayersViewModel interface {
	SetLayerIndex(int) bool
	GetPrintableLayers() []fmt.Stringer
	SwitchLayerMode() error
	GetMode() viewmodels.LayerCompareMode
}

type LayerList struct {
	*tview.Box
	bufferIndexLowerBound int
	cmpIndex              int
	changed               LayerListHandler

	keyInputHandler *KeyInputHandler

	LayersViewModel
}

type LayerListHandler func(index int, shortcut rune)

func NewLayerList(model LayersViewModel) *LayerList {
	return &LayerList{
		Box:             tview.NewBox(),
		cmpIndex:        0,
		LayersViewModel: model,
		keyInputHandler: NewKeyInputHandler(),
	}
}

type LayerListViewOption func(ll *LayerList)

var AlwaysFalse = func() bool {return false}
var AlwaysTrue = func() bool {return true}


func UpLayerListBindingOption(k KeyBinding) LayerListViewOption {
	return func(ll *LayerList) {
		displayBinding := KeyBindingDisplay{
			KeyBinding: &k,
			Selected:   AlwaysFalse,
			Hide:       AlwaysTrue,
		}
		ll.keyInputHandler.AddBinding(displayBinding, func() { ll.keyUp() })
	}
}

func DownLayerListBindingOption(k KeyBinding) LayerListViewOption {
	return func(ll *LayerList) {
		displayBinding := KeyBindingDisplay{
			KeyBinding: &k,
			Selected:   AlwaysFalse,
			Hide:       AlwaysTrue,
		}
		ll.keyInputHandler.AddBinding(displayBinding, func() { ll.keyDown() })
	}
}

func PageUpLayerListBindingOption(k KeyBinding) LayerListViewOption {
	return func(ll *LayerList) {
		displayBinding := KeyBindingDisplay{
			KeyBinding: &k,
			Selected:   AlwaysFalse,
			Hide:       AlwaysFalse,
		}
		ll.keyInputHandler.AddBinding(displayBinding, func() { ll.pageUp() })
	}
}

func PageDownLayerListBindingOption(k KeyBinding) LayerListViewOption {
	return func(ll *LayerList) {
		displayBinding := KeyBindingDisplay{
			KeyBinding: &k,
			Selected:   AlwaysFalse,
			Hide:       AlwaysFalse,
		}
		ll.keyInputHandler.AddBinding(displayBinding, func() { ll.pageDown() })
	}
}

func SwitchCompareLayerListBindingOption(k KeyBinding) LayerListViewOption {
	return func(ll *LayerList) {
		displayBinding := KeyBindingDisplay{
			KeyBinding: &k,
			Selected:   func() bool {return ll.GetMode() == viewmodels.CompareAllLayers},
			Hide:       AlwaysFalse,
		}
		ll.keyInputHandler.AddBinding(displayBinding, func() {
			if err := ll.SwitchLayerMode(); err != nil {
				logrus.Error("SwitchCompareLayers error: ", err.Error())
			}
		})
	}
}

func (ll *LayerList) AddBindingOptions(bindingOptions ...LayerListViewOption) *LayerList {
	for _, option := range bindingOptions {
		option(ll)
	}

	return ll
}

func (ll *LayerList) Setup(config KeyBindingConfig) *LayerList {

	ll.AddBindingOptions(
		UpLayerListBindingOption(NewKeyBinding("Cursor Up", tcell.NewEventKey(tcell.KeyUp, rune(0), tcell.ModNone))),
		UpLayerListBindingOption(NewKeyBinding("", tcell.NewEventKey(tcell.KeyLeft, rune(0), tcell.ModNone))),
		DownLayerListBindingOption(NewKeyBinding("Cursor Down", tcell.NewEventKey(tcell.KeyDown, rune(0), tcell.ModNone))),
		DownLayerListBindingOption(NewKeyBinding("", tcell.NewEventKey(tcell.KeyRight, rune(0), tcell.ModNone))),
	)

	bindingOrder := []string {
		"keybinding.page-up",
		"keybinding.page-down",
		"keybinding.compare-all",
	}

	bindingSettings := map[string]func(KeyBinding) LayerListViewOption{
		"keybinding.page-up":       PageUpLayerListBindingOption,
		"keybinding.page-down":     PageDownLayerListBindingOption,
		"keybinding.compare-all":   SwitchCompareLayerListBindingOption,
	}


	for _, keybinding := range bindingOrder {
		action := bindingSettings[keybinding]
		binding, err := config.GetKeyBinding(keybinding)
		if err != nil {
			panic(fmt.Errorf("setup error for keybinding: %s: %w", keybinding, err))
			// TODO handle this error
			//return nil
		}
		ll.AddBindingOptions(action(binding))
	}

	return ll
}

func (ll *LayerList) GetKeyBindings() []KeyBindingDisplay {
	return ll.keyInputHandler.Order
}

func (ll *LayerList) getBox() *tview.Box {
	return ll.Box
}

func (ll *LayerList) getDraw() drawFn {
	return ll.Draw
}

func (ll *LayerList) getInputWrapper() inputFn {
	return ll.InputHandler
}

func (ll *LayerList) Draw(screen tcell.Screen) {
	ll.Box.Draw(screen)
	x, y, width, height := ll.Box.GetInnerRect()
	compressedView := width < 25

	cmpString := "  "
	printableLayers := ll.GetPrintableLayers()
	for yIndex := 0; yIndex < height; yIndex++ {
		layerIndex := ll.bufferIndexLowerBound + yIndex
		if layerIndex >= len(printableLayers) {
			break
		}
		layer := printableLayers[layerIndex]
		cmpFormatter := format.Normal
		lineFormatter := format.Normal
		switch {
		case layerIndex == ll.cmpIndex:
			cmpFormatter = format.CompareTop
			lineFormatter = format.Selected
		case layerIndex > 0 && layerIndex < ll.cmpIndex && ll.GetMode() == viewmodels.CompareAllLayers:
			cmpFormatter = format.CompareTop
		case layerIndex < ll.cmpIndex:
			cmpFormatter = format.CompareBottom
		}
		line := fmt.Sprintf("%s %s", cmpFormatter(cmpString), lineFormatter(layer.String()))
		if compressedView {
			line = fmt.Sprintf("%s %s", cmpFormatter(cmpString), lineFormatter(fmt.Sprintf("%d", yIndex + 1)))
		}
		printWidth := intMin(len(line), width)
		format.PrintLine(screen, line, x, y+yIndex, printWidth, tview.AlignLeft, tcell.StyleDefault)
	}
}

func (ll *LayerList) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
	return ll.keyInputHandler.Handle()
}

func (ll *LayerList) Focus(delegate func(p tview.Primitive)) {
	ll.Box.Focus(delegate)
}

func (ll *LayerList) HasFocus() bool {
	return ll.Box.HasFocus()
}

func (ll *LayerList) SetChangedFunc(handler LayerListHandler) *LayerList {
	ll.changed = handler
	return ll
}

func (ll *LayerList) keyUp() bool {
	if ll.cmpIndex <= 0 {
		return false
	}
	ll.cmpIndex--
	if ll.cmpIndex < ll.bufferIndexLowerBound {
		ll.bufferIndexLowerBound--
	}

	logrus.Debugln("keyUp in layers")
	logrus.Debugf("  cmpIndex: %d", ll.cmpIndex)
	logrus.Debugf("  bufferIndexLowerBound: %d", ll.bufferIndexLowerBound)
	return ll.SetLayerIndex(ll.cmpIndex)
}

// TODO (simplify all page increments to rely an a single function)
func (ll *LayerList) keyDown() bool {
	_, _, _, height := ll.Box.GetInnerRect()

	visibleSize := len(ll.GetPrintableLayers())
	if ll.cmpIndex+1 >= visibleSize {
		return false
	}
	ll.cmpIndex++
	if ll