summaryrefslogtreecommitdiffstats
path: root/runtime/ui/components/filetree_primative.go
blob: 5a38efecb60df92507105e753be0132a5fbfc404 (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
package components

import (
	"bytes"
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
	"github.com/sirupsen/logrus"
	"github.com/wagoodman/dive/dive/filetree"
	"io"
	"regexp"
	"strings"
)

type TreeView struct {
	*tview.Box
	// TODO: make me an interface

	tree *filetree.FileTree


	// Note that the following two fields are distinct
	// treeIndex is the index about where we are in the current fileTree
	// this should be updated every keypresws
	treeIndex int

	// bufferIndex is the index about where we are in the Buffer,
	// basically lets us scroll down but NOT shift the buffer
	bufferIndexLowerBound int
	bufferIndex int

	//changed func(index int, mainText string, shortcut rune)
}

func NewTreeView(tree *filetree.FileTree) *TreeView {
	return &TreeView{
		Box: tview.NewBox(),
		tree: tree,
	}
}

// TODO: make these keys configurable
func (t *TreeView) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
	return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
		switch event.Key() {
		case tcell.KeyUp:
			t.keyUp()
		case tcell.KeyDown:
			t.keyDown()
		}
		switch event.Rune() {
		case ' ':
			t.spaceDown()
		}
		//t.changed(t.cmpIndex, t.layers[t.cmpIndex], event.Rune())
	})
}

func (t *TreeView) SetTree(newTree *filetree.FileTree) *TreeView {
	// preserve collapsed nodes based on path
	collapsedList := map[string]interface{}{}

	evaluateFunc := func(node *filetree.FileNode) bool {
		if node.Parent != nil && (node.Parent.Data.ViewInfo.Collapsed || node.Parent.Data.ViewInfo.Hidden) {
			return false
		}
		return true
	}

	t.tree.VisitDepthParentFirst(func(node *filetree.FileNode) error {
		if node.Data.ViewInfo.Collapsed {
			collapsedList[node.Path()] = true
		}
		return nil
	},evaluateFunc)

	newTree.VisitDepthParentFirst(func(node *filetree.FileNode) error {
		_, ok := collapsedList[node.Path()]
		if ok {
			node.Data.ViewInfo.Collapsed = true
		}
		return nil
	}, evaluateFunc)

	t.tree = newTree

	return t
}

func (t *TreeView) GetTree(tree *filetree.FileTree) *filetree.FileTree {
	return t.tree
}

func (t *TreeView) Focus(delegate func(p tview.Primitive)) {
	t.Box.Focus(delegate)
}

func (t *TreeView) HasFocus() bool {
	return t.Box.HasFocus()
}


// Private helper methods

func (t *TreeView) spaceDown() bool {
	node := t.getAbsPositionNode(nil)
	if node != nil && node.Data.FileInfo.IsDir {
		node.Data.ViewInfo.Collapsed = !node.Data.ViewInfo.Collapsed
		return true
	}
	return false
}

// getAbsPositionNode determines the selected screen cursor's location in the file tree, returning the selected FileNode.
func (t *TreeView) getAbsPositionNode(filterRegex *regexp.Regexp) (node *filetree.FileNode) {
	var visitor func(*filetree.FileNode) error
	var evaluator func(*filetree.FileNode) bool
	var dfsCounter int

	visitor = func(curNode *filetree.FileNode) error {
		if dfsCounter == t.treeIndex {
			node = curNode
		}
		dfsCounter++
		return nil
	}

	evaluator = func(curNode *filetree.FileNode) bool {
		regexMatch := true
		if filterRegex != nil {
			match := filterRegex.Find([]byte(curNode.Path()))
			regexMatch = match != nil
		}
		return !curNode.Parent.Data.ViewInfo.Collapsed && !curNode.Data.ViewInfo.Hidden && regexMatch
	}

	err := t.tree.VisitDepthParentFirst(visitor, evaluator)
	if err != nil {
		logrus.Errorf("unable to get node position: %+v", err)
	}

	return node
}

func (t *TreeView) keyDown() bool {

	_, _, _, height := t.Box.GetInnerRect()

	// treeIndex is the index about where we are in the current file
	if t.treeIndex >= t.tree.VisibleSize() {
		return false
	}
	t.treeIndex++
	if t.treeIndex > height {
		t.bufferIndexLowerBound++
	}
	t.bufferIndex++
	if t.bufferIndex > height {
		t.bufferIndex = height
	}
	return true
}

func (t *TreeView) keyUp() bool {
	if t.treeIndex <= 0 {
		return false
	}
	t.treeIndex--
	if t.treeIndex < t.bufferIndexLowerBound {
		t.bufferIndexLowerBound--
	}
	if t.bufferIndex > 0 {
		t.bufferIndex--
	}
	return true
}

func (t *TreeView) bufferIndexUpperBound() int {
	_,_, _, height := t.Box.GetInnerRect()
	return t.bufferIndexLowerBound + height

}

func (t *TreeView) Draw(screen tcell.Screen) {
	t.Box.Draw(screen)

	x, y, width, height := t.Box.GetInnerRect()

	// TODO add switch for showing attributes.
	treeString := t.tree.StringBetween(t.bufferIndexLowerBound, t.bufferIndexUpperBound(), false)
	lines := strings.Split(treeString, "\n")

	// update the contents
	for yIndex, line := range lines {
		if yIndex >= height {
			break
		}
		// Strip out ansi colors, Tview cannot use these
		stripLine := bytes.NewBuffer(nil)
		w := tview.ANSIWriter(stripLine)
		if _, err := io.Copy(w, strings.NewReader(line)); err != nil  {
			//TODO: handle panic gracefully
			panic(err)
		}

		tview.Print(screen, stripLine.String(), x, y+yIndex, width, tview.AlignLeft, tcell.ColorDefault)
		for xIndex := 0; xIndex < width; xIndex++ {
			m, c, style, _ := screen.GetContent(x+xIndex, y+yIndex)
			// TODO make these background an forground colors flexable
			style = style.Background(tcell.ColorWhite).Foreground(tcell.ColorBlack).Bold(true)
			if yIndex == t.bufferIndex {
				screen.SetContent(x+xIndex, y+yIndex, m, c, style)
				screen.SetContent(x+xIndex, y+yIndex, m, c, style)
			} else if yIndex > t.bufferIndex {
				break
			}
		}
	}

}