summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-03-21 10:46:43 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-03-30 21:57:00 +1100
commit2b8302bceddc7391ded956418029349a85824c7b (patch)
tree15abbbf3e50d1037181d3633ed79b7a4855e05a4
parent1b94462410daa7424f1d050549d4da278da4a02b (diff)
refactor
-rw-r--r--pkg/commands/models/file_change_interface.go (renamed from pkg/commands/models/status_line.go)0
-rw-r--r--pkg/commands/models/file_change_node.go (renamed from pkg/commands/models/status_line_node.go)24
-rw-r--r--pkg/commands/models/file_change_node_test.go (renamed from pkg/commands/models/status_line_node_test.go)31
-rw-r--r--pkg/gui/file_change_manager.go (renamed from pkg/gui/status_line_manager.go)6
-rw-r--r--pkg/gui/file_change_manager_test.go (renamed from pkg/gui/status_line_manager_test.go)0
-rw-r--r--pkg/gui/list_context.go3
-rw-r--r--pkg/gui/presentation/files.go19
-rw-r--r--pkg/gui/status_tree.go20
8 files changed, 38 insertions, 65 deletions
diff --git a/pkg/commands/models/status_line.go b/pkg/commands/models/file_change_interface.go
index 5413b076f..5413b076f 100644
--- a/pkg/commands/models/status_line.go
+++ b/pkg/commands/models/file_change_interface.go
diff --git a/pkg/commands/models/status_line_node.go b/pkg/commands/models/file_change_node.go
index 9e0f241f3..d265b3f05 100644
--- a/pkg/commands/models/status_line_node.go
+++ b/pkg/commands/models/file_change_node.go
@@ -9,10 +9,11 @@ import (
)
type FileChangeNode struct {
- Children []*FileChangeNode
- File *File
- Path string // e.g. '/path/to/mydir'
- Collapsed bool
+ Children []*FileChangeNode
+ File *File
+ Path string // e.g. '/path/to/mydir'
+ Collapsed bool
+ CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered
}
func (s *FileChangeNode) GetHasUnstagedChanges() bool {
@@ -186,8 +187,10 @@ func (s *FileChangeNode) compressAux() *FileChangeNode {
for i := range s.Children {
for s.Children[i].HasExactlyOneChild() {
+ prevCompressionLevel := s.Children[i].CompressionLevel
grandchild := s.Children[i].Children[0]
s.Children[i] = grandchild
+ s.Children[i].CompressionLevel = prevCompressionLevel + 1
}
}
@@ -241,6 +244,19 @@ func (s *FileChangeNode) ForEachFile(cb func(*File) error) error {
return nil
}
+func (s *FileChangeNode) GetLeaves() []*FileChangeNode {
+ if s.IsLeaf() {
+ return []*FileChangeNode{s}
+ }
+
+ output := []*FileChangeNode{}
+ for _, child := range s.Children {
+ output = append(output, child.GetLeaves()...)
+ }
+
+ return output
+}
+
func (s *FileChangeNode) NameAtDepth(depth int) string {
splitName := strings.Split(s.Path, string(os.PathSeparator))
name := filepath.Join(splitName[depth:]...)
diff --git a/pkg/commands/models/status_line_node_test.go b/pkg/commands/models/file_change_node_test.go
index 0f0f95ce5..8190230e3 100644
--- a/pkg/commands/models/status_line_node_test.go
+++ b/pkg/commands/models/file_change_node_test.go
@@ -20,61 +20,53 @@ func TestCompress(t *testing.T) {
{
name: "leaf node",
root: &FileChangeNode{
- Name: "",
+ Path: "",
Children: []*FileChangeNode{
- {File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Name: "test"},
+ {File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
expected: &FileChangeNode{
- Name: "",
+ Path: "",
Children: []*FileChangeNode{
- {File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Name: "test"},
+ {File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
},
{
name: "big example",
root: &FileChangeNode{
- Name: "",
+ Path: "",
Children: []*FileChangeNode{
{
- Name: "dir1",
Path: "dir1",
Children: []*FileChangeNode{
{
File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
- Name: "file2",
Path: "dir1/file2",
},
},
},
{
- Name: "dir2",
Path: "dir2",
Children: []*FileChangeNode{
{
File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
- Name: "file3",
Path: "dir2/file3",
},
{
File: &File{Name: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
- Name: "file4",
Path: "dir2/file4",
},
},
},
{
- Name: "dir3",
Path: "dir3",
Children: []*FileChangeNode{
{
- Name: "dir3-1",
Path: "dir3/dir3-1",
Children: []*FileChangeNode{
{
File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
- Name: "file5",
Path: "dir3/dir3-1/file5",
},
},
@@ -83,43 +75,36 @@ func TestCompress(t *testing.T) {
},
{
File: &File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
- Name: "file1",
Path: "file1",
},
},
},
expected: &FileChangeNode{
- Name: "",
+ Path: "",
Children: []*FileChangeNode{
{
- Name: "dir1/file2",
- File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
+ File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
},
{
- Name: "dir2",
Path: "dir2",
Children: []*FileChangeNode{
{
File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
- Name: "file3",
Path: "dir2/file3",
},
{
File: &File{Name: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
- Name: "file4",
Path: "dir2/file4",
},
},
},
{
- Name: "dir3/dir3-1/file5",
- File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir3/dir3-1/file5",
+ File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
},
{
File: &File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
- Name: "file1",
Path: "file1",
},
},
diff --git a/pkg/gui/status_line_manager.go b/pkg/gui/file_change_manager.go
index 41c0d07d1..40f547666 100644
--- a/pkg/gui/status_line_manager.go
+++ b/pkg/gui/file_change_manager.go
@@ -63,7 +63,7 @@ func (m *FileChangeManager) SetFiles(files []*models.File) {
func (m *FileChangeManager) SetTree() {
if m.ShowTree {
- m.Tree = GetTreeFromStatusFiles(m.Files, m.Log)
+ m.Tree = GetTreeFromStatusFiles(m.Files)
} else {
m.Tree = GetFlatTreeFromStatusFiles(m.Files)
}
@@ -93,7 +93,7 @@ func (m *FileChangeManager) renderAux(s *models.FileChangeNode, prefix string, d
}
getLine := func() string {
- return prefix + presentation.GetStatusNodeLine(s.GetHasUnstagedChanges(), s.GetHasStagedChanges(), s.NameAtDepth(depth), diffName, submoduleConfigs, s.File)
+ return prefix + presentation.GetFileLine(s.GetHasUnstagedChanges(), s.GetHasStagedChanges(), s.NameAtDepth(depth), diffName, submoduleConfigs, s.File)
}
if s.IsLeaf() {
@@ -131,7 +131,7 @@ func (m *FileChangeManager) renderAux(s *models.FileChangeNode, prefix string, d
childPrefix = newPrefix + INNER_ITEM
}
- arr = append(arr, m.renderAux(child, childPrefix, depth+1, diffName, submoduleConfigs)...)
+ arr = append(arr, m.renderAux(child, childPrefix, depth+1+s.CompressionLevel, diffName, submoduleConfigs)...)
}
return arr
diff --git a/pkg/gui/status_line_manager_test.go b/pkg/gui/file_change_manager_test.go
index 3d078f61d..3d078f61d 100644
--- a/pkg/gui/status_line_manager_test.go
+++ b/pkg/gui/file_change_manager_test.go
diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go
index c0df02014..65fac2137 100644
--- a/pkg/gui/list_context.go
+++ b/pkg/gui/list_context.go
@@ -277,9 +277,6 @@ func (gui *Gui) filesListContext() *ListContext {
}
return mappedLines
-
- // TODO: Fix this up
- return presentation.GetFileListDisplayStrings(gui.State.FileChangeManager.GetAllFiles(), gui.State.Modes.Diffing.Ref, gui.State.Submodules)
},
SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedStatusNode()
diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go
index 900a5cabe..5cf312c55 100644
--- a/pkg/gui/presentation/files.go
+++ b/pkg/gui/presentation/files.go
@@ -7,24 +7,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils"
)
-func GetFileListDisplayStrings(files []*models.File, diffName string, submoduleConfigs []*models.SubmoduleConfig) [][]string {
- lines := make([][]string, len(files))
-
- for i := range files {
- lines[i] = getFileDisplayStrings(files[i], diffName, submoduleConfigs)
- }
-
- return lines
-}
-
-// getFileDisplayStrings returns the display string of branch
-func getFileDisplayStrings(f *models.File, diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
- output := GetStatusNodeLine(f.HasUnstagedChanges, f.HasStagedChanges, f.Name, diffName, submoduleConfigs, f)
-
- return []string{output}
-}
-
-func GetStatusNodeLine(hasUnstagedChanges bool, hasStagedChanges bool, name string, diffName string, submoduleConfigs []*models.SubmoduleConfig, file *models.File) string {
+func GetFileLine(hasUnstagedChanges bool, hasStagedChanges bool, name string, diffName string, submoduleConfigs []*models.SubmoduleConfig, file *models.File) string {
// potentially inefficient to be instantiating these color
// objects with each render
red := color.New(color.FgRed)
diff --git a/pkg/gui/status_tree.go b/pkg/gui/status_tree.go
index f95dff160..abf301e03 100644
--- a/pkg/gui/status_tree.go
+++ b/pkg/gui/status_tree.go
@@ -7,10 +7,9 @@ import (
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
- "github.com/sirupsen/logrus"
)
-func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.FileChangeNode {
+func GetTreeFromStatusFiles(files []*models.File) *models.FileChangeNode {
root := &models.FileChangeNode{}
var curr *models.FileChangeNode
@@ -51,21 +50,14 @@ func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.Fil
}
func GetFlatTreeFromStatusFiles(files []*models.File) *models.FileChangeNode {
- root := &models.FileChangeNode{}
- for _, file := range files {
- root.Children = append(root.Children, &models.FileChangeNode{
- Path: file.GetPath(),
- File: file,
- })
- }
-
- root.Sort()
+ rootAux := GetTreeFromStatusFiles(files)
+ sortedFiles := rootAux.GetLeaves()
// Move merge conflicts to top. This is the one way in which sorting
// differs between flat mode and tree mode
- sort.SliceStable(root.Children, func(i, j int) bool {
- return root.Children[i].File != nil && root.Children[i].File.HasMergeConflicts && !(root.Children[j].File != nil && root.Children[j].File.HasMergeConflicts)
+ sort.SliceStable(sortedFiles, func(i, j int) bool {
+ return sortedFiles[i].File != nil && sortedFiles[i].File.HasMergeConflicts && !(sortedFiles[j].File != nil && sortedFiles[j].File.HasMergeConflicts)
})
- return root
+ return &models.FileChangeNode{Children: sortedFiles}
}