1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-31 14:24:25 +03:00

use generics to DRY up context code

This commit is contained in:
Jesse Duffield
2022-03-19 09:31:52 +11:00
parent 4b56d428ff
commit d93fef4c61
31 changed files with 117 additions and 364 deletions

View File

@ -3,12 +3,11 @@ package context
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context/traits"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type StashContext struct {
*StashViewModel
*BasicViewModel[*models.StashEntry]
*ListContextTrait
}
@ -25,10 +24,10 @@ func NewStashContext(
c *types.HelperCommon,
) *StashContext {
viewModel := NewStashViewModel(getModel)
viewModel := NewBasicViewModel(getModel)
return &StashContext{
StashViewModel: viewModel,
BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
ViewName: "stash",
@ -71,30 +70,3 @@ func (self *StashContext) GetSelectedRefName() string {
return item.RefName()
}
type StashViewModel struct {
*traits.ListCursor
getModel func() []*models.StashEntry
}
func NewStashViewModel(getModel func() []*models.StashEntry) *StashViewModel {
self := &StashViewModel{
getModel: getModel,
}
self.ListCursor = traits.NewListCursor(self)
return self
}
func (self *StashViewModel) GetItemsLength() int {
return len(self.getModel())
}
func (self *StashViewModel) GetSelected() *models.StashEntry {
if self.GetItemsLength() == 0 {
return nil
}
return self.getModel()[self.GetSelectedLineIdx()]
}