From 82d5266547b2f406996751e3da70a3c68b85e678 Mon Sep 17 00:00:00 2001 From: johannaschwarz Date: Mon, 21 Oct 2024 09:59:07 +0000 Subject: [PATCH 1/6] Add number of line changes to file --- pkg/commands/git_commands/file_loader.go | 60 ++++++++++++++++++++++++ pkg/commands/models/file.go | 2 + pkg/gui/presentation/files.go | 25 ++++++++++ 3 files changed, 87 insertions(+) diff --git a/pkg/commands/git_commands/file_loader.go b/pkg/commands/git_commands/file_loader.go index 72329543a77..8555de52594 100644 --- a/pkg/commands/git_commands/file_loader.go +++ b/pkg/commands/git_commands/file_loader.go @@ -3,6 +3,7 @@ package git_commands import ( "fmt" "path/filepath" + "strconv" "strings" "github.com/jesseduffield/lazygit/pkg/commands/models" @@ -48,6 +49,11 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File } files := []*models.File{} + fileDiffs, err := self.getFileDiffs() + if err != nil { + self.Log.Error(err) + } + for _, status := range statuses { if strings.HasPrefix(status.StatusString, "warning") { self.Log.Warningf("warning when calling git status: %s", status.StatusString) @@ -60,6 +66,11 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File DisplayString: status.StatusString, } + if diff, ok := fileDiffs[status.Name]; ok { + file.LinesAdded = diff.LinesAdded + file.LinesDeleted = diff.LinesDeleted + } + models.SetStatusFields(file, status.Change) files = append(files, file) } @@ -87,6 +98,45 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File return files } +type FileDiff struct { + LinesAdded int + LinesDeleted int +} + +func (fileLoader *FileLoader) getFileDiffs() (map[string]FileDiff, error) { + diffs, err := fileLoader.gitDiffNumStat() + if err != nil { + return nil, err + } + + splitLines := strings.Split(diffs, "\x00") + + fileDiffs := map[string]FileDiff{} + for _, line := range splitLines { + splitLine := strings.Split(line, "\t") + if len(splitLine) != 3 { + continue + } + + linesAdded, err := strconv.Atoi(splitLine[0]) + if err != nil { + continue + } + linesDeleted, err := strconv.Atoi(splitLine[1]) + if err != nil { + continue + } + + fileName := splitLine[2] + fileDiffs[fileName] = FileDiff{ + LinesAdded: linesAdded, + LinesDeleted: linesDeleted, + } + } + + return fileDiffs, nil +} + // GitStatus returns the file status of the repo type GitStatusOptions struct { NoRenames bool @@ -100,6 +150,16 @@ type FileStatus struct { PreviousName string } +func (fileLoader *FileLoader) gitDiffNumStat() (string, error) { + return fileLoader.cmd.New( + NewGitCmd("diff"). + Arg("--numstat"). + Arg("-z"). + Arg("HEAD"). + ToArgv(), + ).DontLog().RunWithOutput() +} + func (self *FileLoader) gitStatus(opts GitStatusOptions) ([]FileStatus, error) { cmdArgs := NewGitCmd("status"). Arg(opts.UntrackedFilesArg). diff --git a/pkg/commands/models/file.go b/pkg/commands/models/file.go index 45f1ec5d7f0..4be424e2235 100644 --- a/pkg/commands/models/file.go +++ b/pkg/commands/models/file.go @@ -19,6 +19,8 @@ type File struct { HasInlineMergeConflicts bool DisplayString string ShortStatus string // e.g. 'AD', ' A', 'M ', '??' + LinesDeleted int + LinesAdded int // If true, this must be a worktree folder IsWorktree bool diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index 5941934c60e..0c86c01c11d 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -1,6 +1,7 @@ package presentation import ( + "strconv" "strings" "github.com/gookit/color" @@ -163,6 +164,10 @@ func getFileLine( if isSubmodule { output += theme.DefaultTextColor.Sprint(" (submodule)") + } else { + if lineChanges := formatLineChanges(file); lineChanges != "" { + output += " " + lineChanges + } } return output @@ -186,6 +191,26 @@ func formatFileStatus(file *models.File, restColor style.TextStyle) string { return firstCharCl.Sprint(firstChar) + secondCharCl.Sprint(secondChar) } +func formatLineChanges(file *models.File) string { + if file == nil { + return "" + } + output := "" + + if file.LinesAdded != 0 { + output += style.FgGreen.Sprint("+" + strconv.Itoa(file.LinesAdded)) + } + + if file.LinesDeleted != 0 { + if output != "" { + output += " " + } + output += style.FgRed.Sprint("−" + strconv.Itoa(file.LinesDeleted)) + } + + return output +} + func getCommitFileLine( isCollapsed bool, treeDepth int, From 61898bc0e00e2f3404b08ddeff9579667d9f664a Mon Sep 17 00:00:00 2001 From: johannaschwarz Date: Sat, 26 Oct 2024 10:08:56 +0200 Subject: [PATCH 2/6] Add showNumberOfLineChanges to user config --- docs/Config.md | 3 +++ pkg/config/user_config.go | 3 +++ pkg/gui/context/working_tree_context.go | 3 ++- pkg/gui/presentation/files.go | 14 ++++++++------ schema/config.json | 7 ++++++- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 432e701863e..3dada749913 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -164,6 +164,9 @@ gui: # This can be toggled from within Lazygit with the '~' key, but that will not change the default. showFileTree: true + # If true, show the number of lines changed per file in the Files view + showNumberOfLineChanges: true + # If true, show a random tip in the command log when Lazygit starts showRandomTip: true diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 2a8778ef9db..46b4c45effe 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -109,6 +109,8 @@ type GuiConfig struct { // If true, display the files in the file views as a tree. If false, display the files as a flat list. // This can be toggled from within Lazygit with the '~' key, but that will not change the default. ShowFileTree bool `yaml:"showFileTree"` + // If true, show the number of lines changed per file in the Files view + ShowNumberOfLineChanges bool `yaml:"showNumberOfLineChanges"` // If true, show a random tip in the command log when Lazygit starts ShowRandomTip bool `yaml:"showRandomTip"` // If true, show the command log @@ -712,6 +714,7 @@ func GetDefaultConfig() *UserConfig { ShowBottomLine: true, ShowPanelJumps: true, ShowFileTree: true, + ShowNumberOfLineChanges: true, ShowRandomTip: true, ShowIcons: false, NerdFontsVersion: "", diff --git a/pkg/gui/context/working_tree_context.go b/pkg/gui/context/working_tree_context.go index 88d2ab9fef6..381fb32193f 100644 --- a/pkg/gui/context/working_tree_context.go +++ b/pkg/gui/context/working_tree_context.go @@ -30,7 +30,8 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext { getDisplayStrings := func(_ int, _ int) [][]string { showFileIcons := icons.IsIconEnabled() && c.UserConfig().Gui.ShowFileIcons - lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons) + showNumberOfLineChanges := c.UserConfig().Gui.ShowNumberOfLineChanges + lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons, showNumberOfLineChanges) return lo.Map(lines, func(line string, _ int) []string { return []string{line} }) diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index 0c86c01c11d..bed4b227dc8 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -22,13 +22,14 @@ const ( func RenderFileTree( tree filetree.IFileTree, submoduleConfigs []*models.SubmoduleConfig, - showFileIcons bool, + showFileIcons, + showNumberOfLineChanges 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, showNumberOfLineChanges, showFileIcons, submoduleConfigs, node) }) } @@ -112,6 +113,7 @@ func getFileLine( hasStagedChanges bool, treeDepth int, visualDepth int, + showNumberOfLineChanges, showFileIcons bool, submoduleConfigs []*models.SubmoduleConfig, node *filetree.Node[models.File], @@ -164,10 +166,10 @@ func getFileLine( if isSubmodule { output += theme.DefaultTextColor.Sprint(" (submodule)") - } else { - if lineChanges := formatLineChanges(file); lineChanges != "" { - output += " " + lineChanges - } + } + + if lineChanges := formatLineChanges(file); showNumberOfLineChanges && lineChanges != "" { + output += " " + lineChanges } return output diff --git a/schema/config.json b/schema/config.json index 78e932f9d3e..e69fc508d65 100644 --- a/schema/config.json +++ b/schema/config.json @@ -293,6 +293,11 @@ "description": "If true, display the files in the file views as a tree. If false, display the files as a flat list.\nThis can be toggled from within Lazygit with the '~' key, but that will not change the default.", "default": true }, + "showNumberOfLineChanges": { + "type": "boolean", + "description": "If true, show the number of lines changed per file in the Files view", + "default": true + }, "showRandomTip": { "type": "boolean", "description": "If true, show a random tip in the command log when Lazygit starts", @@ -1720,4 +1725,4 @@ }, "additionalProperties": false, "type": "object" -} \ No newline at end of file +} From acefc242ec4d354496747f85f5077cb4855b2e08 Mon Sep 17 00:00:00 2001 From: johannaschwarz Date: Sat, 26 Oct 2024 12:03:00 +0000 Subject: [PATCH 3/6] Add tests --- pkg/commands/git_commands/config.go | 4 + pkg/commands/git_commands/file_loader.go | 13 +++- pkg/commands/git_commands/file_loader_test.go | 73 ++++++++++++------- pkg/gui/presentation/files.go | 2 +- pkg/gui/presentation/files_test.go | 23 ++++-- schema/config.json | 2 +- 6 files changed, 79 insertions(+), 38 deletions(-) diff --git a/pkg/commands/git_commands/config.go b/pkg/commands/git_commands/config.go index 9771f01d37a..da709254d89 100644 --- a/pkg/commands/git_commands/config.go +++ b/pkg/commands/git_commands/config.go @@ -111,3 +111,7 @@ func (self *ConfigCommands) GetCoreCommentChar() byte { func (self *ConfigCommands) GetRebaseUpdateRefs() bool { return self.gitConfig.GetBool("rebase.updateRefs") } + +func (cfgCommands *ConfigCommands) GetShowNumberOfLineChanges() bool { + return cfgCommands.UserConfig().Gui.ShowNumberOfLineChanges +} diff --git a/pkg/commands/git_commands/file_loader.go b/pkg/commands/git_commands/file_loader.go index 8555de52594..e7c6aa454a4 100644 --- a/pkg/commands/git_commands/file_loader.go +++ b/pkg/commands/git_commands/file_loader.go @@ -12,6 +12,7 @@ import ( type FileLoaderConfig interface { GetShowUntrackedFiles() string + GetShowNumberOfLineChanges() bool } type FileLoader struct { @@ -31,7 +32,8 @@ func NewFileLoader(gitCommon *GitCommon, cmd oscommands.ICmdObjBuilder, config F } type GetStatusFileOptions struct { - NoRenames bool + NoRenames bool + GetFileDiffs bool } func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File { @@ -49,9 +51,12 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File } files := []*models.File{} - fileDiffs, err := self.getFileDiffs() - if err != nil { - self.Log.Error(err) + fileDiffs := map[string]FileDiff{} + if self.config.GetShowNumberOfLineChanges() { + fileDiffs, err = self.getFileDiffs() + if err != nil { + self.Log.Error(err) + } } for _, status := range statuses { diff --git a/pkg/commands/git_commands/file_loader_test.go b/pkg/commands/git_commands/file_loader_test.go index 5a9f15700ed..fe091bb8a4b 100644 --- a/pkg/commands/git_commands/file_loader_test.go +++ b/pkg/commands/git_commands/file_loader_test.go @@ -11,29 +11,35 @@ import ( func TestFileGetStatusFiles(t *testing.T) { type scenario struct { - testName string - similarityThreshold int - runner oscommands.ICmdObjRunner - expectedFiles []*models.File + testName string + similarityThreshold int + runner oscommands.ICmdObjRunner + showNumberOfLineChanges bool + expectedFiles []*models.File } scenarios := []scenario{ { - "No files found", - 50, - oscommands.NewFakeRunner(t). + testName: "No files found", + similarityThreshold: 50, + runner: oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, "", nil), - []*models.File{}, + expectedFiles: []*models.File{}, }, { - "Several files found", - 50, - oscommands.NewFakeRunner(t). + testName: "Several files found", + similarityThreshold: 50, + runner: oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, "MM file1.txt\x00A file3.txt\x00AM file2.txt\x00?? file4.txt\x00UU file5.txt", nil, + ). + ExpectGitArgs([]string{"diff", "--numstat", "-z", "HEAD"}, + "4\t1\tfile1.txt\x001\t0\tfile2.txt\x002\t2\tfile3.txt\x000\t2\tfile4.txt\x002\t2\tfile5.txt", + nil, ), - []*models.File{ + showNumberOfLineChanges: true, + expectedFiles: []*models.File{ { Name: "file1.txt", HasStagedChanges: true, @@ -45,6 +51,8 @@ func TestFileGetStatusFiles(t *testing.T) { HasInlineMergeConflicts: false, DisplayString: "MM file1.txt", ShortStatus: "MM", + LinesAdded: 4, + LinesDeleted: 1, }, { Name: "file3.txt", @@ -57,6 +65,8 @@ func TestFileGetStatusFiles(t *testing.T) { HasInlineMergeConflicts: false, DisplayString: "A file3.txt", ShortStatus: "A ", + LinesAdded: 2, + LinesDeleted: 2, }, { Name: "file2.txt", @@ -69,6 +79,8 @@ func TestFileGetStatusFiles(t *testing.T) { HasInlineMergeConflicts: false, DisplayString: "AM file2.txt", ShortStatus: "AM", + LinesAdded: 1, + LinesDeleted: 0, }, { Name: "file4.txt", @@ -81,6 +93,8 @@ func TestFileGetStatusFiles(t *testing.T) { HasInlineMergeConflicts: false, DisplayString: "?? file4.txt", ShortStatus: "??", + LinesAdded: 0, + LinesDeleted: 2, }, { Name: "file5.txt", @@ -93,15 +107,17 @@ func TestFileGetStatusFiles(t *testing.T) { HasInlineMergeConflicts: true, DisplayString: "UU file5.txt", ShortStatus: "UU", + LinesAdded: 2, + LinesDeleted: 2, }, }, }, { - "File with new line char", - 50, - oscommands.NewFakeRunner(t). + testName: "File with new line char", + similarityThreshold: 50, + runner: oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, "MM a\nb.txt", nil), - []*models.File{ + expectedFiles: []*models.File{ { Name: "a\nb.txt", HasStagedChanges: true, @@ -117,14 +133,14 @@ func TestFileGetStatusFiles(t *testing.T) { }, }, { - "Renamed files", - 50, - oscommands.NewFakeRunner(t). + testName: "Renamed files", + similarityThreshold: 50, + runner: oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, "R after1.txt\x00before1.txt\x00RM after2.txt\x00before2.txt", nil, ), - []*models.File{ + expectedFiles: []*models.File{ { Name: "after1.txt", PreviousName: "before1.txt", @@ -154,14 +170,14 @@ func TestFileGetStatusFiles(t *testing.T) { }, }, { - "File with arrow in name", - 50, - oscommands.NewFakeRunner(t). + testName: "File with arrow in name", + similarityThreshold: 50, + runner: oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, `?? a -> b.txt`, nil, ), - []*models.File{ + expectedFiles: []*models.File{ { Name: "a -> b.txt", HasStagedChanges: false, @@ -188,7 +204,7 @@ func TestFileGetStatusFiles(t *testing.T) { loader := &FileLoader{ GitCommon: buildGitCommon(commonDeps{appState: appState}), cmd: cmd, - config: &FakeFileLoaderConfig{showUntrackedFiles: "yes"}, + config: &FakeFileLoaderConfig{showUntrackedFiles: "yes", showNumberOfLineChanges: s.showNumberOfLineChanges}, getFileType: func(string) string { return "file" }, } @@ -198,9 +214,14 @@ func TestFileGetStatusFiles(t *testing.T) { } type FakeFileLoaderConfig struct { - showUntrackedFiles string + showUntrackedFiles string + showNumberOfLineChanges bool } func (self *FakeFileLoaderConfig) GetShowUntrackedFiles() string { return self.showUntrackedFiles } + +func (self *FakeFileLoaderConfig) GetShowNumberOfLineChanges() bool { + return self.showNumberOfLineChanges +} diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index bed4b227dc8..81dec319a91 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -207,7 +207,7 @@ func formatLineChanges(file *models.File) string { if output != "" { output += " " } - output += style.FgRed.Sprint("−" + strconv.Itoa(file.LinesDeleted)) + output += style.FgRed.Sprint("-" + strconv.Itoa(file.LinesDeleted)) } return output diff --git a/pkg/gui/presentation/files_test.go b/pkg/gui/presentation/files_test.go index f041991412f..adecf2dbd0b 100644 --- a/pkg/gui/presentation/files_test.go +++ b/pkg/gui/presentation/files_test.go @@ -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,16 @@ func TestRenderFileTree(t *testing.T) { }, expected: []string{" M test"}, }, + { + name: "line changes", + 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}, + }, + showLineChanges: true, + expected: []string{" M test +1 -1", " M test2 +1", " M test3 -1"}, + }, { name: "big example", files: []*models.File{ @@ -72,7 +83,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) }) } diff --git a/schema/config.json b/schema/config.json index e69fc508d65..c3728bb7198 100644 --- a/schema/config.json +++ b/schema/config.json @@ -1725,4 +1725,4 @@ }, "additionalProperties": false, "type": "object" -} +} \ No newline at end of file From 222f9a49077d4303943c6e9348ecc01faf2384f8 Mon Sep 17 00:00:00 2001 From: johannaschwarz Date: Tue, 12 Nov 2024 13:23:39 +0100 Subject: [PATCH 4/6] Add earlier short circuit to formatLineChanges --- pkg/gui/presentation/files.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index 81dec319a91..16d46c24ff1 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -168,8 +168,10 @@ func getFileLine( output += theme.DefaultTextColor.Sprint(" (submodule)") } - if lineChanges := formatLineChanges(file); showNumberOfLineChanges && lineChanges != "" { - output += " " + lineChanges + if showNumberOfLineChanges { + if lineChanges := formatLineChanges(file); lineChanges != "" { + output += " " + lineChanges + } } return output From 913d0263956b3cf5ad77db6ed5de13f322a8d5c1 Mon Sep 17 00:00:00 2001 From: johannaschwarz Date: Tue, 12 Nov 2024 13:52:32 +0100 Subject: [PATCH 5/6] Rename feature in config and default to false --- docs/Config.md | 2 +- pkg/commands/git_commands/config.go | 4 -- pkg/commands/git_commands/file_loader.go | 3 +- pkg/commands/git_commands/file_loader_test.go | 29 ++++++----- pkg/config/user_config.go | 52 +++++++++---------- pkg/gui/context/working_tree_context.go | 2 +- schema/config.json | 4 +- 7 files changed, 46 insertions(+), 50 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 3dada749913..bafbbfc99b0 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -165,7 +165,7 @@ gui: showFileTree: true # If true, show the number of lines changed per file in the Files view - showNumberOfLineChanges: true + showNumberOfLineChangesFilesView: false # If true, show a random tip in the command log when Lazygit starts showRandomTip: true diff --git a/pkg/commands/git_commands/config.go b/pkg/commands/git_commands/config.go index da709254d89..9771f01d37a 100644 --- a/pkg/commands/git_commands/config.go +++ b/pkg/commands/git_commands/config.go @@ -111,7 +111,3 @@ func (self *ConfigCommands) GetCoreCommentChar() byte { func (self *ConfigCommands) GetRebaseUpdateRefs() bool { return self.gitConfig.GetBool("rebase.updateRefs") } - -func (cfgCommands *ConfigCommands) GetShowNumberOfLineChanges() bool { - return cfgCommands.UserConfig().Gui.ShowNumberOfLineChanges -} diff --git a/pkg/commands/git_commands/file_loader.go b/pkg/commands/git_commands/file_loader.go index e7c6aa454a4..38f55ca1dcc 100644 --- a/pkg/commands/git_commands/file_loader.go +++ b/pkg/commands/git_commands/file_loader.go @@ -12,7 +12,6 @@ import ( type FileLoaderConfig interface { GetShowUntrackedFiles() string - GetShowNumberOfLineChanges() bool } type FileLoader struct { @@ -52,7 +51,7 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File files := []*models.File{} fileDiffs := map[string]FileDiff{} - if self.config.GetShowNumberOfLineChanges() { + if self.GitCommon.Common.UserConfig().Gui.ShowNumberOfLineChangesFilesView { fileDiffs, err = self.getFileDiffs() if err != nil { self.Log.Error(err) diff --git a/pkg/commands/git_commands/file_loader_test.go b/pkg/commands/git_commands/file_loader_test.go index fe091bb8a4b..4d45633c9d9 100644 --- a/pkg/commands/git_commands/file_loader_test.go +++ b/pkg/commands/git_commands/file_loader_test.go @@ -11,11 +11,11 @@ import ( func TestFileGetStatusFiles(t *testing.T) { type scenario struct { - testName string - similarityThreshold int - runner oscommands.ICmdObjRunner - showNumberOfLineChanges bool - expectedFiles []*models.File + testName string + similarityThreshold int + runner oscommands.ICmdObjRunner + showNumberOfLineChangesFilesView bool + expectedFiles []*models.File } scenarios := []scenario{ @@ -38,7 +38,7 @@ func TestFileGetStatusFiles(t *testing.T) { "4\t1\tfile1.txt\x001\t0\tfile2.txt\x002\t2\tfile3.txt\x000\t2\tfile4.txt\x002\t2\tfile5.txt", nil, ), - showNumberOfLineChanges: true, + showNumberOfLineChangesFilesView: true, expectedFiles: []*models.File{ { Name: "file1.txt", @@ -201,10 +201,16 @@ func TestFileGetStatusFiles(t *testing.T) { appState := &config.AppState{} appState.RenameSimilarityThreshold = s.similarityThreshold + userConfig := &config.UserConfig{ + Gui: config.GuiConfig{ + ShowNumberOfLineChangesFilesView: s.showNumberOfLineChangesFilesView, + }, + } + loader := &FileLoader{ - GitCommon: buildGitCommon(commonDeps{appState: appState}), + GitCommon: buildGitCommon(commonDeps{appState: appState, userConfig: userConfig}), cmd: cmd, - config: &FakeFileLoaderConfig{showUntrackedFiles: "yes", showNumberOfLineChanges: s.showNumberOfLineChanges}, + config: &FakeFileLoaderConfig{showUntrackedFiles: "yes"}, getFileType: func(string) string { return "file" }, } @@ -214,14 +220,9 @@ func TestFileGetStatusFiles(t *testing.T) { } type FakeFileLoaderConfig struct { - showUntrackedFiles string - showNumberOfLineChanges bool + showUntrackedFiles string } func (self *FakeFileLoaderConfig) GetShowUntrackedFiles() string { return self.showUntrackedFiles } - -func (self *FakeFileLoaderConfig) GetShowNumberOfLineChanges() bool { - return self.showNumberOfLineChanges -} diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 46b4c45effe..9d92e2a3986 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -110,7 +110,7 @@ type GuiConfig struct { // This can be toggled from within Lazygit with the '~' key, but that will not change the default. ShowFileTree bool `yaml:"showFileTree"` // If true, show the number of lines changed per file in the Files view - ShowNumberOfLineChanges bool `yaml:"showNumberOfLineChanges"` + ShowNumberOfLineChangesFilesView bool `yaml:"showNumberOfLineChangesFilesView"` // If true, show a random tip in the command log when Lazygit starts ShowRandomTip bool `yaml:"showRandomTip"` // If true, show the command log @@ -707,31 +707,31 @@ func GetDefaultConfig() *UserConfig { UnstagedChangesColor: []string{"red"}, DefaultFgColor: []string{"default"}, }, - CommitLength: CommitLengthConfig{Show: true}, - SkipNoStagedFilesWarning: false, - ShowListFooter: true, - ShowCommandLog: true, - ShowBottomLine: true, - ShowPanelJumps: true, - ShowFileTree: true, - ShowNumberOfLineChanges: true, - ShowRandomTip: true, - ShowIcons: false, - NerdFontsVersion: "", - ShowFileIcons: true, - CommitAuthorShortLength: 2, - CommitAuthorLongLength: 17, - CommitHashLength: 8, - ShowBranchCommitHash: false, - ShowDivergenceFromBaseBranch: "none", - CommandLogSize: 8, - SplitDiff: "auto", - SkipRewordInEditorWarning: false, - WindowSize: "normal", - Border: "rounded", - AnimateExplosion: true, - PortraitMode: "auto", - FilterMode: "substring", + CommitLength: CommitLengthConfig{Show: true}, + SkipNoStagedFilesWarning: false, + ShowListFooter: true, + ShowCommandLog: true, + ShowBottomLine: true, + ShowPanelJumps: true, + ShowFileTree: true, + ShowNumberOfLineChangesFilesView: false, + ShowRandomTip: true, + ShowIcons: false, + NerdFontsVersion: "", + ShowFileIcons: true, + CommitAuthorShortLength: 2, + CommitAuthorLongLength: 17, + CommitHashLength: 8, + ShowBranchCommitHash: false, + ShowDivergenceFromBaseBranch: "none", + CommandLogSize: 8, + SplitDiff: "auto", + SkipRewordInEditorWarning: false, + WindowSize: "normal", + Border: "rounded", + AnimateExplosion: true, + PortraitMode: "auto", + FilterMode: "substring", Spinner: SpinnerConfig{ Frames: []string{"|", "/", "-", "\\"}, Rate: 50, diff --git a/pkg/gui/context/working_tree_context.go b/pkg/gui/context/working_tree_context.go index 381fb32193f..a0d7f5aaddb 100644 --- a/pkg/gui/context/working_tree_context.go +++ b/pkg/gui/context/working_tree_context.go @@ -30,7 +30,7 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext { getDisplayStrings := func(_ int, _ int) [][]string { showFileIcons := icons.IsIconEnabled() && c.UserConfig().Gui.ShowFileIcons - showNumberOfLineChanges := c.UserConfig().Gui.ShowNumberOfLineChanges + showNumberOfLineChanges := c.UserConfig().Gui.ShowNumberOfLineChangesFilesView lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons, showNumberOfLineChanges) return lo.Map(lines, func(line string, _ int) []string { return []string{line} diff --git a/schema/config.json b/schema/config.json index c3728bb7198..af26492183f 100644 --- a/schema/config.json +++ b/schema/config.json @@ -293,10 +293,10 @@ "description": "If true, display the files in the file views as a tree. If false, display the files as a flat list.\nThis can be toggled from within Lazygit with the '~' key, but that will not change the default.", "default": true }, - "showNumberOfLineChanges": { + "showNumberOfLineChangesFilesView": { "type": "boolean", "description": "If true, show the number of lines changed per file in the Files view", - "default": true + "default": false }, "showRandomTip": { "type": "boolean", From e80983b0659ac1f2226e0d85e4b8bcb07a98b575 Mon Sep 17 00:00:00 2001 From: johannaschwarz Date: Thu, 14 Nov 2024 21:19:53 +0100 Subject: [PATCH 6/6] Refactor formatLineChanges with int arguments for earlier nil check --- pkg/gui/presentation/files.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index 16d46c24ff1..ca3ec749adb 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -168,8 +168,8 @@ func getFileLine( output += theme.DefaultTextColor.Sprint(" (submodule)") } - if showNumberOfLineChanges { - if lineChanges := formatLineChanges(file); lineChanges != "" { + if file != nil && showNumberOfLineChanges { + if lineChanges := formatLineChanges(file.LinesAdded, file.LinesDeleted); lineChanges != "" { output += " " + lineChanges } } @@ -195,21 +195,18 @@ func formatFileStatus(file *models.File, restColor style.TextStyle) string { return firstCharCl.Sprint(firstChar) + secondCharCl.Sprint(secondChar) } -func formatLineChanges(file *models.File) string { - if file == nil { - return "" - } +func formatLineChanges(linesAdded, linesDeleted int) string { output := "" - if file.LinesAdded != 0 { - output += style.FgGreen.Sprint("+" + strconv.Itoa(file.LinesAdded)) + if linesAdded != 0 { + output += style.FgGreen.Sprint("+" + strconv.Itoa(linesAdded)) } - if file.LinesDeleted != 0 { + if linesDeleted != 0 { if output != "" { output += " " } - output += style.FgRed.Sprint("-" + strconv.Itoa(file.LinesDeleted)) + output += style.FgRed.Sprint("-" + strconv.Itoa(linesDeleted)) } return output