File: files.go

package info (click to toggle)
lazygit 0.50.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,808 kB
  • sloc: sh: 128; makefile: 76
file content (346 lines) | stat: -rw-r--r-- 9,408 bytes parent folder | download
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package presentation

import (
	"strings"

	"github.com/gookit/color"
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/patch"
	"github.com/jesseduffield/lazygit/pkg/config"
	"github.com/jesseduffield/lazygit/pkg/gui/filetree"
	"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
	"github.com/jesseduffield/lazygit/pkg/gui/style"
	"github.com/jesseduffield/lazygit/pkg/theme"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

const (
	EXPANDED_ARROW  = "▼"
	COLLAPSED_ARROW = "▶"
)

func RenderFileTree(
	tree filetree.IFileTree,
	submoduleConfigs []*models.SubmoduleConfig,
	showFileIcons bool,
	showNumstat bool,
	customIconsConfig *config.CustomIconsConfig,
) []string {
	collapsedPaths := tree.CollapsedPaths()
	return renderAux(tree.GetRoot().Raw(), collapsedPaths, -1, -1, func(node *filetree.Node[models.File], treeDepth int, visualDepth int, isCollapsed bool) string {
		fileNode := filetree.NewFileNode(node)

		return getFileLine(isCollapsed, fileNode.GetHasUnstagedChanges(), fileNode.GetHasStagedChanges(), treeDepth, visualDepth, showNumstat, showFileIcons, submoduleConfigs, node, customIconsConfig)
	})
}

func RenderCommitFileTree(
	tree *filetree.CommitFileTreeViewModel,
	patchBuilder *patch.PatchBuilder,
	showFileIcons bool,
	customIconsConfig *config.CustomIconsConfig,
) []string {
	collapsedPaths := tree.CollapsedPaths()
	return renderAux(tree.GetRoot().Raw(), collapsedPaths, -1, -1, func(node *filetree.Node[models.CommitFile], treeDepth int, visualDepth int, isCollapsed bool) string {
		status := commitFilePatchStatus(node, tree, patchBuilder)

		return getCommitFileLine(isCollapsed, treeDepth, visualDepth, node, status, showFileIcons, customIconsConfig)
	})
}

// Returns the status of a commit file in terms of its inclusion in the custom patch
func commitFilePatchStatus(node *filetree.Node[models.CommitFile], tree *filetree.CommitFileTreeViewModel, patchBuilder *patch.PatchBuilder) patch.PatchStatus {
	// This is a little convoluted because we're dealing with either a leaf or a non-leaf.
	// But this code actually applies to both. If it's a leaf, the status will just
	// be whatever status it is, but if it's a non-leaf it will determine its status
	// based on the leaves of that subtree
	if node.EveryFile(func(file *models.CommitFile) bool {
		return patchBuilder.GetFileStatus(file.Path, tree.GetRef().RefName()) == patch.WHOLE
	}) {
		return patch.WHOLE
	} else if node.EveryFile(func(file *models.CommitFile) bool {
		return patchBuilder.GetFileStatus(file.Path, tree.GetRef().RefName()) == patch.UNSELECTED
	}) {
		return patch.UNSELECTED
	} else {
		return patch.PART
	}
}

func renderAux[T any](
	node *filetree.Node[T],
	collapsedPaths *filetree.CollapsedPaths,
	// treeDepth is the depth of the node in the actual file tree. This is different to
	// visualDepth because some directory nodes are compressed e.g. 'pkg/gui/blah' takes
	// up two tree depths, but one visual depth. We need to track these separately,
	// because indentation relies on visual depth, whereas file path truncation
	// relies on tree depth.
	treeDepth int,
	visualDepth int,
	renderLine func(*filetree.Node[T], int, int, bool) string,
) []string {
	if node == nil {
		return []string{}
	}

	isRoot := treeDepth == -1

	if node.IsFile() {
		if isRoot {
			return []string{}
		}
		return []string{renderLine(node, treeDepth, visualDepth, false)}
	}

	arr := []string{}
	if !isRoot {
		isCollapsed := collapsedPaths.IsCollapsed(node.GetInternalPath())
		arr = append(arr, renderLine(node, treeDepth, visualDepth, isCollapsed))
	}

	if collapsedPaths.IsCollapsed(node.GetInternalPath()) {
		return arr
	}

	for _, child := range node.Children {
		arr = append(arr, renderAux(child, collapsedPaths, treeDepth+1+node.CompressionLevel, visualDepth+1, renderLine)...)
	}

	return arr
}

func getFileLine(
	isCollapsed bool,
	hasUnstagedChanges bool,
	hasStagedChanges bool,
	treeDepth int,
	visualDepth int,
	showNumstat,
	showFileIcons bool,
	submoduleConfigs []*models.SubmoduleConfig,
	node *filetree.Node[models.File],
	customIconsConfig *config.CustomIconsConfig,
) string {
	name := fileNameAtDepth(node, treeDepth)
	output := ""

	var nameColor style.TextStyle

	file := node.File

	indentation := strings.Repeat("  ", visualDepth)

	if hasStagedChanges && !hasUnstagedChanges {
		nameColor = style.FgGreen
	} else if hasStagedChanges {
		nameColor = style.FgYellow
	} else {
		nameColor = theme.DefaultTextColor
	}

	if file == nil {
		output += indentation + ""
		arrow := EXPANDED_ARROW
		if isCollapsed {
			arrow = COLLAPSED_ARROW
		}

		arrowStyle := nameColor

		output += arrowStyle.Sprint(arrow) + " "
	} else {
		// Sprinting the space at the end in the specific style is for the sake of
		// when a reverse style is used in the theme, which looks ugly if you just
		// use the default style
		output += indentation + formatFileStatus(file, nameColor) + nameColor.Sprint(" ")
	}

	isSubmodule := file != nil && file.IsSubmodule(submoduleConfigs)
	isLinkedWorktree := file != nil && file.IsWorktree
	isDirectory := file == nil

	if showFileIcons {
		icon := icons.IconForFile(name, isSubmodule, isLinkedWorktree, isDirectory, customIconsConfig)
		paint := color.HEX(icon.Color, false)
		output += paint.Sprint(icon.Icon) + nameColor.Sprint(" ")
	}

	output += nameColor.Sprint(utils.EscapeSpecialChars(name))

	if isSubmodule {
		output += theme.DefaultTextColor.Sprint(" (submodule)")
	}

	if file != nil && showNumstat {
		if lineChanges := formatLineChanges(file.LinesAdded, file.LinesDeleted); lineChanges != "" {
			output += " " + lineChanges
		}
	}

	return output
}

func formatFileStatus(file *models.File, restColor style.TextStyle) string {
	firstChar := file.ShortStatus[0:1]
	firstCharCl := style.FgGreen
	if firstChar == "?" {
		firstCharCl = theme.UnstagedChangesColor
	} else if firstChar == " " {
		firstCharCl = restColor
	}

	secondChar := file.ShortStatus[1:2]
	secondCharCl := theme.UnstagedChangesColor
	if secondChar == " " {
		secondCharCl = restColor
	}

	return firstCharCl.Sprint(firstChar) + secondCharCl.Sprint(secondChar)
}

func formatLineChanges(linesAdded, linesDeleted int) string {
	output := ""

	if linesAdded != 0 {
		output += style.FgGreen.Sprintf("+%d", linesAdded)
	}

	if linesDeleted != 0 {
		if output != "" {
			output += " "
		}
		output += style.FgRed.Sprintf("-%d", linesDeleted)
	}

	return output
}

func getCommitFileLine(
	isCollapsed bool,
	treeDepth int,
	visualDepth int,
	node *filetree.Node[models.CommitFile],
	status patch.PatchStatus,
	showFileIcons bool,
	customIconsConfig *config.CustomIconsConfig,
) string {
	indentation := strings.Repeat("  ", visualDepth)
	name := commitFileNameAtDepth(node, treeDepth)
	commitFile := node.File
	output := indentation

	isDirectory := commitFile == nil

	nameColor := theme.DefaultTextColor

	switch status {
	case patch.WHOLE:
		nameColor = style.FgGreen
	case patch.PART:
		nameColor = style.FgYellow
	case patch.UNSELECTED:
		nameColor = theme.DefaultTextColor
	}

	if isDirectory {
		arrow := EXPANDED_ARROW
		if isCollapsed {
			arrow = COLLAPSED_ARROW
		}

		output += nameColor.Sprint(arrow) + " "
	} else {
		var symbol string
		symbolStyle := nameColor

		switch status {
		case patch.WHOLE:
			symbol = "●"
		case patch.PART:
			symbol = "◐"
		case patch.UNSELECTED:
			symbol = commitFile.ChangeStatus
			symbolStyle = getColorForChangeStatus(symbol)
		}

		output += symbolStyle.Sprint(symbol) + " "
	}

	name = utils.EscapeSpecialChars(name)
	isSubmodule := false
	isLinkedWorktree := false

	if showFileIcons {
		icon := icons.IconForFile(name, isSubmodule, isLinkedWorktree, isDirectory, customIconsConfig)
		paint := color.HEX(icon.Color, false)
		output += paint.Sprint(icon.Icon) + " "
	}

	output += nameColor.Sprint(name)
	return output
}

func getColorForChangeStatus(changeStatus string) style.TextStyle {
	switch changeStatus {
	case "A":
		return style.FgGreen
	case "M", "R":
		return style.FgYellow
	case "D":
		return theme.UnstagedChangesColor
	case "C":
		return style.FgCyan
	case "T":
		return style.FgMagenta
	default:
		return theme.DefaultTextColor
	}
}

func fileNameAtDepth(node *filetree.Node[models.File], depth int) string {
	splitName := split(node.GetInternalPath())
	if depth == 0 && splitName[0] == "." {
		if len(splitName) == 1 {
			return "/"
		}
		depth = 1
	}
	name := join(splitName[depth:])

	if node.File != nil && node.File.IsRename() {
		splitPrevName := split("./" + node.File.PreviousPath)

		prevName := node.File.PreviousPath
		// if the file has just been renamed inside the same directory, we can shave off
		// the prefix for the previous path too. Otherwise we'll keep it unchanged
		sameParentDir := len(splitName) == len(splitPrevName) && join(splitName[0:depth]) == join(splitPrevName[0:depth])
		if sameParentDir {
			prevName = join(splitPrevName[depth:])
		}

		return prevName + " → " + name
	}

	return name
}

func commitFileNameAtDepth(node *filetree.Node[models.CommitFile], depth int) string {
	splitName := split(node.GetInternalPath())
	if depth == 0 && splitName[0] == "." {
		if len(splitName) == 1 {
			return "/"
		}
		depth = 1
	}
	name := join(splitName[depth:])

	return name
}

func split(str string) []string {
	return strings.Split(str, "/")
}

func join(strs []string) string {
	return strings.Join(strs, "/")
}