Skip to content

Add user config for hiding the root item in the file tree #4593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ gui:
# This can be toggled from within Lazygit with the '`' key, but that will not change the default.
showFileTree: true

# 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.
showRootItemInFileTree: true

# If true, show the number of lines changed per file in the Files view
showNumstatInFilesView: false

Expand Down
3 changes: 3 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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, 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.
ShowRootItemInFileTree bool `yaml:"showRootItemInFileTree"`
// If true, show the number of lines changed per file in the Files view
ShowNumstatInFilesView bool `yaml:"showNumstatInFilesView"`
// If true, show a random tip in the command log when Lazygit starts
Expand Down Expand Up @@ -764,6 +766,7 @@ func GetDefaultConfig() *UserConfig {
ShowBottomLine: true,
ShowPanelJumps: true,
ShowFileTree: true,
ShowRootItemInFileTree: true,
ShowNumstatInFilesView: false,
ShowRandomTip: true,
ShowIcons: false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/context/commit_files_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
viewModel := filetree.NewCommitFileTreeViewModel(
func() []*models.CommitFile { return c.Model().CommitFiles },
c.Log,
c.Common,
c.UserConfig().Gui.ShowFileTree,
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/context/working_tree_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ var (
func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
viewModel := filetree.NewFileTreeViewModel(
func() []*models.File { return c.Model().Files },
c.Log,
c.Common,
c.UserConfig().Gui.ShowFileTree,
)

getDisplayStrings := func(_ int, _ int) [][]string {
showFileIcons := icons.IsIconEnabled() && c.UserConfig().Gui.ShowFileIcons
showNumstat := c.UserConfig().Gui.ShowNumstatInFilesView
lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons, showNumstat, &c.UserConfig().Gui.CustomIcons)
lines := presentation.RenderFileTree(viewModel, c.Model().Submodules, showFileIcons, showNumstat, &c.UserConfig().Gui.CustomIcons, c.UserConfig().Gui.ShowRootItemInFileTree)
return lo.Map(lines, func(line string, _ int) []string {
return []string{line}
})
Expand Down
24 changes: 16 additions & 8 deletions pkg/gui/filetree/build_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
)

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

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

var curr *Node[models.File]
for _, file := range files {
splitPath := split("./" + file.Path)
splitPath := SplitFileTreePath(file.Path, showRootItem)
curr = root
outer:
for i := range splitPath {
Expand Down Expand Up @@ -63,19 +63,19 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
return root
}

func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFile] {
rootAux := BuildTreeFromCommitFiles(files)
func BuildFlatTreeFromCommitFiles(files []*models.CommitFile, showRootItem bool) *Node[models.CommitFile] {
rootAux := BuildTreeFromCommitFiles(files, showRootItem)
sortedFiles := rootAux.GetLeaves()

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

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

var curr *Node[models.CommitFile]
for _, file := range files {
splitPath := split("./" + file.Path)
splitPath := SplitFileTreePath(file.Path, showRootItem)
curr = root
outer:
for i := range splitPath {
Expand Down Expand Up @@ -115,8 +115,8 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFil
return root
}

func BuildFlatTreeFromFiles(files []*models.File) *Node[models.File] {
rootAux := BuildTreeFromFiles(files)
func BuildFlatTreeFromFiles(files []*models.File, showRootItem bool) *Node[models.File] {
rootAux := BuildTreeFromFiles(files, showRootItem)
sortedFiles := rootAux.GetLeaves()

// from top down we have merge conflict files, then tracked file, then untracked
Expand Down Expand Up @@ -160,3 +160,11 @@ func split(str string) []string {
func join(strs []string) string {
return strings.Join(strs, "/")
}

func SplitFileTreePath(path string, showRootItem bool) []string {
if showRootItem {
return split("./" + path)
}

return split(path)
}
Loading