1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-18 05:17:55 +02:00
lazygit/pkg/gui/context/sub_commits_context.go

114 lines
2.6 KiB
Go
Raw Normal View History

2022-02-05 17:04:10 +11:00
package context
import (
"fmt"
2022-02-05 17:04:10 +11:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
2022-02-05 17:04:10 +11:00
)
type SubCommitsContext struct {
*SubCommitsViewModel
2022-02-05 17:04:10 +11:00
*ViewportListContextTrait
2022-03-26 14:44:30 +11:00
*DynamicTitleBuilder
2022-02-05 17:04:10 +11:00
}
var _ types.IListContext = (*SubCommitsContext)(nil)
func NewSubCommitsContext(
getModel func() []*models.Commit,
view *gocui.View,
getDisplayStrings func(startIdx int, length int) [][]string,
onFocus func(...types.OnFocusOpts) error,
onRenderToMain func(...types.OnFocusOpts) error,
onFocusLost func() error,
2022-02-06 15:54:26 +11:00
c *types.HelperCommon,
2022-02-05 17:04:10 +11:00
) *SubCommitsContext {
viewModel := &SubCommitsViewModel{
BasicViewModel: NewBasicViewModel(getModel),
refName: "",
}
2022-02-05 17:04:10 +11:00
return &SubCommitsContext{
SubCommitsViewModel: viewModel,
2022-03-26 14:44:30 +11:00
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.SubCommitsDynamicTitle),
2022-02-05 17:04:10 +11:00
ViewportListContextTrait: &ViewportListContextTrait{
ListContextTrait: &ListContextTrait{
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
ViewName: "subCommits",
2022-02-05 17:04:10 +11:00
WindowName: "branches",
Key: SUB_COMMITS_CONTEXT_KEY,
Kind: types.SIDE_CONTEXT,
Focusable: true,
Transient: true,
2022-02-05 17:04:10 +11:00
}), ContextCallbackOpts{
OnFocus: onFocus,
OnFocusLost: onFocusLost,
OnRenderToMain: onRenderToMain,
}),
list: viewModel,
viewTrait: NewViewTrait(view),
getDisplayStrings: getDisplayStrings,
c: c,
2022-03-19 09:38:49 +11:00
},
},
2022-02-05 17:04:10 +11:00
}
}
type SubCommitsViewModel struct {
// name of the ref that the sub-commits are shown for
refName string
*BasicViewModel[*models.Commit]
}
func (self *SubCommitsViewModel) SetRefName(refName string) {
self.refName = refName
}
2022-02-05 17:04:10 +11:00
func (self *SubCommitsContext) GetSelectedItemId() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.ID()
}
2022-02-13 17:54:36 +11:00
func (self *SubCommitsContext) CanRebase() bool {
return false
}
// not to be confused with the refName in the view model. This is the ref name of
// the selected commit
2022-02-13 18:26:44 +11:00
func (self *SubCommitsContext) GetSelectedRefName() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.RefName()
}
func (self *SubCommitsContext) GetCommits() []*models.Commit {
return self.getModel()
}
func (self *SubCommitsContext) Title() string {
return fmt.Sprintf(self.c.Tr.SubCommitsDynamicTitle, utils.TruncateWithEllipsis(self.refName, 50))
}
2022-03-26 14:44:30 +11:00
func (self *SubCommitsContext) GetSelectedDescription() string {
item := self.GetSelected()
if item == nil {
return ""
}
return item.Description()
}