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

144 lines
3.5 KiB
Go
Raw Normal View History

2021-03-21 06:25:29 +02:00
package filetree
import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/sirupsen/logrus"
)
const EXPANDED_ARROW = "▼"
const COLLAPSED_ARROW = "►"
2021-03-21 06:25:54 +02:00
const INNER_ITEM = "├─ "
const LAST_ITEM = "└─ "
const NESTED = "│ "
const NOTHING = " "
2021-03-21 01:23:14 +02:00
type FileChangeManager struct {
2021-03-21 06:28:18 +02:00
files []*models.File
2021-03-21 06:58:15 +02:00
tree *FileChangeNode
2021-03-21 06:28:18 +02:00
showTree bool
log *logrus.Entry
collapsedPaths map[string]bool
2021-03-14 11:18:06 +02:00
}
2021-03-21 01:23:14 +02:00
func NewFileChangeManager(files []*models.File, log *logrus.Entry, showTree bool) *FileChangeManager {
return &FileChangeManager{
2021-03-21 06:28:18 +02:00
files: files,
log: log,
showTree: showTree,
collapsedPaths: map[string]bool{},
2021-03-14 11:18:06 +02:00
}
}
2021-03-21 06:28:18 +02:00
func (m *FileChangeManager) ToggleShowTree() {
m.showTree = !m.showTree
m.SetTree()
}
2021-03-21 06:58:15 +02:00
func (m *FileChangeManager) GetItemAtIndex(index int) *FileChangeNode {
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
}
2021-03-21 01:23:14 +02:00
func (m *FileChangeManager) 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
}
2021-03-21 06:58:15 +02:00
func (m *FileChangeManager) GetAllItems() []*FileChangeNode {
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
}
2021-03-21 01:23:14 +02:00
func (m *FileChangeManager) GetItemsLength() int {
2021-03-21 06:28:18 +02:00
return m.tree.Size(m.collapsedPaths) - 1 // ignoring root
}
2021-03-21 01:23:14 +02:00
func (m *FileChangeManager) GetAllFiles() []*models.File {
2021-03-21 06:28:18 +02:00
return m.files
}
2021-03-21 01:23:14 +02:00
func (m *FileChangeManager) 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()
}
2021-03-21 01:23:14 +02:00
func (m *FileChangeManager) SetTree() {
2021-03-21 06:28:18 +02:00
if m.showTree {
m.tree = BuildTreeFromFiles(m.files)
2021-03-20 23:41:06 +02:00
} else {
2021-03-21 06:28:18 +02:00
m.tree = BuildFlatTreeFromFiles(m.files)
2021-03-20 23:41:06 +02:00
}
}
2021-03-21 01:23:14 +02:00
func (m *FileChangeManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
2021-03-21 06:28:18 +02:00
return m.renderAux(m.tree, "", -1, diffName, submoduleConfigs)
}
2021-03-21 06:58:15 +02:00
func (m *FileChangeManager) IsCollapsed(s *FileChangeNode) bool {
2021-03-21 06:28:18 +02:00
return m.collapsedPaths[s.GetPath()]
2021-03-14 11:18:06 +02:00
}
2021-03-21 06:58:15 +02:00
func (m *FileChangeManager) ToggleCollapsed(s *FileChangeNode) {
2021-03-21 06:28:18 +02:00
m.collapsedPaths[s.GetPath()] = !m.collapsedPaths[s.GetPath()]
2021-03-14 11:18:06 +02:00
}
2021-03-21 06:58:15 +02:00
func (m *FileChangeManager) renderAux(s *FileChangeNode, prefix string, depth int, diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
2021-03-14 05:18:23 +02:00
isRoot := depth == -1
if s == nil {
return []string{}
}
getLine := func() string {
2021-03-21 01:46:43 +02:00
return prefix + presentation.GetFileLine(s.GetHasUnstagedChanges(), s.GetHasStagedChanges(), s.NameAtDepth(depth), diffName, submoduleConfigs, s.File)
}
if s.IsLeaf() {
2021-03-14 05:18:23 +02:00
if isRoot {
return []string{}
}
return []string{getLine()}
}
2021-03-14 11:18:06 +02:00
if m.IsCollapsed(s) {
2021-03-14 05:41:11 +02:00
return []string{fmt.Sprintf("%s %s", getLine(), COLLAPSED_ARROW)}
}
arr := []string{}
2021-03-14 05:18:23 +02:00
if !isRoot {
2021-03-14 05:41:11 +02:00
arr = append(arr, fmt.Sprintf("%s %s", getLine(), EXPANDED_ARROW))
2021-03-14 05:18:23 +02:00
}
newPrefix := prefix
if strings.HasSuffix(prefix, LAST_ITEM) {
newPrefix = strings.TrimSuffix(prefix, LAST_ITEM) + NOTHING
} else if strings.HasSuffix(prefix, INNER_ITEM) {
newPrefix = strings.TrimSuffix(prefix, INNER_ITEM) + NESTED
}
2021-03-14 05:18:23 +02:00
for i, child := range s.Children {
isLast := i == len(s.Children)-1
var childPrefix string
if isRoot {
childPrefix = newPrefix
} else if isLast {
childPrefix = newPrefix + LAST_ITEM
} else {
childPrefix = newPrefix + INNER_ITEM
}
2021-03-21 01:46:43 +02:00
arr = append(arr, m.renderAux(child, childPrefix, depth+1+s.CompressionLevel, diffName, submoduleConfigs)...)
}
return arr
}