2020-11-15 01:45:55 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-20 23:41:06 +02:00
|
|
|
"sort"
|
2020-11-15 01:45:55 +02:00
|
|
|
"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 = "►"
|
|
|
|
|
|
|
|
type StatusLineManager struct {
|
2021-03-14 11:18:06 +02:00
|
|
|
Files []*models.File
|
|
|
|
Tree *models.StatusLineNode
|
|
|
|
TreeMode bool
|
|
|
|
Log *logrus.Entry
|
|
|
|
CollapsedPaths map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStatusLineManager(files []*models.File, log *logrus.Entry) *StatusLineManager {
|
|
|
|
return &StatusLineManager{
|
|
|
|
Files: files,
|
|
|
|
Log: log,
|
2021-03-20 23:41:06 +02:00
|
|
|
TreeMode: false, // always true for now
|
2021-03-14 11:18:06 +02:00
|
|
|
CollapsedPaths: map[string]bool{},
|
|
|
|
}
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) GetItemAtIndex(index int) *models.StatusLineNode {
|
2021-03-20 23:41:06 +02:00
|
|
|
// need to traverse the three depth first until we get to the index.
|
|
|
|
return m.Tree.GetNodeAtIndex(index+1, m.CollapsedPaths) // ignoring root
|
|
|
|
}
|
2020-11-15 01:45:55 +02:00
|
|
|
|
2021-03-20 23:41:06 +02:00
|
|
|
func (m *StatusLineManager) GetIndexForPath(path string) (int, bool) {
|
|
|
|
index, found := m.Tree.GetIndexForPath(path, m.CollapsedPaths)
|
|
|
|
return index - 1, found
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) GetAllItems() []*models.StatusLineNode {
|
2021-03-14 11:18:06 +02:00
|
|
|
return m.Tree.Flatten(m.CollapsedPaths)[1:] // ignoring root
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) GetItemsLength() int {
|
2021-03-14 11:18:06 +02:00
|
|
|
return m.Tree.Size(m.CollapsedPaths) - 1 // ignoring root
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) GetAllFiles() []*models.File {
|
|
|
|
return m.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) SetFiles(files []*models.File) {
|
|
|
|
m.Files = files
|
2021-03-20 23:41:06 +02:00
|
|
|
sort.SliceStable(m.Files, func(i, j int) bool {
|
|
|
|
return m.Files[i].Name < m.Files[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
m.SetTree()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) SetTree() {
|
|
|
|
if m.TreeMode {
|
|
|
|
m.Tree = GetTreeFromStatusFiles(m.Files, m.Log)
|
|
|
|
} else {
|
|
|
|
m.Tree = GetFlatTreeFromStatusFiles(m.Files)
|
|
|
|
}
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
|
2021-03-14 05:18:23 +02:00
|
|
|
return m.renderAux(m.Tree, "", -1, diffName, submoduleConfigs)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-14 05:18:23 +02:00
|
|
|
const INNER_ITEM = "├─ "
|
|
|
|
const LAST_ITEM = "└─ "
|
2021-03-14 10:44:52 +02:00
|
|
|
const NESTED = "│ "
|
|
|
|
const NOTHING = " "
|
2021-03-14 05:18:23 +02:00
|
|
|
|
2021-03-14 11:18:06 +02:00
|
|
|
func (m *StatusLineManager) IsCollapsed(s *models.StatusLineNode) bool {
|
|
|
|
return m.CollapsedPaths[s.GetPath()]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusLineManager) ToggleCollapsed(s *models.StatusLineNode) {
|
|
|
|
m.CollapsedPaths[s.GetPath()] = !m.CollapsedPaths[s.GetPath()]
|
|
|
|
}
|
|
|
|
|
2021-03-14 05:18:23 +02:00
|
|
|
func (m *StatusLineManager) renderAux(s *models.StatusLineNode, prefix string, depth int, diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
|
|
|
|
isRoot := depth == -1
|
2020-11-15 01:45:55 +02:00
|
|
|
if s == nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLine := func() string {
|
2021-03-20 23:41:06 +02:00
|
|
|
return prefix + presentation.GetStatusNodeLine(s.GetHasUnstagedChanges(), s.GetHasStagedChanges(), s.NameAtDepth(depth), diffName, submoduleConfigs, s.File)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.IsLeaf() {
|
2021-03-14 05:18:23 +02:00
|
|
|
if isRoot {
|
2020-11-15 01:45:55 +02:00
|
|
|
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)}
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
arr = append(arr, m.renderAux(child, childPrefix, depth+1, diffName, submoduleConfigs)...)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|