mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-25 18:55:28 -06:00
refactor
This commit is contained in:
@@ -3,6 +3,7 @@ package gui
|
||||
import (
|
||||
"github.com/jesseduffield/gocui"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
|
||||
)
|
||||
|
||||
@@ -104,7 +105,7 @@ func (gui *Gui) refreshCommitFilesView() error {
|
||||
to := gui.State.Panels.CommitFiles.refName
|
||||
from, reverse := gui.getFromAndReverseArgsForDiff(to)
|
||||
|
||||
files, err := gui.GitCommand.GetFilesInDiff(from, to, reverse, gui.GitCommand.PatchManager)
|
||||
files, err := gui.GitCommand.GetFilesInDiff(from, to, reverse)
|
||||
if err != nil {
|
||||
return gui.surfaceError(err)
|
||||
}
|
||||
@@ -137,8 +138,6 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: if file is nil, toggle all leaves underneath on/off
|
||||
|
||||
toggleTheFile := func() error {
|
||||
if !gui.GitCommand.PatchManager.Active() {
|
||||
if err := gui.startPatchManager(); err != nil {
|
||||
@@ -146,15 +145,29 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := gui.GitCommand.PatchManager.ToggleFileWhole(node.GetPath()); err != nil {
|
||||
return err
|
||||
// if there is any file that hasn't been fully added we'll fully add everything,
|
||||
// otherwise we'll remove everything
|
||||
adding := node.AnyFile(func(file *models.CommitFile) bool {
|
||||
return gui.GitCommand.PatchManager.GetFileStatus(file.Name) != patch.WHOLE
|
||||
})
|
||||
|
||||
err := node.ForEachFile(func(file *models.CommitFile) error {
|
||||
if adding {
|
||||
return gui.GitCommand.PatchManager.AddFileWhole(file.Name)
|
||||
} else {
|
||||
return gui.GitCommand.PatchManager.RemoveFile(file.Name)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return gui.surfaceError(err)
|
||||
}
|
||||
|
||||
if gui.GitCommand.PatchManager.IsEmpty() {
|
||||
gui.GitCommand.PatchManager.Reset()
|
||||
}
|
||||
|
||||
return gui.refreshCommitFilesView()
|
||||
return gui.postRefreshUpdate(gui.Contexts.CommitFiles.Context)
|
||||
}
|
||||
|
||||
if gui.GitCommand.PatchManager.Active() && gui.GitCommand.PatchManager.To != gui.State.CommitFileChangeManager.GetParent() {
|
||||
|
||||
@@ -2,6 +2,7 @@ package filetree
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/patch"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -87,9 +88,9 @@ func (m *CommitFileChangeManager) ToggleCollapsed(path string) {
|
||||
m.collapsedPaths.ToggleCollapsed(path)
|
||||
}
|
||||
|
||||
func (m *CommitFileChangeManager) Render(diffName string) []string {
|
||||
func (m *CommitFileChangeManager) Render(diffName string, patchManager *patch.PatchManager) []string {
|
||||
return renderAux(m.tree, m.collapsedPaths, "", -1, func(n INode, depth int) string {
|
||||
castN := n.(*CommitFileChangeNode)
|
||||
return presentation.GetCommitFileLine(castN.NameAtDepth(depth), diffName, castN.File)
|
||||
return presentation.GetCommitFileLine(castN.NameAtDepth(depth), diffName, castN.File, patchManager)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -81,6 +81,21 @@ func (s *CommitFileChangeNode) Any(test func(node *CommitFileChangeNode) bool) b
|
||||
})
|
||||
}
|
||||
|
||||
func (s *CommitFileChangeNode) Every(test func(node *CommitFileChangeNode) bool) bool {
|
||||
return every(s, func(n INode) bool {
|
||||
castNode := n.(*CommitFileChangeNode)
|
||||
return test(castNode)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *CommitFileChangeNode) EveryFile(test func(file *models.CommitFile) bool) bool {
|
||||
return every(s, func(n INode) bool {
|
||||
castNode := n.(*CommitFileChangeNode)
|
||||
|
||||
return castNode.File == nil || test(castNode.File)
|
||||
})
|
||||
}
|
||||
|
||||
func (n *CommitFileChangeNode) Flatten(collapsedPaths map[string]bool) []*CommitFileChangeNode {
|
||||
results := flatten(n, collapsedPaths)
|
||||
nodes := make([]*CommitFileChangeNode, len(results))
|
||||
|
||||
@@ -77,6 +77,20 @@ func any(node INode, test func(INode) bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func every(node INode, test func(INode) bool) bool {
|
||||
if !test(node) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, child := range node.GetChildren() {
|
||||
if !every(child, test) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func flatten(node INode, collapsedPaths map[string]bool) []INode {
|
||||
result := []INode{}
|
||||
result = append(result, node)
|
||||
|
||||
@@ -465,7 +465,7 @@ func (gui *Gui) commitFilesListContext() *ListContext {
|
||||
return [][]string{{utils.ColoredString("(none)", color.FgRed)}}
|
||||
}
|
||||
|
||||
lines := gui.State.CommitFileChangeManager.Render(gui.State.Modes.Diffing.Ref)
|
||||
lines := gui.State.CommitFileChangeManager.Render(gui.State.Modes.Diffing.Ref, gui.GitCommand.PatchManager)
|
||||
mappedLines := make([][]string, len(lines))
|
||||
for i, line := range lines {
|
||||
mappedLines[i] = []string{line}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
)
|
||||
|
||||
func GetCommitFileLine(name string, diffName string, commitFile *models.CommitFile) string {
|
||||
func GetCommitFileLine(name string, diffName string, commitFile *models.CommitFile, patchManager *patch.PatchManager) string {
|
||||
yellow := color.New(color.FgYellow)
|
||||
green := color.New(color.FgGreen)
|
||||
defaultColor := color.New(theme.DefaultTextColor)
|
||||
@@ -22,7 +22,8 @@ func GetCommitFileLine(name string, diffName string, commitFile *models.CommitFi
|
||||
if diffName == name {
|
||||
colour = diffTerminalColor
|
||||
} else if commitFile != nil {
|
||||
switch commitFile.PatchStatus {
|
||||
status := patchManager.GetFileStatus(commitFile.Name)
|
||||
switch status {
|
||||
case patch.UNSELECTED:
|
||||
colour = defaultColor
|
||||
case patch.WHOLE:
|
||||
|
||||
Reference in New Issue
Block a user