1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-21 00:30:00 +02:00

rename some things

This commit is contained in:
Jesse Duffield
2021-03-21 10:23:14 +11:00
parent 120bb443fe
commit 1b94462410
11 changed files with 100 additions and 100 deletions

View File

@ -137,12 +137,12 @@ func (c *GitCommand) DiscardAllFileChanges(file *models.File) error {
return c.DiscardUnstagedFileChanges(file) return c.DiscardUnstagedFileChanges(file)
} }
func (c *GitCommand) DiscardAllDirChanges(node *models.StatusLineNode) error { func (c *GitCommand) DiscardAllDirChanges(node *models.FileChangeNode) error {
// this could be more efficient but we would need to handle all the edge cases // this could be more efficient but we would need to handle all the edge cases
return node.ForEachFile(c.DiscardAllFileChanges) return node.ForEachFile(c.DiscardAllFileChanges)
} }
func (c *GitCommand) DiscardUnstagedDirChanges(node *models.StatusLineNode) error { func (c *GitCommand) DiscardUnstagedDirChanges(node *models.FileChangeNode) error {
if err := c.RemoveUntrackedDirFiles(node); err != nil { if err := c.RemoveUntrackedDirFiles(node); err != nil {
return err return err
} }
@ -155,9 +155,9 @@ func (c *GitCommand) DiscardUnstagedDirChanges(node *models.StatusLineNode) erro
return nil return nil
} }
func (c *GitCommand) RemoveUntrackedDirFiles(node *models.StatusLineNode) error { func (c *GitCommand) RemoveUntrackedDirFiles(node *models.FileChangeNode) error {
untrackedFilePaths := node.GetPathsMatching( untrackedFilePaths := node.GetPathsMatching(
func(n *models.StatusLineNode) bool { return n.File != nil && !n.File.GetIsTracked() }, func(n *models.FileChangeNode) bool { return n.File != nil && !n.File.GetIsTracked() },
) )
for _, path := range untrackedFilePaths { for _, path := range untrackedFilePaths {
@ -188,7 +188,7 @@ func (c *GitCommand) WorktreeFileDiff(file *models.File, plain bool, cached bool
return s return s
} }
func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IStatusLine, plain bool, cached bool) string { func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFileChange, plain bool, cached bool) string {
cachedArg := "" cachedArg := ""
trackedArg := "--" trackedArg := "--"
colorArg := c.colorArg() colorArg := c.colorArg()

View File

@ -1,6 +1,6 @@
package models package models
type IStatusLine interface { type IFileChange interface {
GetHasUnstagedChanges() bool GetHasUnstagedChanges() bool
GetHasStagedChanges() bool GetHasStagedChanges() bool
GetIsTracked() bool GetIsTracked() bool

View File

@ -8,14 +8,14 @@ import (
"strings" "strings"
) )
type StatusLineNode struct { type FileChangeNode struct {
Children []*StatusLineNode Children []*FileChangeNode
File *File File *File
Path string // e.g. '/path/to/mydir' Path string // e.g. '/path/to/mydir'
Collapsed bool Collapsed bool
} }
func (s *StatusLineNode) GetHasUnstagedChanges() bool { func (s *FileChangeNode) GetHasUnstagedChanges() bool {
if s.IsLeaf() { if s.IsLeaf() {
return s.File.HasUnstagedChanges return s.File.HasUnstagedChanges
} }
@ -29,7 +29,7 @@ func (s *StatusLineNode) GetHasUnstagedChanges() bool {
return false return false
} }
func (s *StatusLineNode) GetHasStagedChanges() bool { func (s *FileChangeNode) GetHasStagedChanges() bool {
if s.IsLeaf() { if s.IsLeaf() {
return s.File.HasStagedChanges return s.File.HasStagedChanges
} }
@ -43,13 +43,13 @@ func (s *StatusLineNode) GetHasStagedChanges() bool {
return false return false
} }
func (s *StatusLineNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *StatusLineNode { func (s *FileChangeNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileChangeNode {
node, _ := s.getNodeAtIndexAux(index, collapsedPaths) node, _ := s.getNodeAtIndexAux(index, collapsedPaths)
return node return node
} }
func (s *StatusLineNode) getNodeAtIndexAux(index int, collapsedPaths map[string]bool) (*StatusLineNode, int) { func (s *FileChangeNode) getNodeAtIndexAux(index int, collapsedPaths map[string]bool) (*FileChangeNode, int) {
offset := 1 offset := 1
if index == 0 { if index == 0 {
@ -69,11 +69,11 @@ func (s *StatusLineNode) getNodeAtIndexAux(index int, collapsedPaths map[string]
return nil, offset return nil, offset
} }
func (s *StatusLineNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { func (s *FileChangeNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) {
return s.getIndexForPathAux(path, collapsedPaths) return s.getIndexForPathAux(path, collapsedPaths)
} }
func (s *StatusLineNode) getIndexForPathAux(path string, collapsedPaths map[string]bool) (int, bool) { func (s *FileChangeNode) getIndexForPathAux(path string, collapsedPaths map[string]bool) (int, bool) {
offset := 0 offset := 0
if s.Path == path { if s.Path == path {
@ -93,11 +93,11 @@ func (s *StatusLineNode) getIndexForPathAux(path string, collapsedPaths map[stri
return offset, false return offset, false
} }
func (s *StatusLineNode) IsLeaf() bool { func (s *FileChangeNode) IsLeaf() bool {
return len(s.Children) == 0 return len(s.Children) == 0
} }
func (s *StatusLineNode) Size(collapsedPaths map[string]bool) int { func (s *FileChangeNode) Size(collapsedPaths map[string]bool) int {
output := 1 output := 1
if !collapsedPaths[s.GetPath()] { if !collapsedPaths[s.GetPath()] {
@ -109,8 +109,8 @@ func (s *StatusLineNode) Size(collapsedPaths map[string]bool) int {
return output return output
} }
func (s *StatusLineNode) Flatten(collapsedPaths map[string]bool) []*StatusLineNode { func (s *FileChangeNode) Flatten(collapsedPaths map[string]bool) []*FileChangeNode {
arr := []*StatusLineNode{s} arr := []*FileChangeNode{s}
if !collapsedPaths[s.GetPath()] { if !collapsedPaths[s.GetPath()] {
for _, child := range s.Children { for _, child := range s.Children {
@ -121,7 +121,7 @@ func (s *StatusLineNode) Flatten(collapsedPaths map[string]bool) []*StatusLineNo
return arr return arr
} }
func (s *StatusLineNode) Sort() { func (s *FileChangeNode) Sort() {
s.sortChildren() s.sortChildren()
for _, child := range s.Children { for _, child := range s.Children {
@ -129,12 +129,12 @@ func (s *StatusLineNode) Sort() {
} }
} }
func (s *StatusLineNode) sortChildren() { func (s *FileChangeNode) sortChildren() {
if s.IsLeaf() { if s.IsLeaf() {
return return
} }
sortedChildren := make([]*StatusLineNode, len(s.Children)) sortedChildren := make([]*FileChangeNode, len(s.Children))
copy(sortedChildren, s.Children) copy(sortedChildren, s.Children)
sort.Slice(sortedChildren, func(i, j int) bool { sort.Slice(sortedChildren, func(i, j int) bool {
@ -153,7 +153,7 @@ func (s *StatusLineNode) sortChildren() {
} }
// returns true if any descendant file is tracked // returns true if any descendant file is tracked
func (s *StatusLineNode) GetIsTracked() bool { func (s *FileChangeNode) GetIsTracked() bool {
if s.File != nil { if s.File != nil {
return s.File.GetIsTracked() return s.File.GetIsTracked()
} }
@ -167,11 +167,11 @@ func (s *StatusLineNode) GetIsTracked() bool {
return false return false
} }
func (s *StatusLineNode) GetPath() string { func (s *FileChangeNode) GetPath() string {
return s.Path return s.Path
} }
func (s *StatusLineNode) Compress() { func (s *FileChangeNode) Compress() {
if s == nil { if s == nil {
return return
} }
@ -179,7 +179,7 @@ func (s *StatusLineNode) Compress() {
s.compressAux() s.compressAux()
} }
func (s *StatusLineNode) compressAux() *StatusLineNode { func (s *FileChangeNode) compressAux() *FileChangeNode {
if s.IsLeaf() { if s.IsLeaf() {
return s return s
} }
@ -198,12 +198,12 @@ func (s *StatusLineNode) compressAux() *StatusLineNode {
return s return s
} }
func (s *StatusLineNode) HasExactlyOneChild() bool { func (s *FileChangeNode) HasExactlyOneChild() bool {
return len(s.Children) == 1 return len(s.Children) == 1
} }
// This ignores the root // This ignores the root
func (s *StatusLineNode) GetPathsMatching(test func(*StatusLineNode) bool) []string { func (s *FileChangeNode) GetPathsMatching(test func(*FileChangeNode) bool) []string {
paths := []string{} paths := []string{}
if test(s) { if test(s) {
@ -217,15 +217,15 @@ func (s *StatusLineNode) GetPathsMatching(test func(*StatusLineNode) bool) []str
return paths return paths
} }
func (s *StatusLineNode) ID() string { func (s *FileChangeNode) ID() string {
return s.GetPath() return s.GetPath()
} }
func (s *StatusLineNode) Description() string { func (s *FileChangeNode) Description() string {
return s.GetPath() return s.GetPath()
} }
func (s *StatusLineNode) ForEachFile(cb func(*File) error) error { func (s *FileChangeNode) ForEachFile(cb func(*File) error) error {
if s.File != nil { if s.File != nil {
if err := cb(s.File); err != nil { if err := cb(s.File); err != nil {
return err return err
@ -241,7 +241,7 @@ func (s *StatusLineNode) ForEachFile(cb func(*File) error) error {
return nil return nil
} }
func (s *StatusLineNode) NameAtDepth(depth int) string { func (s *FileChangeNode) NameAtDepth(depth int) string {
splitName := strings.Split(s.Path, string(os.PathSeparator)) splitName := strings.Split(s.Path, string(os.PathSeparator))
name := filepath.Join(splitName[depth:]...) name := filepath.Join(splitName[depth:]...)

View File

@ -9,8 +9,8 @@ import (
func TestCompress(t *testing.T) { func TestCompress(t *testing.T) {
scenarios := []struct { scenarios := []struct {
name string name string
root *StatusLineNode root *FileChangeNode
expected *StatusLineNode expected *FileChangeNode
}{ }{
{ {
name: "nil node", name: "nil node",
@ -19,28 +19,28 @@ func TestCompress(t *testing.T) {
}, },
{ {
name: "leaf node", name: "leaf node",
root: &StatusLineNode{ root: &FileChangeNode{
Name: "", Name: "",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Name: "test"}, {File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Name: "test"},
}, },
}, },
expected: &StatusLineNode{ expected: &FileChangeNode{
Name: "", Name: "",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Name: "test"}, {File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Name: "test"},
}, },
}, },
}, },
{ {
name: "big example", name: "big example",
root: &StatusLineNode{ root: &FileChangeNode{
Name: "", Name: "",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
Name: "dir1", Name: "dir1",
Path: "dir1", Path: "dir1",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true}, File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Name: "file2", Name: "file2",
@ -51,7 +51,7 @@ func TestCompress(t *testing.T) {
{ {
Name: "dir2", Name: "dir2",
Path: "dir2", Path: "dir2",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true}, File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Name: "file3", Name: "file3",
@ -67,11 +67,11 @@ func TestCompress(t *testing.T) {
{ {
Name: "dir3", Name: "dir3",
Path: "dir3", Path: "dir3",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
Name: "dir3-1", Name: "dir3-1",
Path: "dir3/dir3-1", Path: "dir3/dir3-1",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true}, File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Name: "file5", Name: "file5",
@ -88,9 +88,9 @@ func TestCompress(t *testing.T) {
}, },
}, },
}, },
expected: &StatusLineNode{ expected: &FileChangeNode{
Name: "", Name: "",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
Name: "dir1/file2", Name: "dir1/file2",
File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true}, File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
@ -99,7 +99,7 @@ func TestCompress(t *testing.T) {
{ {
Name: "dir2", Name: "dir2",
Path: "dir2", Path: "dir2",
Children: []*StatusLineNode{ Children: []*FileChangeNode{
{ {
File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true}, File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Name: "file3", Name: "file3",

View File

@ -21,13 +21,13 @@ import (
// list panel functions // list panel functions
func (gui *Gui) getSelectedStatusNode() *models.StatusLineNode { func (gui *Gui) getSelectedStatusNode() *models.FileChangeNode {
selectedLine := gui.State.Panels.Files.SelectedLineIdx selectedLine := gui.State.Panels.Files.SelectedLineIdx
if selectedLine == -1 { if selectedLine == -1 {
return nil return nil
} }
return gui.State.StatusLineManager.GetItemAtIndex(selectedLine) return gui.State.FileChangeManager.GetItemAtIndex(selectedLine)
} }
func (gui *Gui) getSelectedFile() *models.File { func (gui *Gui) getSelectedFile() *models.File {
@ -36,7 +36,7 @@ func (gui *Gui) getSelectedFile() *models.File {
return nil return nil
} }
node := gui.State.StatusLineManager.GetItemAtIndex(selectedLine) node := gui.State.FileChangeManager.GetItemAtIndex(selectedLine)
if node == nil { if node == nil {
return nil return nil
} }
@ -156,7 +156,7 @@ func (gui *Gui) refreshFilesAndSubmodules() error {
// specific functions // specific functions
func (gui *Gui) stagedFiles() []*models.File { func (gui *Gui) stagedFiles() []*models.File {
files := gui.State.StatusLineManager.GetAllFiles() files := gui.State.FileChangeManager.GetAllFiles()
result := make([]*models.File, 0) result := make([]*models.File, 0)
for _, file := range files { for _, file := range files {
if file.HasStagedChanges { if file.HasStagedChanges {
@ -167,7 +167,7 @@ func (gui *Gui) stagedFiles() []*models.File {
} }
func (gui *Gui) trackedFiles() []*models.File { func (gui *Gui) trackedFiles() []*models.File {
files := gui.State.StatusLineManager.GetAllFiles() files := gui.State.FileChangeManager.GetAllFiles()
result := make([]*models.File, 0, len(files)) result := make([]*models.File, 0, len(files))
for _, file := range files { for _, file := range files {
if file.Tracked { if file.Tracked {
@ -264,7 +264,7 @@ func (gui *Gui) handleFilePress() error {
} }
func (gui *Gui) allFilesStaged() bool { func (gui *Gui) allFilesStaged() bool {
for _, file := range gui.State.StatusLineManager.GetAllFiles() { for _, file := range gui.State.FileChangeManager.GetAllFiles() {
if file.HasUnstagedChanges { if file.HasUnstagedChanges {
return false return false
} }
@ -525,24 +525,24 @@ func (gui *Gui) refreshStateFiles() error {
selectedNode := gui.getSelectedStatusNode() selectedNode := gui.getSelectedStatusNode()
prevNodes := gui.State.StatusLineManager.GetAllItems() prevNodes := gui.State.FileChangeManager.GetAllItems()
prevSelectedLineIdx := gui.State.Panels.Files.SelectedLineIdx prevSelectedLineIdx := gui.State.Panels.Files.SelectedLineIdx
files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{}) files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{})
gui.State.StatusLineManager.SetFiles(files) gui.State.FileChangeManager.SetFiles(files)
if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil { if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil {
return err return err
} }
if selectedNode != nil { if selectedNode != nil {
newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], gui.State.StatusLineManager.GetAllItems()) newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], gui.State.FileChangeManager.GetAllItems())
if newIdx != -1 && newIdx != prevSelectedLineIdx { if newIdx != -1 && newIdx != prevSelectedLineIdx {
gui.State.Panels.Files.SelectedLineIdx = newIdx gui.State.Panels.Files.SelectedLineIdx = newIdx
} }
} }
gui.refreshSelectedLine(gui.State.Panels.Files, gui.State.StatusLineManager.GetItemsLength()) gui.refreshSelectedLine(gui.State.Panels.Files, gui.State.FileChangeManager.GetItemsLength())
return nil return nil
} }
@ -553,8 +553,8 @@ func (gui *Gui) refreshStateFiles() error {
// nodes until we find one that exists in the new set of nodes, then move the cursor // nodes until we find one that exists in the new set of nodes, then move the cursor
// to that. // to that.
// prevNodes starts from our previously selected node because we don't need to consider anything above that // prevNodes starts from our previously selected node because we don't need to consider anything above that
func (gui *Gui) findNewSelectedIdx(prevNodes []*models.StatusLineNode, currNodes []*models.StatusLineNode) int { func (gui *Gui) findNewSelectedIdx(prevNodes []*models.FileChangeNode, currNodes []*models.FileChangeNode) int {
getPaths := func(node *models.StatusLineNode) []string { getPaths := func(node *models.FileChangeNode) []string {
if node == nil { if node == nil {
return nil return nil
} }
@ -773,7 +773,7 @@ func (gui *Gui) openFile(filename string) error {
} }
func (gui *Gui) anyFilesWithMergeConflicts() bool { func (gui *Gui) anyFilesWithMergeConflicts() bool {
for _, file := range gui.State.StatusLineManager.GetAllFiles() { for _, file := range gui.State.FileChangeManager.GetAllFiles() {
if file.HasMergeConflicts { if file.HasMergeConflicts {
return true return true
} }
@ -824,7 +824,7 @@ func (gui *Gui) handleToggleDirCollapsed() error {
return nil return nil
} }
gui.State.StatusLineManager.ToggleCollapsed(node) gui.State.FileChangeManager.ToggleCollapsed(node)
if err := gui.postRefreshUpdate(gui.Contexts.Files.Context); err != nil { if err := gui.postRefreshUpdate(gui.Contexts.Files.Context); err != nil {
gui.Log.Error(err) gui.Log.Error(err)
@ -840,12 +840,12 @@ func (gui *Gui) handleToggleFileTreeView() error {
if node != nil { if node != nil {
path = node.Path path = node.Path
} }
gui.State.StatusLineManager.ShowTree = !gui.State.StatusLineManager.ShowTree gui.State.FileChangeManager.ShowTree = !gui.State.FileChangeManager.ShowTree
gui.State.StatusLineManager.SetTree() gui.State.FileChangeManager.SetTree()
// find that same node in the new format and move the cursor to it // find that same node in the new format and move the cursor to it
if path != "" { if path != "" {
index, found := gui.State.StatusLineManager.GetIndexForPath(path) index, found := gui.State.FileChangeManager.GetIndexForPath(path)
if found { if found {
gui.filesListContext().GetPanelState().SetSelectedLineIdx(index) gui.filesListContext().GetPanelState().SetSelectedLineIdx(index)
} }

View File

@ -303,7 +303,7 @@ type guiStateMutexes struct {
} }
type guiState struct { type guiState struct {
StatusLineManager *StatusLineManager FileChangeManager *FileChangeManager
Submodules []*models.SubmoduleConfig Submodules []*models.SubmoduleConfig
Branches []*models.Branch Branches []*models.Branch
Commits []*models.Commit Commits []*models.Commit
@ -380,7 +380,7 @@ func (gui *Gui) resetState() {
showTree := gui.Config.GetUserConfig().Gui.ShowFileTree showTree := gui.Config.GetUserConfig().Gui.ShowFileTree
gui.State = &guiState{ gui.State = &guiState{
StatusLineManager: NewStatusLineManager(make([]*models.File, 0), gui.Log, showTree), FileChangeManager: NewFileChangeManager(make([]*models.File, 0), gui.Log, showTree),
Commits: make([]*models.Commit, 0), Commits: make([]*models.Commit, 0),
FilteredReflogCommits: make([]*models.Commit, 0), FilteredReflogCommits: make([]*models.Commit, 0),
ReflogCommits: make([]*models.Commit, 0), ReflogCommits: make([]*models.Commit, 0),

View File

@ -262,7 +262,7 @@ func (gui *Gui) filesListContext() *ListContext {
return &ListContext{ return &ListContext{
ViewName: "files", ViewName: "files",
ContextKey: FILES_CONTEXT_KEY, ContextKey: FILES_CONTEXT_KEY,
GetItemsLength: func() int { return gui.State.StatusLineManager.GetItemsLength() }, GetItemsLength: func() int { return gui.State.FileChangeManager.GetItemsLength() },
GetPanelState: func() IListPanelState { return gui.State.Panels.Files }, GetPanelState: func() IListPanelState { return gui.State.Panels.Files },
OnFocus: gui.focusAndSelectFile, OnFocus: gui.focusAndSelectFile,
OnClickSelectedItem: gui.handleFilePress, OnClickSelectedItem: gui.handleFilePress,
@ -270,7 +270,7 @@ func (gui *Gui) filesListContext() *ListContext {
ResetMainViewOriginOnFocus: false, ResetMainViewOriginOnFocus: false,
Kind: SIDE_CONTEXT, Kind: SIDE_CONTEXT,
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
lines := gui.State.StatusLineManager.Render(gui.State.Modes.Diffing.Ref, gui.State.Submodules) lines := gui.State.FileChangeManager.Render(gui.State.Modes.Diffing.Ref, gui.State.Submodules)
mappedLines := make([][]string, len(lines)) mappedLines := make([][]string, len(lines))
for i, line := range lines { for i, line := range lines {
mappedLines[i] = []string{line} mappedLines[i] = []string{line}
@ -279,7 +279,7 @@ func (gui *Gui) filesListContext() *ListContext {
return mappedLines return mappedLines
// TODO: Fix this up // TODO: Fix this up
return presentation.GetFileListDisplayStrings(gui.State.StatusLineManager.GetAllFiles(), gui.State.Modes.Diffing.Ref, gui.State.Submodules) return presentation.GetFileListDisplayStrings(gui.State.FileChangeManager.GetAllFiles(), gui.State.Modes.Diffing.Ref, gui.State.Submodules)
}, },
SelectedItem: func() (ListItem, bool) { SelectedItem: func() (ListItem, bool) {
item := gui.getSelectedStatusNode() item := gui.getSelectedStatusNode()

View File

@ -12,16 +12,16 @@ import (
const EXPANDED_ARROW = "▼" const EXPANDED_ARROW = "▼"
const COLLAPSED_ARROW = "►" const COLLAPSED_ARROW = "►"
type StatusLineManager struct { type FileChangeManager struct {
Files []*models.File Files []*models.File
Tree *models.StatusLineNode Tree *models.FileChangeNode
ShowTree bool ShowTree bool
Log *logrus.Entry Log *logrus.Entry
CollapsedPaths map[string]bool CollapsedPaths map[string]bool
} }
func NewStatusLineManager(files []*models.File, log *logrus.Entry, showTree bool) *StatusLineManager { func NewFileChangeManager(files []*models.File, log *logrus.Entry, showTree bool) *FileChangeManager {
return &StatusLineManager{ return &FileChangeManager{
Files: files, Files: files,
Log: log, Log: log,
ShowTree: showTree, ShowTree: showTree,
@ -29,17 +29,17 @@ func NewStatusLineManager(files []*models.File, log *logrus.Entry, showTree bool
} }
} }
func (m *StatusLineManager) GetItemAtIndex(index int) *models.StatusLineNode { func (m *FileChangeManager) GetItemAtIndex(index int) *models.FileChangeNode {
// need to traverse the three depth first until we get to the index. // need to traverse the three depth first until we get to the index.
return m.Tree.GetNodeAtIndex(index+1, m.CollapsedPaths) // ignoring root return m.Tree.GetNodeAtIndex(index+1, m.CollapsedPaths) // ignoring root
} }
func (m *StatusLineManager) GetIndexForPath(path string) (int, bool) { func (m *FileChangeManager) GetIndexForPath(path string) (int, bool) {
index, found := m.Tree.GetIndexForPath(path, m.CollapsedPaths) index, found := m.Tree.GetIndexForPath(path, m.CollapsedPaths)
return index - 1, found return index - 1, found
} }
func (m *StatusLineManager) GetAllItems() []*models.StatusLineNode { func (m *FileChangeManager) GetAllItems() []*models.FileChangeNode {
if m.Tree == nil { if m.Tree == nil {
return nil return nil
} }
@ -47,21 +47,21 @@ func (m *StatusLineManager) GetAllItems() []*models.StatusLineNode {
return m.Tree.Flatten(m.CollapsedPaths)[1:] // ignoring root return m.Tree.Flatten(m.CollapsedPaths)[1:] // ignoring root
} }
func (m *StatusLineManager) GetItemsLength() int { func (m *FileChangeManager) GetItemsLength() int {
return m.Tree.Size(m.CollapsedPaths) - 1 // ignoring root return m.Tree.Size(m.CollapsedPaths) - 1 // ignoring root
} }
func (m *StatusLineManager) GetAllFiles() []*models.File { func (m *FileChangeManager) GetAllFiles() []*models.File {
return m.Files return m.Files
} }
func (m *StatusLineManager) SetFiles(files []*models.File) { func (m *FileChangeManager) SetFiles(files []*models.File) {
m.Files = files m.Files = files
m.SetTree() m.SetTree()
} }
func (m *StatusLineManager) SetTree() { func (m *FileChangeManager) SetTree() {
if m.ShowTree { if m.ShowTree {
m.Tree = GetTreeFromStatusFiles(m.Files, m.Log) m.Tree = GetTreeFromStatusFiles(m.Files, m.Log)
} else { } else {
@ -69,7 +69,7 @@ func (m *StatusLineManager) SetTree() {
} }
} }
func (m *StatusLineManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string { func (m *FileChangeManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
return m.renderAux(m.Tree, "", -1, diffName, submoduleConfigs) return m.renderAux(m.Tree, "", -1, diffName, submoduleConfigs)
} }
@ -78,15 +78,15 @@ const LAST_ITEM = "└─ "
const NESTED = "│ " const NESTED = "│ "
const NOTHING = " " const NOTHING = " "
func (m *StatusLineManager) IsCollapsed(s *models.StatusLineNode) bool { func (m *FileChangeManager) IsCollapsed(s *models.FileChangeNode) bool {
return m.CollapsedPaths[s.GetPath()] return m.CollapsedPaths[s.GetPath()]
} }
func (m *StatusLineManager) ToggleCollapsed(s *models.StatusLineNode) { func (m *FileChangeManager) ToggleCollapsed(s *models.FileChangeNode) {
m.CollapsedPaths[s.GetPath()] = !m.CollapsedPaths[s.GetPath()] m.CollapsedPaths[s.GetPath()] = !m.CollapsedPaths[s.GetPath()]
} }
func (m *StatusLineManager) renderAux(s *models.StatusLineNode, prefix string, depth int, diffName string, submoduleConfigs []*models.SubmoduleConfig) []string { func (m *FileChangeManager) renderAux(s *models.FileChangeNode, prefix string, depth int, diffName string, submoduleConfigs []*models.SubmoduleConfig) []string {
isRoot := depth == -1 isRoot := depth == -1
if s == nil { if s == nil {
return []string{} return []string{}

View File

@ -10,7 +10,7 @@ import (
func TestRender(t *testing.T) { func TestRender(t *testing.T) {
scenarios := []struct { scenarios := []struct {
name string name string
root *models.StatusLineNode root *models.FileChangeNode
expected []string expected []string
}{ }{
{ {
@ -20,9 +20,9 @@ func TestRender(t *testing.T) {
}, },
{ {
name: "leaf node", name: "leaf node",
root: &models.StatusLineNode{ root: &models.FileChangeNode{
Path: "", Path: "",
Children: []*models.StatusLineNode{ Children: []*models.FileChangeNode{
{File: &models.File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"}, {File: &models.File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
}, },
}, },
@ -30,13 +30,13 @@ func TestRender(t *testing.T) {
}, },
{ {
name: "big example", name: "big example",
root: &models.StatusLineNode{ root: &models.FileChangeNode{
Path: "", Path: "",
Children: []*models.StatusLineNode{ Children: []*models.FileChangeNode{
{ {
Path: "dir1", Path: "dir1",
Collapsed: true, Collapsed: true,
Children: []*models.StatusLineNode{ Children: []*models.FileChangeNode{
{ {
File: &models.File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true}, File: &models.File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file2", Path: "file2",
@ -45,10 +45,10 @@ func TestRender(t *testing.T) {
}, },
{ {
Path: "dir2", Path: "dir2",
Children: []*models.StatusLineNode{ Children: []*models.FileChangeNode{
{ {
Path: "dir2", Path: "dir2",
Children: []*models.StatusLineNode{ Children: []*models.FileChangeNode{
{ {
File: &models.File{Name: "file3", ShortStatus: " M", HasStagedChanges: true}, File: &models.File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "file3", Path: "file3",
@ -79,7 +79,7 @@ func TestRender(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
s := s s := s
t.Run(s.name, func(t *testing.T) { t.Run(s.name, func(t *testing.T) {
mngr := &StatusLineManager{Tree: s.root} mngr := &FileChangeManager{Tree: s.root}
result := mngr.Render("", nil) result := mngr.Render("", nil)
assert.EqualValues(t, s.expected, result) assert.EqualValues(t, s.expected, result)
}) })

View File

@ -10,10 +10,10 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.StatusLineNode { func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.FileChangeNode {
root := &models.StatusLineNode{} root := &models.FileChangeNode{}
var curr *models.StatusLineNode var curr *models.FileChangeNode
for _, file := range files { for _, file := range files {
split := strings.Split(file.Name, string(os.PathSeparator)) split := strings.Split(file.Name, string(os.PathSeparator))
curr = root curr = root
@ -34,7 +34,7 @@ func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.Sta
} }
} }
newChild := &models.StatusLineNode{ newChild := &models.FileChangeNode{
Path: path, Path: path,
File: setFile, File: setFile,
} }
@ -50,10 +50,10 @@ func GetTreeFromStatusFiles(files []*models.File, log *logrus.Entry) *models.Sta
return root return root
} }
func GetFlatTreeFromStatusFiles(files []*models.File) *models.StatusLineNode { func GetFlatTreeFromStatusFiles(files []*models.File) *models.FileChangeNode {
root := &models.StatusLineNode{} root := &models.FileChangeNode{}
for _, file := range files { for _, file := range files {
root.Children = append(root.Children, &models.StatusLineNode{ root.Children = append(root.Children, &models.FileChangeNode{
Path: file.GetPath(), Path: file.GetPath(),
File: file, File: file,
}) })

View File

@ -98,7 +98,7 @@ func (gui *Gui) handleResetSubmodule(submodule *models.SubmoduleConfig) error {
} }
func (gui *Gui) fileForSubmodule(submodule *models.SubmoduleConfig) *models.File { func (gui *Gui) fileForSubmodule(submodule *models.SubmoduleConfig) *models.File {
for _, file := range gui.State.StatusLineManager.GetAllFiles() { for _, file := range gui.State.FileChangeManager.GetAllFiles() {
if file.IsSubmodule([]*models.SubmoduleConfig{submodule}) { if file.IsSubmodule([]*models.SubmoduleConfig{submodule}) {
return file return file
} }