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:
@ -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()]
|
||||
}
|
||||
|
Reference in New Issue
Block a user