From 61cc7852f9ec3374b2a36e5fd5ecd5fc803a74b3 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 1 Mar 2025 14:53:50 +0100 Subject: [PATCH] WIP Try to fix some problems, e.g. switch between tree and flat view --- .../controllers/commits_files_controller.go | 2 +- pkg/gui/controllers/files_controller.go | 2 +- pkg/gui/filetree/node.go | 25 +++++++++++++------ pkg/gui/presentation/files.go | 8 +++--- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index ac1237ac3..2512f6ec2 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -477,7 +477,7 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode } func (self *CommitFilesController) handleToggleCommitFileDirCollapsed(node *filetree.CommitFileNode) error { - self.context().CommitFileTreeViewModel.ToggleCollapsed(node.GetPath()) + self.context().CommitFileTreeViewModel.ToggleCollapsed(node.GetInternalPath()) self.c.PostRefreshUpdate(self.context()) diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 1a569529f..e8c0b2f55 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -1072,7 +1072,7 @@ func (self *FilesController) handleToggleDirCollapsed() error { return nil } - self.context().FileTreeViewModel.ToggleCollapsed(node.GetPath()) + self.context().FileTreeViewModel.ToggleCollapsed(node.GetInternalPath()) self.c.PostRefreshUpdate(self.c.Contexts().Files) diff --git a/pkg/gui/filetree/node.go b/pkg/gui/filetree/node.go index 18fdd190d..ef7bbd91a 100644 --- a/pkg/gui/filetree/node.go +++ b/pkg/gui/filetree/node.go @@ -20,7 +20,7 @@ type Node[T any] struct { Children []*Node[T] // path of the file/directory - // private; use GetPath() to access + // private; use either GetPath() or GetInternalPath() to access path string // rather than render a tree as: @@ -47,11 +47,20 @@ func (self *Node[T]) GetFile() *T { return self.File } +// This returns the logical path from the user's point of view. It is the +// relative path from the root of the repository. +// Use this for display, or when you want to perform some action on the path +// (e.g. a git command). func (self *Node[T]) GetPath() string { return strings.TrimPrefix(self.path, "./") } -func (self *Node[T]) GetFullPath() string { +// This returns the internal path from the tree's point of view. It's the same +// as GetPath() except that it has a "./" prefix when we are showing a root +// item. +// Use this when interacting with the tree itself, e.g. when calling +// ToggleCollapsed. +func (self *Node[T]) GetInternalPath() string { return self.path } @@ -94,7 +103,7 @@ func (self *Node[T]) SortChildren() { return false } - return a.GetPath() < b.GetPath() + return a.path < b.path }) // TODO: think about making this in-place @@ -164,7 +173,7 @@ func (self *Node[T]) EveryFile(test func(*T) bool) bool { func (self *Node[T]) Flatten(collapsedPaths *CollapsedPaths) []*Node[T] { result := []*Node[T]{self} - if len(self.Children) > 0 && !collapsedPaths.IsCollapsed(self.GetPath()) { + if len(self.Children) > 0 && !collapsedPaths.IsCollapsed(self.path) { result = append(result, lo.FlatMap(self.Children, func(child *Node[T], _ int) []*Node[T] { return child.Flatten(collapsedPaths) })...) @@ -190,7 +199,7 @@ func (self *Node[T]) getNodeAtIndexAux(index int, collapsedPaths *CollapsedPaths return self, offset } - if !collapsedPaths.IsCollapsed(self.GetPath()) { + if !collapsedPaths.IsCollapsed(self.path) { for _, child := range self.Children { foundNode, offsetChange := child.getNodeAtIndexAux(index-offset, collapsedPaths) offset += offsetChange @@ -206,11 +215,11 @@ func (self *Node[T]) getNodeAtIndexAux(index int, collapsedPaths *CollapsedPaths func (self *Node[T]) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) { offset := 0 - if self.GetPath() == path { + if self.path == path { return offset, true } - if !collapsedPaths.IsCollapsed(self.GetPath()) { + if !collapsedPaths.IsCollapsed(self.path) { for _, child := range self.Children { offsetChange, found := child.GetIndexForPath(path, collapsedPaths) offset += offsetChange + 1 @@ -230,7 +239,7 @@ func (self *Node[T]) Size(collapsedPaths *CollapsedPaths) int { output := 1 - if !collapsedPaths.IsCollapsed(self.GetPath()) { + if !collapsedPaths.IsCollapsed(self.path) { for _, child := range self.Children { output += child.Size(collapsedPaths) } diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index a6387f7fa..a38484dc1 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -91,11 +91,11 @@ func renderAux[T any]( arr := []string{} if !isRoot { - isCollapsed := collapsedPaths.IsCollapsed(node.GetPath()) + isCollapsed := collapsedPaths.IsCollapsed(node.GetInternalPath()) arr = append(arr, renderLine(node, treeDepth, visualDepth, isCollapsed)) } - if collapsedPaths.IsCollapsed(node.GetPath()) { + if collapsedPaths.IsCollapsed(node.GetInternalPath()) { return arr } @@ -293,7 +293,7 @@ func getColorForChangeStatus(changeStatus string) style.TextStyle { } func fileNameAtDepth(node *filetree.Node[models.File], depth int) string { - splitName := split(node.GetFullPath()) + splitName := split(node.GetInternalPath()) if depth == 0 && splitName[0] == "." { if len(splitName) == 1 { return "" @@ -320,7 +320,7 @@ func fileNameAtDepth(node *filetree.Node[models.File], depth int) string { } func commitFileNameAtDepth(node *filetree.Node[models.CommitFile], depth int) string { - splitName := split(node.GetFullPath()) + splitName := split(node.GetInternalPath()) if depth == 0 && splitName[0] == "." { if len(splitName) == 1 { return ""