diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-03-31 23:26:53 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-02 11:00:15 +1100 |
commit | 9e85d37fb949bbc83f28cb079f2ac4b45ae895ce (patch) | |
tree | baec08e7f7616129d1a23a1834ac9146138efe87 | |
parent | 8dee06f83a1aebe9eb085e67b790f166f9d205af (diff) |
refactor to no longer call these things file changes
19 files changed, 339 insertions, 341 deletions
diff --git a/pkg/commands/files.go b/pkg/commands/files.go index 5443b7d04..3f6d1c377 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/files.go @@ -138,12 +138,12 @@ func (c *GitCommand) DiscardAllFileChanges(file *models.File) error { return c.DiscardUnstagedFileChanges(file) } -func (c *GitCommand) DiscardAllDirChanges(node *filetree.FileChangeNode) error { +func (c *GitCommand) DiscardAllDirChanges(node *filetree.FileNode) error { // this could be more efficient but we would need to handle all the edge cases return node.ForEachFile(c.DiscardAllFileChanges) } -func (c *GitCommand) DiscardUnstagedDirChanges(node *filetree.FileChangeNode) error { +func (c *GitCommand) DiscardUnstagedDirChanges(node *filetree.FileNode) error { if err := c.RemoveUntrackedDirFiles(node); err != nil { return err } @@ -156,9 +156,9 @@ func (c *GitCommand) DiscardUnstagedDirChanges(node *filetree.FileChangeNode) er return nil } -func (c *GitCommand) RemoveUntrackedDirFiles(node *filetree.FileChangeNode) error { +func (c *GitCommand) RemoveUntrackedDirFiles(node *filetree.FileNode) error { untrackedFilePaths := node.GetPathsMatching( - func(n *filetree.FileChangeNode) bool { return n.File != nil && !n.File.GetIsTracked() }, + func(n *filetree.FileNode) bool { return n.File != nil && !n.File.GetIsTracked() }, ) for _, path := range untrackedFilePaths { @@ -189,7 +189,7 @@ func (c *GitCommand) WorktreeFileDiff(file *models.File, plain bool, cached bool return s } -func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFileChange, plain bool, cached bool) string { +func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFile, plain bool, cached bool) string { cachedArg := "" trackedArg := "--" colorArg := c.colorArg() diff --git a/pkg/commands/models/file.go b/pkg/commands/models/file.go index a02beae63..0bbca78ae 100644 --- a/pkg/commands/models/file.go +++ b/pkg/commands/models/file.go @@ -22,7 +22,7 @@ type File struct { } // sometimes we need to deal with either a node (which contains a file) or an actual file -type IFileChange interface { +type IFile interface { GetHasUnstagedChanges() bool GetHasStagedChanges() bool GetIsTracked() bool diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index aa95b0862..25e999c87 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -7,17 +7,15 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/filetree" ) -// todo: rename to getSelectedCommitFileChangeNode, or decide to remove the change part in the context of files -func (gui *Gui) getSelectedCommitFileNode() *filetree.CommitFileChangeNode { +func (gui *Gui) getSelectedCommitFileNode() *filetree.CommitFileNode { selectedLine := gui.State.Panels.CommitFiles.SelectedLineIdx - if selectedLine == -1 || selectedLine > gui.State.CommitFileChangeManager.GetItemsLength()-1 { + if selectedLine == -1 || selectedLine > gui.State.CommitFileManager.GetItemsLength()-1 { return nil } - return gui.State.CommitFileChangeManager.GetItemAtIndex(selectedLine) + return gui.State.CommitFileManager.GetItemAtIndex(selectedLine) } -// todo: rename to getSelectedCommitFileChange func (gui *Gui) getSelectedCommitFile() *models.CommitFile { node := gui.getSelectedCommitFileNode() if node == nil { @@ -42,7 +40,7 @@ func (gui *Gui) handleCommitFileSelect() error { return nil } - to := gui.State.CommitFileChangeManager.GetParent() + to := gui.State.CommitFileManager.GetParent() from, reverse := gui.getFromAndReverseArgsForDiff(to) cmd := gui.OSCommand.ExecutableFromString( @@ -66,7 +64,7 @@ func (gui *Gui) handleCheckoutCommitFile(g *gocui.Gui, v *gocui.View) error { } // TODO: verify this works for directories - if err := gui.GitCommand.CheckoutFile(gui.State.CommitFileChangeManager.GetParent(), node.GetPath()); err != nil { + if err := gui.GitCommand.CheckoutFile(gui.State.CommitFileManager.GetParent(), node.GetPath()); err != nil { return gui.surfaceError(err) } @@ -109,7 +107,7 @@ func (gui *Gui) refreshCommitFilesView() error { if err != nil { return gui.surfaceError(err) } - gui.State.CommitFileChangeManager.SetFiles(files, to) + gui.State.CommitFileManager.SetFiles(files, to) return gui.postRefreshUpdate(gui.Contexts.CommitFiles.Context) } @@ -152,7 +150,7 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error { // 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, gui.State.CommitFileChangeManager.GetParent()) != patch.WHOLE + return gui.GitCommand.PatchManager.GetFileStatus(file.Name, gui.State.CommitFileManager.GetParent()) != patch.WHOLE }) err := node.ForEachFile(func(file *models.CommitFile) error { @@ -174,7 +172,7 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error { return gui.postRefreshUpdate(gui.Contexts.CommitFiles.Context) } - if gui.GitCommand.PatchManager.Active() && gui.GitCommand.PatchManager.To != gui.State.CommitFileChangeManager.GetParent() { + if gui.GitCommand.PatchManager.Active() && gui.GitCommand.PatchManager.To != gui.State.CommitFileManager.GetParent() { return gui.ask(askOpts{ title: gui.Tr.DiscardPatch, prompt: gui.Tr.DiscardPatchConfirm, @@ -225,7 +223,7 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error { return gui.handleRefreshPatchBuildingPanel(selectedLineIdx) } - if gui.GitCommand.PatchManager.Active() && gui.GitCommand.PatchManager.To != gui.State.CommitFileChangeManager.GetParent() { + if gui.GitCommand.PatchManager.Active() && gui.GitCommand.PatchManager.To != gui.State.CommitFileManager.GetParent() { return gui.ask(askOpts{ title: gui.Tr.DiscardPatch, prompt: gui.Tr.DiscardPatchConfirm, @@ -249,7 +247,7 @@ func (gui *Gui) handleToggleCommitFileDirCollapsed() error { return nil } - gui.State.CommitFileChangeManager.ToggleCollapsed(node.GetPath()) + gui.State.CommitFileManager.ToggleCollapsed(node.GetPath()) if err := gui.postRefreshUpdate(gui.Contexts.CommitFiles.Context); err != nil { gui.Log.Error(err) @@ -280,12 +278,12 @@ func (gui *Gui) switchToCommitFilesContext(refName string, canRebase bool, conte func (gui *Gui) handleToggleCommitFileTreeView() error { path := gui.getSelectedCommitFilePath() - gui.State.CommitFileChangeManager.ToggleShowTree() + gui.State.CommitFileManager.ToggleShowTree() // find that same node in the new format and move the cursor to it if path != "" { - gui.State.CommitFileChangeManager.ExpandToPath(path) - index, found := gui.State.CommitFileChangeManager.GetIndexForPath(path) + gui.State.CommitFileManager.ExpandToPath(path) + index, found := gui.State.CommitFileManager.GetIndexForPath(path) if found { gui.commitFilesListContext().GetPanelState().SetSelectedLineIdx(index) } diff --git a/pkg/gui/discard_changes_menu_panel.go b/pkg/gui/discard_changes_menu_panel.go index 9499080e2..daa2d76d8 100644 --- a/pkg/gui/discard_changes_menu_panel.go +++ b/pkg/gui/discard_changes_menu_panel.go @@ -1,7 +1,7 @@ package gui func (gui *Gui) handleCreateDiscardMenu() error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index c244c46e8..c6fcb07d9 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -22,17 +22,17 @@ import ( // list panel functions -func (gui *Gui) getSelectedFileChangeNode() *filetree.FileChangeNode { +func (gui *Gui) getSelectedFileNode() *filetree.FileNode { selectedLine := gui.State.Panels.Files.SelectedLineIdx if selectedLine == -1 { return nil } - return gui.State.FileChangeManager.GetItemAtIndex(selectedLine) + return gui.State.FileManager.GetItemAtIndex(selectedLine) } func (gui *Gui) getSelectedFile() *models.File { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } @@ -40,7 +40,7 @@ func (gui *Gui) getSelectedFile() *models.File { } func (gui *Gui) getSelectedPath() string { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return "" } @@ -51,7 +51,7 @@ func (gui *Gui) getSelectedPath() string { func (gui *Gui) selectFile(alreadySelected bool) error { gui.getFilesView().FocusPoint(0, gui.State.Panels.Files.SelectedLineIdx) - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return gui.refreshMainViews(refreshMainOpts{ @@ -152,7 +152,7 @@ func (gui *Gui) refreshFilesAndSubmodules() error { // specific functions func (gui *Gui) stagedFiles() []*models.File { - files := gui.State.FileChangeManager.GetAllFiles() + files := gui.State.FileManager.GetAllFiles() result := make([]*models.File, 0) for _, file := range files { if file.HasStagedChanges { @@ -163,7 +163,7 @@ func (gui *Gui) stagedFiles() []*models.File { } func (gui *Gui) trackedFiles() []*models.File { - files := gui.State.FileChangeManager.GetAllFiles() + files := gui.State.FileManager.GetAllFiles() result := make([]*models.File, 0, len(files)) for _, file := range files { if file.Tracked { @@ -187,7 +187,7 @@ func (gui *Gui) handleEnterFile(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) enterFile(forceSecondaryFocused bool, selectedLineIdx int) error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } @@ -216,7 +216,7 @@ func (gui *Gui) enterFile(forceSecondaryFocused bool, selectedLineIdx int) error } func (gui *Gui) handleFilePress() error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } @@ -264,7 +264,7 @@ func (gui *Gui) handleFilePress() error { } func (gui *Gui) allFilesStaged() bool { - for _, file := range gui.State.FileChangeManager.GetAllFiles() { + for _, file := range gui.State.FileManager.GetAllFiles() { if file.HasUnstagedChanges { return false } @@ -295,7 +295,7 @@ func (gui *Gui) handleStageAll(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleIgnoreFile() error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } @@ -500,7 +500,7 @@ func (gui *Gui) editFile(filename string) error { } func (gui *Gui) handleFileEdit(g *gocui.Gui, v *gocui.View) error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } @@ -513,7 +513,7 @@ func (gui *Gui) handleFileEdit(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleFileOpen(g *gocui.Gui, v *gocui.View) error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } @@ -530,9 +530,9 @@ func (gui *Gui) refreshStateFiles() error { // when we refresh, go looking for a matching name // move the cursor to there. - selectedNode := gui.getSelectedFileChangeNode() + selectedNode := gui.getSelectedFileNode() - prevNodes := gui.State.FileChangeManager.GetAllItems() + prevNodes := gui.State.FileManager.GetAllItems() prevSelectedLineIdx := gui.State.Panels.Files.SelectedLineIdx files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{}) @@ -540,24 +540,24 @@ func (gui *Gui) refreshStateFiles() error { // for when you stage the old file of a rename and the new file is in a collapsed dir for _, file := range files { if selectedNode != nil && selectedNode.Path != "" && file.PreviousName == selectedNode.Path { - gui.State.FileChangeManager.ExpandToPath(file.Name) + gui.State.FileManager.ExpandToPath(file.Name) } } - gui.State.FileChangeManager.SetFiles(files) + gui.State.FileManager.SetFiles(files) if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil { return err } if selectedNode != nil { - newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], gui.State.FileChangeManager.GetAllItems()) + newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], gui.State.FileManager.GetAllItems()) if newIdx != -1 && newIdx != prevSelectedLineIdx { gui.State.Panels.Files.SelectedLineIdx = newIdx } } - gui.refreshSelectedLine(gui.State.Panels.Files, gui.State.FileChangeManager.GetItemsLength()) + gui.refreshSelectedLine(gui.State.Panels.Files, gui.State.FileManager.GetItemsLength()) return nil } @@ -568,8 +568,8 @@ func (gui *Gui) refreshStateFiles() error { // nodes until we find one that exists in the new set of nodes, then move the cursor // to that. // prevNodes starts from our previously selected node because we don't need to consider anything above that -func (gui *Gui) findNewSelectedIdx(prevNodes []*filetree.FileChangeNode, currNodes []*filetree.FileChangeNode) int { - getPaths := func(node *filetree.FileChangeNode) []string { +func (gui *Gui) findNewSelectedIdx(prevNodes []*filetree.FileNode, currNodes []*filetree.FileNode) int { + getPaths := func(node *filetree.FileNode) []string { if node == nil { return nil } @@ -788,7 +788,7 @@ func (gui *Gui) openFile(filename string) error { } func (gui *Gui) anyFilesWithMergeConflicts() bool { - for _, file := range gui.State.FileChangeManager.GetAllFiles() { + for _, file := range gui.State.FileManager.GetAllFiles() { if file.HasMergeConflicts { return true } @@ -834,12 +834,12 @@ func (gui *Gui) handleCreateResetToUpstreamMenu(g *gocui.Gui, v *gocui.View) err } func (gui *Gui) handleToggleDirCollapsed() error { - node := gui.getSelectedFileChangeNode() + node := gui.getSelectedFileNode() if node == nil { return nil } - gui.State.FileChangeManager.ToggleCollapsed(node.GetPath()) + gui.State.FileManager.ToggleCollapsed(node.GetPath()) if err := gui.postRefreshUpdate(gui.Contexts.Files.Context); err != nil { gui.Log.Error(err) @@ -852,12 +852,12 @@ func (gui *Gui) handleToggleFileTreeView() error { // get path of currently selected file path := gui.getSelectedPath() - gui.State.FileChangeManager.ToggleShowTree() + gui.State.FileManager.ToggleShowTree() // find that same node in the new format and move the cursor to it if path != "" { - gui.State.FileChangeManager.ExpandToPath(path) - index, found := gui.State.FileChangeManager.GetIndexForPath(path) + gui.State.FileManager.ExpandToPath(path) + index, found := gui.State.FileManager.GetIndexForPath(path) if found { gui.filesListContext().GetPanelState().SetSelectedLineIdx(index) } diff --git a/pkg/gui/filetree/build_tree.go b/pkg/gui/filetree/build_tree.go index d6fe9b98d..ab7ac40a4 100644 --- a/pkg/gui/filetree/build_tree.go +++ b/pkg/gui/filetree/build_tree.go @@ -9,10 +9,10 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" ) -func BuildTreeFromFiles(files []*models.File) *FileChangeNode { - root := &FileChangeNode{} +func BuildTreeFromFiles(files []*models.File) *FileNode { + root := &FileNode{} - var curr *FileChangeNode + var curr *FileNode for _, file := range files { split := strings.Split(file.Name, string(os.PathSeparator)) curr = root @@ -33,7 +33,7 @@ func BuildTreeFromFiles(files []*models.File) *FileChangeNode { } } - newChild := &FileChangeNode{ + newChild := &FileNode{ Path: path, File: setFile, } @@ -49,17 +49,17 @@ func BuildTreeFromFiles(files []*models.File) *FileChangeNode { return root } -func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode { +func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *CommitFileNode { rootAux := BuildTreeFromCommitFiles(files) sortedFiles := rootAux.GetLeaves() - return &CommitFileChangeNode{Children: sortedFiles} + return &CommitFileNode{Children: sortedFiles} } -func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode { - root := &CommitFileChangeNode{} +func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileNode { + root := &CommitFileNode{} - var curr *CommitFileChangeNode + var curr *CommitFileNode for _, file := range files { split := strings.Split(file.Name, string(os.PathSeparator)) curr = root @@ -80,7 +80,7 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode } } - newChild := &CommitFileChangeNode{ + newChild := &CommitFileNode{ Path: path, File: setFile, } @@ -96,7 +96,7 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode return root } -func BuildFlatTreeFromFiles(files []*models.File) *FileChangeNode { +func BuildFlatTreeFromFiles(files []*models.File) *FileNode { rootAux := BuildTreeFromFiles(files) sortedFiles := rootAux.GetLeaves() @@ -106,5 +106,5 @@ func BuildFlatTreeFromFiles(files []*models.File) *FileChangeNode { return sortedFiles[i].File != nil && sortedFiles[i].File.HasMergeConflicts && !(sortedFiles[j].File != nil && sortedFiles[j].File.HasMergeConflicts) }) - return &FileChangeNode{Children: sortedFiles} + return &FileNode{Children: sortedFiles} } diff --git a/pkg/gui/filetree/commit_file_change_node.go b/pkg/gui/filetree/commit_file_change_node.go deleted file mode 100644 index 011c1d954..000000000 --- a/pkg/gui/filetree/commit_file_change_node.go +++ /dev/null @@ -1,162 +0,0 @@ -package filetree - -import ( - "os" - "path/filepath" - "strings" - - "github.com/jesseduffield/lazygit/pkg/commands/models" -) - -type CommitFileChangeNode struct { - Children []*CommitFileChangeNode - File *models.CommitFile - Path string // e.g. '/path/to/mydir' - CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode -} - -// methods satisfying ListItem interface - -func (s *CommitFileChangeNode) ID() string { - return s.GetPath() -} - -func (s *CommitFileChangeNode) Description() string { - return s.GetPath() -} - -// methods satisfying INode interface - -func (s *CommitFileChangeNode) IsLeaf() bool { - return s.File != nil -} - -func (s *CommitFileChangeNode) GetPath() string { - return s.Path -} - -func (s *CommitFileChangeNode) GetChildren() []INode { - result := make([]INode, len(s.Children)) - for i, child := range s.Children { - result[i] = child - } - - return result -} - -func (s *CommitFileChangeNode) SetChildren(children []INode) { - castChildren := make([]*CommitFileChangeNode, len(children)) - for i, child := range children { - castChildren[i] = child.(*CommitFileChangeNode) - } - - s.Children = castChildren -} - -func (s *CommitFileChangeNode) GetCompressionLevel() int { - return s.CompressionLevel -} - -func (s *CommitFileChangeNode) SetCompressionLevel(level int) { - s.CompressionLevel = level -} - -// methods utilising generic functions for INodes - -func (s *CommitFileChangeNode) Sort() { - sortNode(s) -} - -func (s *CommitFileChangeNode) ForEachFile(cb func(*models.CommitFile) error) error { - return forEachLeaf(s, func(n INode) error { - castNode := n.(*CommitFileChangeNode) - return cb(castNode.File) - }) -} - -func (s *CommitFileChangeNode) Any(test func(node *CommitFileChangeNode) bool) bool { - return any(s, func(n INode) bool { - castNode := n.(*CommitFileChangeNode) - return test(castNode) - }) -} - -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)) - for i, result := range results { - nodes[i] = result.(*CommitFileChangeNode) - } - - return nodes -} - -func (node *CommitFileChangeNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileChangeNode { - return getNodeAtIndex(node, index, collapsedPaths).(*CommitFileChangeNode) -} - -func (node *CommitFileChangeNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { - return getIndexForPath(node, path, collapsedPaths) -} - -func (node *CommitFileChangeNode) Size(collapsedPaths map[string]bool) int { - return size(node, collapsedPaths) -} - -func (s *CommitFileChangeNode) Compress() { - // with these functions I try to only have type conversion code on the actual struct, - // but comparing interface values to nil is fraught with danger so I'm duplicating - // that code here. - if s == nil { - return - } - - compressAux(s) -} - -// This ignores the root -func (node *CommitFileChangeNode) GetPathsMatching(test func(*CommitFileChangeNode) bool) []string { - return getPathsMatching(node, func(n INode) bool { - return test(n.(*CommitFileChangeNode)) - }) -} - -func (s *CommitFileChangeNode) GetLeaves() []*CommitFileChangeNode { - leaves := getLeaves(s) - castLeaves := make([]*CommitFileChangeNode, len(leaves)) - for i := range leaves { - castLeaves[i] = leaves[i].(*CommitFileChangeNode) - } - - return castLeaves -} - -// extra methods - -func (s *CommitFileChangeNode) AnyFile(test func(file *models.CommitFile) bool) bool { - return s.Any(func(node *CommitFileChangeNode) bool { - return node.IsLeaf() && test(node.File) - }) -} - -func (s *CommitFileChangeNode) NameAtDepth(depth int) string { - splitName := strings.Split(s.Path, string(os.PathSeparator)) - name := filepath.Join(splitName[depth:]...) - - return name -} diff --git a/pkg/gui/filetree/commit_file_change_manager.go b/pkg/gui/filetree/commit_file_manager.go index ee05ca3b9..3fd89b63e 100644 --- a/pkg/gui/filetree/commit_file_change_manager.go +++ b/pkg/gui/filetree/commit_file_manager.go @@ -7,9 +7,9 @@ import ( "github.com/sirupsen/logrus" ) -type CommitFileChangeManager struct { +type CommitFileManager struct { files []*models.CommitFile - tree *CommitFileChangeNode + tree *CommitFileNode showTree bool log *logrus.Entry collapsedPaths CollapsedPaths @@ -17,12 +17,12 @@ type CommitFileChangeManager struct { parent string } -func (m *CommitFileChangeManager) GetParent() string { +func (m *CommitFileManager) GetParent() string { return m.parent } -func NewCommitFileChangeManager(files []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileChangeManager { - return &CommitFileChangeManager{ +func NewCommitFileManager(files []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileManager { + return &CommitFileManager{ files: files, log: log, showTree: showTree, @@ -30,26 +30,26 @@ func NewCommitFileChangeManager(files []*models.CommitFile, log *logrus.Entry, s } } -func (m *CommitFileChangeManager) ExpandToPath(path string) { +func (m *CommitFileManager) ExpandToPath(path string) { m.collapsedPaths.ExpandToPath(path) } -func (m *CommitFileChangeManager) ToggleShowTree() { +func (m *CommitFileManager) ToggleShowTree() { m.showTree = !m.showTree m.SetTree() } -func (m *CommitFileChangeManager) GetItemAtIndex(index int) *CommitFileChangeNode { +func (m *CommitFileManager) GetItemAtIndex(index int) *CommitFileNode { // need to traverse the three depth first until we get to the index. return m.tree.GetNodeAtIndex(index+1, m.collapsedPaths) // ignoring root } -func (m *CommitFileChangeManager) GetIndexForPath(path string) (int, bool) { +func (m *CommitFileManager) GetIndexForPath(path string) (int, bool) { index, found := m.tree.GetIndexForPath(path, m.collapsedPaths) return index - 1, found } -func (m *CommitFileChangeManager) GetAllItems() []*CommitFileChangeNode { +func (m *CommitFileManager) GetAllItems() []*CommitFileNode { if m.tree == nil { return nil } @@ -57,22 +57,22 @@ func (m *CommitFileChangeManager) GetAllItems() []*CommitFileChangeNode { return m.tree.Flatten(m.collapsedPaths)[1:] // ignoring root } -func (m *CommitFileChangeManager) GetItemsLength() int { +func (m *CommitFileManager) GetItemsLength() int { return m.tree.Size(m.collapsedPaths) - 1 // ignoring root } -func (m *CommitFileChangeManager) GetAllFiles() []*models.CommitFile { +func (m *CommitFileManager) GetAllFiles() []*models.CommitFile { return m.files } -func (m *CommitFileChangeManager) SetFiles(files []*models.CommitFile, parent string) { +func (m *CommitFileManager) SetFiles(files []*models.CommitFile, parent string) { m.files = files m.parent = parent m.SetTree() } -func (m *CommitFileChangeManager) SetTree() { +func (m *CommitFileManager) SetTree() { if m.showTree { m.tree = BuildTreeFromCommitFiles(m.files) } else { @@ -80,17 +80,17 @@ func (m *CommitFileChangeManager) SetTree() { } } -func (m *CommitFileChangeManager) IsCollapsed(path string) bool { +func (m *CommitFileManager) IsCollapsed(path string) bool { return m.collapsedPaths.IsCollapsed(path) } -func (m *CommitFileChangeManager) ToggleCollapsed(path string) { +func (m *CommitFileManager) ToggleCollapsed(path string) { m.collapsedPaths.ToggleCollapsed(path) } -func (m *CommitFileChangeManager) Render(diffName string, patchManager *patch.PatchManager) []string { +func (m *CommitFileManager) Render(diffName string, patchManager *patch.PatchManager) []string { return renderAux(m.tree, m.collapsedPaths, "", -1, func(n INode, depth int) string { - castN := n.(*CommitFileChangeNode) + castN := n.(*CommitFileNode) // This is a little convoluted because we're dealing with either a leaf or a non-leaf. // But this code actually applies to both. If it's a leaf, the status will just diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go new file mode 100644 index 000000000..1f1be2a3f --- /dev/null +++ b/pkg/gui/filetree/commit_file_node.go @@ -0,0 +1,162 @@ +package filetree + +import ( + "os" + "path/filepath" + "strings" + + "github.com/jesseduffield/lazygit/pkg/commands/models" +) + +type CommitFileNode struct { + Children []*CommitFileNode + File *models.CommitFile + Path string // e.g. '/path/to/mydir' + CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode +} + +// methods satisfying ListItem interface + |