1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/filetree/file_manager.go

142 lines
3.2 KiB
Go
Raw Normal View History

2021-03-21 06:25:29 +02:00
package filetree
import (
2021-04-03 10:35:45 +02:00
"sync"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/sirupsen/logrus"
)
type FileManagerDisplayFilter int
const (
DisplayAll FileManagerDisplayFilter = iota
DisplayStaged
DisplayUnstaged
)
type FileManager struct {
2021-03-21 06:28:18 +02:00
files []*models.File
tree *FileNode
2021-03-21 06:28:18 +02:00
showTree bool
log *logrus.Entry
filter FileManagerDisplayFilter
2021-03-31 13:08:55 +02:00
collapsedPaths CollapsedPaths
2021-04-03 10:35:45 +02:00
sync.RWMutex
2021-03-14 11:18:06 +02:00
}
2021-04-01 16:36:30 +02:00
func NewFileManager(files []*models.File, log *logrus.Entry, showTree bool) *FileManager {
return &FileManager{
2021-03-21 06:28:18 +02:00
files: files,
log: log,
showTree: showTree,
filter: DisplayAll,
2021-03-31 13:08:55 +02:00
collapsedPaths: CollapsedPaths{},
2021-04-03 10:35:45 +02:00
RWMutex: sync.RWMutex{},
2021-03-14 11:18:06 +02:00
}
}
func (m *FileManager) InTreeMode() bool {
return m.showTree
}
func (m *FileManager) ExpandToPath(path string) {
2021-03-31 13:08:55 +02:00
m.collapsedPaths.ExpandToPath(path)
}
func (m *FileManager) GetFilesForDisplay() []*models.File {
files := m.files
if m.filter == DisplayAll {
return files
}
result := make([]*models.File, 0)
if m.filter == DisplayStaged {
for _, file := range files {
if file.HasStagedChanges {
result = append(result, file)
}
}
} else {
for _, file := range files {
if !file.HasStagedChanges {
result = append(result, file)
}
}
}
return result
}
func (m *FileManager) SetDisplayFilter(filter FileManagerDisplayFilter) {
m.filter = filter
m.SetTree()
}
func (m *FileManager) ToggleShowTree() {
2021-03-21 06:28:18 +02:00
m.showTree = !m.showTree
m.SetTree()
}
func (m *FileManager) GetItemAtIndex(index int) *FileNode {
2021-03-20 23:41:06 +02:00
// need to traverse the three depth first until we get to the index.
2021-03-21 06:28:18 +02:00
return m.tree.GetNodeAtIndex(index+1, m.collapsedPaths) // ignoring root
2021-03-20 23:41:06 +02:00
}
func (m *FileManager) GetIndexForPath(path string) (int, bool) {
2021-03-21 06:28:18 +02:00
index, found := m.tree.GetIndexForPath(path, m.collapsedPaths)
2021-03-20 23:41:06 +02:00
return index - 1, found
}
func (m *FileManager) GetAllItems() []*FileNode {
2021-03-21 06:28:18 +02:00
if m.tree == nil {
2021-03-21 00:20:52 +02:00
return nil
}
2021-03-21 06:28:18 +02:00
return m.tree.Flatten(m.collapsedPaths)[1:] // ignoring root
}
func (m *FileManager) GetItemsLength() int {
2021-03-21 06:28:18 +02:00
return m.tree.Size(m.collapsedPaths) - 1 // ignoring root
}
func (m *FileManager) GetAllFiles() []*models.File {
2021-03-21 06:28:18 +02:00
return m.files
}
func (m *FileManager) SetFiles(files []*models.File) {
2021-03-21 06:28:18 +02:00
m.files = files
2021-03-20 23:41:06 +02:00
m.SetTree()
}
func (m *FileManager) SetTree() {
filesForDisplay := m.GetFilesForDisplay()
2021-03-21 06:28:18 +02:00
if m.showTree {
m.tree = BuildTreeFromFiles(filesForDisplay)
2021-03-20 23:41:06 +02:00
} else {
m.tree = BuildFlatTreeFromFiles(filesForDisplay)
2021-03-20 23:41:06 +02:00
}
}
func (m *FileManager) IsCollapsed(path string) bool {
2021-03-31 13:08:55 +02:00
return m.collapsedPaths.IsCollapsed(path)
}
func (m *FileManager) ToggleCollapsed(path string) {
2021-03-31 13:08:55 +02:00
m.collapsedPaths.ToggleCollapsed(path)
}
func (m *FileManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
2021-04-01 00:25:11 +02:00
// can't rely on renderAux to check for nil because an interface won't be nil if its concrete value is nil
if m.tree == nil {
return []string{}
}
2021-03-31 13:08:55 +02:00
return renderAux(m.tree, m.collapsedPaths, "", -1, func(n INode, depth int) string {
castN := n.(*FileNode)
2021-03-31 13:08:55 +02:00
return presentation.GetFileLine(castN.GetHasUnstagedChanges(), castN.GetHasStagedChanges(), castN.NameAtDepth(depth), diffName, submoduleConfigs, castN.File)
})
}