1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-22 05:29:44 +02:00
lazygit/pkg/gui/filetree/commit_file_tree_view_model.go

112 lines
2.5 KiB
Go
Raw Normal View History

2022-01-22 00:13:51 +11:00
package filetree
import (
"sync"
2022-01-22 00:13:51 +11:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context/traits"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-01-22 00:13:51 +11:00
"github.com/sirupsen/logrus"
)
type ICommitFileTreeViewModel interface {
ICommitFileTree
types.IListCursor
2022-01-22 00:13:51 +11:00
2022-03-26 22:18:08 +09:00
GetRef() types.Ref
SetRef(types.Ref)
GetCanRebase() bool
SetCanRebase(bool)
2022-01-22 00:13:51 +11:00
}
type CommitFileTreeViewModel struct {
sync.RWMutex
ICommitFileTree
types.IListCursor
2022-01-22 00:13:51 +11:00
2022-03-26 22:18:08 +09:00
// this is e.g. the commit for which we're viewing the files
ref types.Ref
2022-01-22 00:13:51 +11:00
// we set this to true when you're viewing the files within the checked-out branch's commits.
// If you're viewing the files of some random other branch we can't do any rebase stuff.
canRebase bool
}
var _ ICommitFileTreeViewModel = &CommitFileTreeViewModel{}
2022-01-22 00:13:51 +11:00
func NewCommitFileTreeViewModel(getFiles func() []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileTreeViewModel {
fileTree := NewCommitFileTree(getFiles, log, showTree)
listCursor := traits.NewListCursor(fileTree)
return &CommitFileTreeViewModel{
ICommitFileTree: fileTree,
IListCursor: listCursor,
2022-03-26 22:18:08 +09:00
ref: nil,
canRebase: false,
}
2022-01-22 00:13:51 +11:00
}
2022-03-26 22:18:08 +09:00
func (self *CommitFileTreeViewModel) GetRef() types.Ref {
return self.ref
2022-01-22 00:13:51 +11:00
}
2022-03-26 22:18:08 +09:00
func (self *CommitFileTreeViewModel) SetRef(ref types.Ref) {
self.ref = ref
2022-01-22 00:13:51 +11:00
}
func (self *CommitFileTreeViewModel) GetCanRebase() bool {
return self.canRebase
2022-01-22 00:13:51 +11:00
}
func (self *CommitFileTreeViewModel) SetCanRebase(canRebase bool) {
self.canRebase = canRebase
2022-01-22 00:13:51 +11:00
}
2022-03-19 09:31:52 +11:00
func (self *CommitFileTreeViewModel) GetSelected() *CommitFileNode {
if self.Len() == 0 {
2022-01-22 00:13:51 +11:00
return nil
}
2022-03-19 09:31:52 +11:00
return self.Get(self.GetSelectedLineIdx())
2022-01-22 00:13:51 +11:00
}
func (self *CommitFileTreeViewModel) GetSelectedFile() *models.CommitFile {
2022-03-19 09:31:52 +11:00
node := self.GetSelected()
if node == nil {
return nil
}
return node.File
}
func (self *CommitFileTreeViewModel) GetSelectedPath() string {
2022-03-19 09:31:52 +11:00
node := self.GetSelected()
if node == nil {
return ""
}
return node.GetPath()
}
// duplicated from file_tree_view_model.go. Generics will help here
func (self *CommitFileTreeViewModel) ToggleShowTree() {
2022-03-19 09:31:52 +11:00
selectedNode := self.GetSelected()
2022-01-22 00:13:51 +11:00
self.ICommitFileTree.ToggleShowTree()
2022-01-22 00:13:51 +11:00
if selectedNode == nil {
return
2022-01-22 00:13:51 +11:00
}
path := selectedNode.Path
2022-01-22 00:13:51 +11:00
if self.InTreeMode() {
self.ExpandToPath(path)
} else if len(selectedNode.Children) > 0 {
path = selectedNode.GetLeaves()[0].Path
}
2022-01-22 00:13:51 +11:00
index, found := self.GetIndexForPath(path)
if found {
self.SetSelectedLineIdx(index)
}
2022-01-22 00:13:51 +11:00
}