summaryrefslogtreecommitdiffstats
path: root/pkg/gui/filetree/commit_file_change_node.go
blob: 011c1d954b0148cd9660202a8af66cd9a9d88ed7 (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
package filetree

import (
	"os"
	"path/filepath"
	"strings"

	"github.com/jesseduffield/lazygit/pkg/commands/models"
)

type CommitFileChangeNode struct {
	Children         []*CommitFileChangeNode
	File             *models.CommitFile
	Path             string // e.g. '/path/to/mydir'
	CompressionLevel int    // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode
}

// methods satisfying ListItem interface

func (s *CommitFileChangeNode) ID() string {
	return s.GetPath()
}

func (s *CommitFileChangeNode) Description() string {
	return s.GetPath()
}

// methods satisfying INode interface

func (s *CommitFileChangeNode) IsLeaf() bool {
	return s.File != nil
}

func (s *CommitFileChangeNode) GetPath() string {
	return s.Path
}

func (s *CommitFileChangeNode) GetChildren() []INode {
	result := make([]INode, len(s.Children))
	for i, child := range s.Children {
		result[i] = child
	}

	return result
}

func (s *CommitFileChangeNode) SetChildren(children []INode) {
	castChildren := make([]*CommitFileChangeNode, len(children))
	for i, child := range children {
		castChildren[i] = child.(*CommitFileChangeNode)
	}

	s.Children = castChildren
}

func (s *CommitFileChangeNode) GetCompressionLevel() int {
	return s.CompressionLevel
}

func (s *CommitFileChangeNode) SetCompressionLevel(level int) {
	s.CompressionLevel = level
}

// methods utilising generic functions for INodes

func (s *CommitFileChangeNode) Sort() {
	sortNode(s)
}

func (s *CommitFileChangeNode) ForEachFile(cb func(*models.CommitFile) error) error {
	return forEachLeaf(s, func(n INode) error {
		castNode := n.(*CommitFileChangeNode)
		return cb(castNode.File)
	})
}

func (s *CommitFileChangeNode) Any(test func(node *CommitFileChangeNode) bool) bool {
	return any(s, func(n INode) bool {
		castNode := n.(*CommitFileChangeNode)
		return test(castNode)
	})
}

func (s *CommitFileChangeNode) Every(test func(node *CommitFileChangeNode) bool) bool {
	return every(s, func(n INode) bool {
		castNode := n.(*CommitFileChangeNode)
		return test(castNode)
	})
}

func (s *CommitFileChangeNode) EveryFile(test func(file *models.CommitFile) bool) bool {
	return every(s, func(n INode) bool {
		castNode := n.(*CommitFileChangeNode)

		return castNode.File == nil || test(castNode.File)
	})
}

func (n *CommitFileChangeNode) Flatten(collapsedPaths map[string]bool) []*CommitFileChangeNode {
	results := flatten(n, collapsedPaths)
	nodes := make([]*CommitFileChangeNode, len(results))
	for i, result := range results {
		nodes[i] = result.(*CommitFileChangeNode)
	}

	return nodes
}

func (node *CommitFileChangeNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileChangeNode {
	return getNodeAtIndex(node, index, collapsedPaths).(*CommitFileChangeNode)
}

func (node *CommitFileChangeNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) {
	return getIndexForPath(node, path, collapsedPaths)
}

func (node *CommitFileChangeNode) Size(collapsedPaths map[string]bool) int {
	return size(node, collapsedPaths)
}

func (s *CommitFileChangeNode) Compress() {
	// with these functions I try to only have type conversion code on the actual struct,
	// but comparing interface values to nil is fraught with danger so I'm duplicating
	// that code here.
	if s == nil {
		return
	}

	compressAux(s)
}

// This ignores the root
func (node *CommitFileChangeNode) GetPathsMatching(test func(*CommitFileChangeNode) bool) []string {
	return getPathsMatching(node, func(n INode) bool {
		return test(n.(*CommitFileChangeNode))
	})
}

func (s *CommitFileChangeNode) GetLeaves() []*CommitFileChangeNode {
	leaves := getLeaves(s)
	castLeaves := make([]*CommitFileChangeNode, len(leaves))
	for i := range leaves {
		castLeaves[i] = leaves[i].(*CommitFileChangeNode)
	}

	return castLeaves
}

// extra methods

func (s *CommitFileChangeNode) AnyFile(test func(file *models.CommitFile) bool) bool {
	return s.Any(func(node *CommitFileChangeNode) bool {
		return node.IsLeaf() && test(node.File)
	})
}

func (s *CommitFileChangeNode) NameAtDepth(depth int) string {
	splitName := strings.Split(s.Path, string(os.PathSeparator))
	name := filepath.Join(splitName[depth:]...)

	return name
}