Skip to content

Commit 21b8b28

Browse files
authored
Add user config for hiding the root item in the file tree (#4593)
- **PR Description** In #4346 we added a `/` root item in the Files and CommitFiles panels whenever there is more than one top-level item. We made it unconditional, but I promised to add a config as soon as users ask for being able to disable it. For a while I was able to convince users who asked for it that it is useful and they don't want to turn it off, but now there's a [stronger request](#4590 (reply in thread)) from someone who refuses to upgrade to the current version, and we don't want that. So, add a config option `gui.showRootItemInFileTree` that is true by default.
2 parents da32b59 + 3cff484 commit 21b8b28

15 files changed

+409
-54
lines changed

docs/Config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ gui:
190190
# This can be toggled from within Lazygit with the '`' key, but that will not change the default.
191191
showFileTree: true
192192

193+
# If true, add a "/" root item in the file tree representing the root of the repository. It is only added when necessary, i.e. when there is more than one item at top level.
194+
showRootItemInFileTree: true
195+
193196
# If true, show the number of lines changed per file in the Files view
194197
showNumstatInFilesView: false
195198

pkg/config/user_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ type GuiConfig struct {
123123
// If true, display the files in the file views as a tree. If false, display the files as a flat list.
124124
// This can be toggled from within Lazygit with the '`' key, but that will not change the default.
125125
ShowFileTree bool `yaml:"showFileTree"`
126+
// If true, add a "/" root item in the file tree representing the root of the repository. It is only added when necessary, i.e. when there is more than one item at top level.
127+
ShowRootItemInFileTree bool `yaml:"showRootItemInFileTree"`
126128
// If true, show the number of lines changed per file in the Files view
127129
ShowNumstatInFilesView bool `yaml:"showNumstatInFilesView"`
128130
// If true, show a random tip in the command log when Lazygit starts
@@ -764,6 +766,7 @@ func GetDefaultConfig() *UserConfig {
764766
ShowBottomLine: true,
765767
ShowPanelJumps: true,
766768
ShowFileTree: true,
769+
ShowRootItemInFileTree: true,
767770
ShowNumstatInFilesView: false,
768771
ShowRandomTip: true,
769772
ShowIcons: false,

pkg/gui/context/commit_files_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
3030
viewModel := filetree.NewCommitFileTreeViewModel(
3131
func() []*models.CommitFile { return c.Model().CommitFiles },
32-
c.Log,
32+
c.Common,
3333
c.UserConfig().Gui.ShowFileTree,
3434
)
3535

pkg/gui/context/working_tree_context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ var (
2424
func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
2525
viewModel := filetree.NewFileTreeViewModel(
2626
func() []*models.File { return c.Model().Files },
27-
c.Log,
27+
c.Common,
2828
c.UserConfig().Gui.ShowFileTree,
2929
)
3030

3131
getDisplayStrings := func(_ int, _ int) [][]string {
3232
showFileIcons := icons.IsIconEnabled() && c.UserConfig().Gui.ShowFileIcons
3333
showNumstat := c.UserConfig().Gui.ShowNumstatInFilesView
34-
lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons, showNumstat, &c.UserConfig().Gui.CustomIcons)
34+
lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons, showNumstat, &c.UserConfig().Gui.CustomIcons, c.UserConfig().Gui.ShowRootItemInFileTree)
3535
return lo.Map(lines, func(line string, _ int) []string {
3636
return []string{line}
3737
})

pkg/gui/filetree/build_tree.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"github.com/jesseduffield/lazygit/pkg/commands/models"
88
)
99

10-
func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
10+
func BuildTreeFromFiles(files []*models.File, showRootItem bool) *Node[models.File] {
1111
root := &Node[models.File]{}
1212

1313
childrenMapsByNode := make(map[*Node[models.File]]map[string]*Node[models.File])
1414

1515
var curr *Node[models.File]
1616
for _, file := range files {
17-
splitPath := split("./" + file.Path)
17+
splitPath := SplitFileTreePath(file.Path, showRootItem)
1818
curr = root
1919
outer:
2020
for i := range splitPath {
@@ -63,19 +63,19 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
6363
return root
6464
}
6565

66-
func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFile] {
67-
rootAux := BuildTreeFromCommitFiles(files)
66+
func BuildFlatTreeFromCommitFiles(files []*models.CommitFile, showRootItem bool) *Node[models.CommitFile] {
67+
rootAux := BuildTreeFromCommitFiles(files, showRootItem)
6868
sortedFiles := rootAux.GetLeaves()
6969

7070
return &Node[models.CommitFile]{Children: sortedFiles}
7171
}
7272

73-
func BuildTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFile] {
73+
func BuildTreeFromCommitFiles(files []*models.CommitFile, showRootItem bool) *Node[models.CommitFile] {
7474
root := &Node[models.CommitFile]{}
7575

7676
var curr *Node[models.CommitFile]
7777
for _, file := range files {
78-
splitPath := split("./" + file.Path)
78+
splitPath := SplitFileTreePath(file.Path, showRootItem)
7979
curr = root
8080
outer:
8181
for i := range splitPath {
@@ -115,8 +115,8 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFil
115115
return root
116116
}
117117

118-
func BuildFlatTreeFromFiles(files []*models.File) *Node[models.File] {
119-
rootAux := BuildTreeFromFiles(files)
118+
func BuildFlatTreeFromFiles(files []*models.File, showRootItem bool) *Node[models.File] {
119+
rootAux := BuildTreeFromFiles(files, showRootItem)
120120
sortedFiles := rootAux.GetLeaves()
121121

122122
// from top down we have merge conflict files, then tracked file, then untracked
@@ -160,3 +160,11 @@ func split(str string) []string {
160160
func join(strs []string) string {
161161
return strings.Join(strs, "/")
162162
}
163+
164+
func SplitFileTreePath(path string, showRootItem bool) []string {
165+
if showRootItem {
166+
return split("./" + path)
167+
}
168+
169+
return split(path)
170+
}

0 commit comments

Comments
 (0)