summaryrefslogtreecommitdiffstats
path: root/runtime/ui/app.go
blob: b81d2b8c9fc64eeb5afa86fe689223d4958b3075 (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
package ui

import (
	"sync"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
	"github.com/wagoodman/dive/dive/filetree"
	"github.com/wagoodman/dive/dive/image"
	"github.com/wagoodman/dive/runtime/ui/components"
	"github.com/wagoodman/dive/runtime/ui/viewmodels"
	"go.uber.org/zap"
)

const debug = false

// type global
var (
	once         sync.Once
	appSingleton *diveApp
)

type diveApp struct {
	app        *tview.Application
	layers     tview.Primitive
	fileTree   tview.Primitive
	filterView tview.Primitive
}

func newApp(app *tview.Application, analysis *image.AnalysisResult, cache filetree.Comparer, isCNB bool) (*diveApp, error) {
	var err error
	once.Do(func() {
		config := components.NewKeyConfig()

		// ensure the background color is inherited from the terminal emulator
		//tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
		//tview.Styles.PrimaryTextColor = tcell.ColorDefault

		//initialize viewmodels
		filterViewModel := viewmodels.NewFilterViewModel(nil)
		var layerModel viewmodels.LayersModel
		var layerDetailsBox *components.Wrapper
		if isCNB {
			cnbLayerViewModel := viewmodels.NewCNBLayersViewModel(analysis.Layers, analysis.BOMMapping)
			cnbLayerDetailsView := components.NewCNBLayerDetailsView(cnbLayerViewModel).Setup()
			layerModel = cnbLayerViewModel
			layerDetailsBox = components.NewWrapper("CNB Layer Details", "", cnbLayerDetailsView).Setup()
		} else {
			layerViewModel := viewmodels.NewLayersViewModel(analysis.Layers)
			regularLayerDetailsView := components.NewLayerDetailsView(layerViewModel).Setup()
			layerModel = layerViewModel
			layerDetailsBox = components.NewWrapper("Layer Details", "", regularLayerDetailsView).Setup()
		}
		layerDetailsBox.SetVisibility(components.MinHeightVisibility(10))

		//layerViewModel := viewmodels.NewLayersViewModel(analysis.Layers)
		treeViewModel, err := viewmodels.NewTreeViewModel(cache, layerModel, filterViewModel)
		if err != nil {
			// TODO: replace panic with a reasonable exit strategy
			panic(err)
		}

		// initialize views
		imageDetailsView := components.NewImageDetailsView(analysis)
		imageDetailsBox := components.NewWrapper("Image Details", "", imageDetailsView).Setup()
		imageDetailsBox.SetVisibility(components.MinHeightVisibility(10))

		filterView := components.NewFilterView(treeViewModel).Setup()

		layersView := components.NewLayerList(treeViewModel).Setup()
		layersBox := components.NewWrapper("Layers", "subtitle!", layersView).Setup()

		fileTreeView := components.NewTreeView(treeViewModel)
		fileTreeView = fileTreeView.Setup(config)
		fileTreeBox := components.NewWrapper("Current Layer Contents", "subtitle!", fileTreeView).Setup()

		// Implementation notes: should we factor out this setup??
		// Probably yes, but difficult to make this both easy to setup & mutable
		leftVisibleGrid := components.NewVisibleFlex()
		leftVisibleGrid.SetDirection(tview.FlexRow)
		rightVisibleGrid := components.NewVisibleFlex()
		rightVisibleGrid.SetDirection(tview.FlexRow)
		totalVisibleGrid := components.NewVisibleFlex()

		leftVisibleGrid.AddItem(layersBox, 0, 3, true).
			AddItem(layerDetailsBox, 0, 1, false).
			AddItem(imageDetailsBox, 0, 1, false).
			SetConsumers(layerDetailsBox, layersBox).
			SetConsumers(imageDetailsBox, layersBox)

		rightVisibleGrid.AddItem(fileTreeBox, 0, 1, false).
			AddItem(filterView, 1, 0, false).
			SetConsumers(filterView, fileTreeBox)

		totalVisibleGrid.AddItem(leftVisibleGrid, 0, 1, true).
			AddItem(rightVisibleGrid, 0, 1, false)

		appSingleton = &diveApp{
			app:      app,
			fileTree: fileTreeBox,
			layers:   layersBox,
		}

		switchFocus := func(event *tcell.EventKey) *tcell.EventKey {
			var result *tcell.EventKey = nil
			switch event.Key() {
			case tcell.KeyTAB:
				if appSingleton.app.GetFocus() == appSingleton.layers {
					appSingleton.app.SetFocus(appSingleton.fileTree)
				} else {
					appSingleton.app.SetFocus(appSingleton.layers)
				}
			case tcell.KeyCtrlF:
				if filterView.HasFocus() {
					filterView.Blur()
					appSingleton.app.SetFocus(fileTreeBox)
				} else {
					appSingleton.app.SetFocus(filterView)
				}

			default:
				result = event
			}
			return result
		}

		totalVisibleGrid.SetInputCapture(switchFocus)

		app.SetRoot(totalVisibleGrid, true)
		app.SetFocus(totalVisibleGrid)
	})

	return appSingleton, err
}

// Run is the UI entrypoint.
func Run(analysis *image.AnalysisResult, treeStack filetree.Comparer, isCNB bool) error {
	cfg := zap.NewDevelopmentConfig()
	os.MkdirAll("/tmp/dive", os.ModePerm)
	cfg.OutputPaths = []string{"/tmp/dive/debug.out"}
	logger, err := cfg.Build()
	if err != nil {
		panic(err)
	}
	zap.ReplaceGlobals(logger)
	defer logger.Sync() // flushes buffer, if any
	logger.Sugar().Debug("Debug Start")

	zap.S().Info("Starting Hidden Flex Program")

	app := tview.NewApplication()
	_, err = newApp(app, analysis, treeStack, isCNB)
	if err != nil {
		return err
	}

	if err = appSingleton.app.Run(); err != nil {
		zap.S().Info("app error: ", err.Error())
		return err
	}
	zap.S().Info("app run loop exited")
	return nil
}