2022-02-05 08:04:10 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-03-24 13:07:30 +02:00
|
|
|
"fmt"
|
|
|
|
|
2022-02-05 08:04:10 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2022-03-24 13:07:30 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2022-02-05 08:04:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SubCommitsContext struct {
|
2022-03-24 13:07:30 +02:00
|
|
|
*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
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +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,
|
|
|
|
view *gocui.View,
|
|
|
|
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 {
|
2022-03-24 13:07:30 +02:00
|
|
|
viewModel := &SubCommitsViewModel{
|
|
|
|
BasicViewModel: NewBasicViewModel(getModel),
|
2023-02-10 14:44:15 +02:00
|
|
|
ref: nil,
|
|
|
|
limitCommits: true,
|
2022-03-24 13:07:30 +02:00
|
|
|
}
|
2022-02-05 08:04:10 +02:00
|
|
|
|
|
|
|
return &SubCommitsContext{
|
2022-03-24 13:07:30 +02:00
|
|
|
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{
|
2022-06-13 03:01:26 +02:00
|
|
|
View: view,
|
2022-02-05 08:04:10 +02:00
|
|
|
WindowName: "branches",
|
|
|
|
Key: SUB_COMMITS_CONTEXT_KEY,
|
|
|
|
Kind: types.SIDE_CONTEXT,
|
|
|
|
Focusable: true,
|
2022-03-24 13:07:30 +02:00
|
|
|
Transient: true,
|
2022-12-30 14:24:24 +02:00
|
|
|
}), ContextCallbackOpts{}),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:07:30 +02:00
|
|
|
type SubCommitsViewModel struct {
|
|
|
|
// name of the ref that the sub-commits are shown for
|
2023-02-10 14:44:15 +02:00
|
|
|
ref types.Ref
|
2022-03-24 13:07:30 +02:00
|
|
|
*BasicViewModel[*models.Commit]
|
2023-02-10 14:44:15 +02:00
|
|
|
|
|
|
|
limitCommits bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *SubCommitsViewModel) SetRef(ref types.Ref) {
|
|
|
|
self.ref = ref
|
2022-03-24 13:07:30 +02:00
|
|
|
}
|
|
|
|
|
2023-02-10 14:44:15 +02:00
|
|
|
func (self *SubCommitsViewModel) GetRef() types.Ref {
|
|
|
|
return self.ref
|
2022-03-24 13:07:30 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-05-05 14:41:44 +02:00
|
|
|
commit := self.GetSelected()
|
|
|
|
if commit == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return commit
|
2022-02-13 09:26:44 +02:00
|
|
|
}
|
2022-03-26 07:42:56 +02:00
|
|
|
|
|
|
|
func (self *SubCommitsContext) GetCommits() []*models.Commit {
|
|
|
|
return self.getModel()
|
|
|
|
}
|
2022-03-24 13:07:30 +02:00
|
|
|
|
|
|
|
func (self *SubCommitsContext) Title() string {
|
2023-02-10 14:44:15 +02:00
|
|
|
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
|
2022-03-24 13:07:30 +02:00
|
|
|
}
|
2022-12-30 14:24:24 +02:00
|
|
|
|
|
|
|
func (self *SubCommitsContext) GetDiffTerminals() []string {
|
|
|
|
itemId := self.GetSelectedItemId()
|
|
|
|
|
|
|
|
return []string{itemId}
|
|
|
|
}
|