mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-16 11:37:01 +02:00
8edad826ca
This begins a big refactor of moving more code out of the Gui struct into contexts, controllers, and helpers. We also move some code into structs in the gui package purely for the sake of better encapsulation
115 lines
2.5 KiB
Go
115 lines
2.5 KiB
Go
package context
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
)
|
|
|
|
type SubCommitsContext struct {
|
|
*SubCommitsViewModel
|
|
*ViewportListContextTrait
|
|
*DynamicTitleBuilder
|
|
}
|
|
|
|
var (
|
|
_ types.IListContext = (*SubCommitsContext)(nil)
|
|
_ types.DiffableContext = (*SubCommitsContext)(nil)
|
|
)
|
|
|
|
func NewSubCommitsContext(
|
|
getModel func() []*models.Commit,
|
|
view *gocui.View,
|
|
getDisplayStrings func(startIdx int, length int) [][]string,
|
|
|
|
c *types.HelperCommon,
|
|
) *SubCommitsContext {
|
|
viewModel := &SubCommitsViewModel{
|
|
BasicViewModel: NewBasicViewModel(getModel),
|
|
ref: nil,
|
|
limitCommits: true,
|
|
}
|
|
|
|
return &SubCommitsContext{
|
|
SubCommitsViewModel: viewModel,
|
|
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.SubCommitsDynamicTitle),
|
|
ViewportListContextTrait: &ViewportListContextTrait{
|
|
ListContextTrait: &ListContextTrait{
|
|
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
|
|
View: view,
|
|
WindowName: "branches",
|
|
Key: SUB_COMMITS_CONTEXT_KEY,
|
|
Kind: types.SIDE_CONTEXT,
|
|
Focusable: true,
|
|
Transient: true,
|
|
}), ContextCallbackOpts{}),
|
|
list: viewModel,
|
|
getDisplayStrings: getDisplayStrings,
|
|
c: c,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
type SubCommitsViewModel struct {
|
|
// name of the ref that the sub-commits are shown for
|
|
ref types.Ref
|
|
*BasicViewModel[*models.Commit]
|
|
|
|
limitCommits bool
|
|
}
|
|
|
|
func (self *SubCommitsViewModel) SetRef(ref types.Ref) {
|
|
self.ref = ref
|
|
}
|
|
|
|
func (self *SubCommitsViewModel) GetRef() types.Ref {
|
|
return self.ref
|
|
}
|
|
|
|
func (self *SubCommitsContext) GetSelectedItemId() string {
|
|
item := self.GetSelected()
|
|
if item == nil {
|
|
return ""
|
|
}
|
|
|
|
return item.ID()
|
|
}
|
|
|
|
func (self *SubCommitsContext) CanRebase() bool {
|
|
return false
|
|
}
|
|
|
|
func (self *SubCommitsContext) GetSelectedRef() types.Ref {
|
|
commit := self.GetSelected()
|
|
if commit == nil {
|
|
return nil
|
|
}
|
|
return commit
|
|
}
|
|
|
|
func (self *SubCommitsContext) GetCommits() []*models.Commit {
|
|
return self.getModel()
|
|
}
|
|
|
|
func (self *SubCommitsContext) Title() string {
|
|
return fmt.Sprintf(self.c.Tr.SubCommitsDynamicTitle, utils.TruncateWithEllipsis(self.ref.RefName(), 50))
|
|
}
|
|
|
|
func (self *SubCommitsContext) SetLimitCommits(value bool) {
|
|
self.limitCommits = value
|
|
}
|
|
|
|
func (self *SubCommitsContext) GetLimitCommits() bool {
|
|
return self.limitCommits
|
|
}
|
|
|
|
func (self *SubCommitsContext) GetDiffTerminals() []string {
|
|
itemId := self.GetSelectedItemId()
|
|
|
|
return []string{itemId}
|
|
}
|