1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-29 22:48:24 +02:00

Add user config gui.showNumstatInFilesView

When enabled, it adds "+n -m" after each file in the Files panel to show how
many lines were added and deleted, as with `git diff --numstat` on the command
line.
This commit is contained in:
johannaschwarz
2024-12-08 12:04:45 +01:00
committed by Stefan Haller
parent f3a5c184e1
commit f455f99705
9 changed files with 174 additions and 33 deletions

View File

@@ -22,12 +22,13 @@ func RenderFileTree(
tree filetree.IFileTree,
submoduleConfigs []*models.SubmoduleConfig,
showFileIcons bool,
showNumstat bool,
) []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, showFileIcons, submoduleConfigs, node)
return getFileLine(isCollapsed, fileNode.GetHasUnstagedChanges(), fileNode.GetHasStagedChanges(), treeDepth, visualDepth, showNumstat, showFileIcons, submoduleConfigs, node)
})
}
@@ -111,6 +112,7 @@ func getFileLine(
hasStagedChanges bool,
treeDepth int,
visualDepth int,
showNumstat,
showFileIcons bool,
submoduleConfigs []*models.SubmoduleConfig,
node *filetree.Node[models.File],
@@ -165,6 +167,12 @@ func getFileLine(
output += theme.DefaultTextColor.Sprint(" (submodule)")
}
if file != nil && showNumstat {
if lineChanges := formatLineChanges(file.LinesAdded, file.LinesDeleted); lineChanges != "" {
output += " " + lineChanges
}
}
return output
}
@@ -186,6 +194,23 @@ func formatFileStatus(file *models.File, restColor style.TextStyle) string {
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,

View File

@@ -19,11 +19,12 @@ func toStringSlice(str string) []string {
func TestRenderFileTree(t *testing.T) {
scenarios := []struct {
name string
root *filetree.FileNode
files []*models.File
collapsedPaths []string
expected []string
name string
root *filetree.FileNode
files []*models.File
collapsedPaths []string
showLineChanges bool
expected []string
}{
{
name: "nil node",
@@ -37,6 +38,22 @@ func TestRenderFileTree(t *testing.T) {
},
expected: []string{" M test"},
},
{
name: "numstat",
files: []*models.File{
{Name: "test", ShortStatus: " M", HasStagedChanges: true, LinesAdded: 1, LinesDeleted: 1},
{Name: "test2", ShortStatus: " M", HasStagedChanges: true, LinesAdded: 1},
{Name: "test3", ShortStatus: " M", HasStagedChanges: true, LinesDeleted: 1},
{Name: "test4", ShortStatus: " M", HasStagedChanges: true, LinesAdded: 0, LinesDeleted: 0},
},
showLineChanges: true,
expected: []string{
" M test +1 -1",
" M test2 +1",
" M test3 -1",
" M test4",
},
},
{
name: "big example",
files: []*models.File{
@@ -72,7 +89,7 @@ M file1
for _, path := range s.collapsedPaths {
viewModel.ToggleCollapsed(path)
}
result := RenderFileTree(viewModel, nil, false)
result := RenderFileTree(viewModel, nil, false, s.showLineChanges)
assert.EqualValues(t, s.expected, result)
})
}