1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/context/sub_commits_context.go

113 lines
2.5 KiB
Go
Raw Normal View History

2022-02-05 08:04:10 +02:00
package context
import (
"fmt"
2022-02-05 08:04:10 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
2022-02-05 08:04:10 +02:00
)
type SubCommitsContext struct {
*SubCommitsViewModel
2022-02-05 08:04:10 +02:00
*ViewportListContextTrait
2022-03-26 05:44:30 +02:00
*DynamicTitleBuilder
2022-02-05 08:04:10 +02:00
}
var (
_ types.IListContext = (*SubCommitsContext)(nil)
_ types.DiffableContext = (*SubCommitsContext)(nil)
)
2022-02-05 08:04:10 +02:00
func NewSubCommitsContext(
getModel func() []*models.Commit,
getDisplayStrings func(startIdx int, length int) [][]string,
2022-02-06 06:54:26 +02:00
c *types.HelperCommon,
2022-02-05 08:04:10 +02:00
) *SubCommitsContext {
viewModel := &SubCommitsViewModel{
BasicViewModel: NewBasicViewModel(getModel),
ref: nil,
limitCommits: true,
}
2022-02-05 08:04:10 +02:00
return &SubCommitsContext{
SubCommitsViewModel: viewModel,
2022-03-26 05:44:30 +02:00
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.SubCommitsDynamicTitle),
2022-02-05 08:04:10 +02:00
ViewportListContextTrait: &ViewportListContextTrait{
ListContextTrait: &ListContextTrait{
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
2023-03-21 12:06:39 +02:00
View: c.Views().SubCommits,
2022-02-05 08:04:10 +02:00
WindowName: "branches",
Key: SUB_COMMITS_CONTEXT_KEY,
Kind: types.SIDE_CONTEXT,
Focusable: true,
Transient: true,
2023-03-21 12:01:58 +02:00
})),
2022-02-05 08:04:10 +02:00
list: viewModel,
getDisplayStrings: getDisplayStrings,
c: c,
2022-03-19 00:38:49 +02:00
},
},
2022-02-05 08:04:10 +02:00
}
}
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
}
2022-02-05 08:04:10 +02:00
func (self *SubCommitsContext) GetSelectedItemId() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.ID()
}
2022-02-13 08:54:36 +02:00
func (self *SubCommitsContext) CanRebase() bool {
return false
}
2022-03-26 15:18:08 +02:00
func (self *SubCommitsContext) GetSelectedRef() types.Ref {
commit := self.GetSelected()
if commit == nil {
return nil
}
return commit
2022-02-13 09:26:44 +02:00
}
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}
}