summaryrefslogtreecommitdiffstats
path: root/runtime/ui/viewmodels/layers_view_model.go
blob: 4418152a8c24b4a0db046772d9c882fb43505485 (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
package viewmodels

import (
	"fmt"

	"github.com/sirupsen/logrus"
	"github.com/wagoodman/dive/dive/filetree"
	"github.com/wagoodman/dive/dive/image"
)

const (
	CompareSingleLayer LayerCompareMode = iota
	CompareAllLayers
)

type LayerCompareMode int

type LayersViewModel struct {
	mode   LayerCompareMode
	layers []*image.Layer
	index  int
}

func NewLayersViewModel(layers []*image.Layer) *LayersViewModel {
	return &LayersViewModel{
		mode:   CompareSingleLayer,
		layers: layers,
	}
}

func (lm *LayersViewModel) GetMode() LayerCompareMode {
	return lm.mode
}

func (lm *LayersViewModel) SwitchMode() {
	lm.mode = (lm.mode + 1) % 2 //this just cycles the mode
}

func (lm *LayersViewModel) GetCompareIndicies() filetree.TreeIndexKey {
	intMax := func(i, j int) int {
		if i > j {
			return i
		}
		return j
	}

	bottomStart := 0
	bottomStop := 0
	if lm.mode == CompareSingleLayer {
		bottomStop = intMax(lm.index-1, 0)
	}
	return filetree.NewTreeIndexKey(bottomStart, bottomStop, lm.index, lm.index)
}

func (lm *LayersViewModel) SetLayerIndex(index int) bool {
	if 0 <= index && index < len(lm.layers) {
		logrus.Debug("setting index, old: %d, new: %d", lm.index, index)
		lm.index = index
		return true
	}
	return false
}

func (lm *LayersViewModel) GetPrintableLayers() []fmt.Stringer {
	var result []fmt.Stringer
	for _, layer := range lm.layers {
		result = append(result, fmt.Stringer(layer))
	}
	return result
}

func (lm *LayersViewModel) GetCurrentLayer() *image.Layer {
	return lm.layers[lm.index]
}