1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-03 15:02:52 +02:00
lazygit/pkg/gui/status_tree.go

67 lines
1.3 KiB
Go
Raw Normal View History

package gui
import (
"os"
2021-03-14 14:41:11 +11:00
"path/filepath"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
2021-03-21 08:41:06 +11:00
"github.com/sirupsen/logrus"
)
2021-03-21 08:41:06 +11:00
func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.StatusLineNode {
root := &models.StatusLineNode{}
var curr *models.StatusLineNode
for _, file := range files {
split := strings.Split(file.Name, string(os.PathSeparator))
curr = root
outer:
2021-03-21 08:41:06 +11:00
for i := range split {
var setFile *models.File
2021-03-21 08:41:06 +11:00
isFile := i == len(split)-1
if isFile {
setFile = file
}
2021-03-21 08:41:06 +11:00
path := filepath.Join(split[:i+1]...)
for _, existingChild := range curr.Children {
2021-03-21 08:41:06 +11:00
if existingChild.Path == path {
curr = existingChild
continue outer
}
}
2021-03-21 08:41:06 +11:00
newChild := &models.StatusLineNode{
2021-03-21 08:41:06 +11:00
Name: path, // TODO: Remove concept of name
Path: path,
File: setFile,
}
curr.Children = append(curr.Children, newChild)
curr = newChild
}
}
root.Sort()
root.Compress()
return root
}
2021-03-21 08:41:06 +11:00
func GetFlatTreeFromStatusFiles(files []*models.File) *models.StatusLineNode {
root := &models.StatusLineNode{}
for _, file := range files {
root.Children = append(root.Children, &models.StatusLineNode{
2021-03-21 09:06:15 +11:00
Name: file.GetPath(),
2021-03-21 08:41:06 +11:00
Path: file.GetPath(),
File: file,
})
}
2021-03-21 09:06:15 +11:00
root.Sort()
2021-03-21 08:41:06 +11:00
return root
}