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

39 lines
685 B
Go
Raw Normal View History

2022-03-19 00:31:52 +02:00
package context
import "github.com/jesseduffield/lazygit/pkg/gui/context/traits"
type ListViewModel[T any] struct {
2022-03-19 00:31:52 +02:00
*traits.ListCursor
getModel func() []T
}
func NewListViewModel[T any](getModel func() []T) *ListViewModel[T] {
self := &ListViewModel[T]{
2022-03-19 00:31:52 +02:00
getModel: getModel,
}
self.ListCursor = traits.NewListCursor(self)
return self
}
func (self *ListViewModel[T]) Len() int {
2022-03-19 00:31:52 +02:00
return len(self.getModel())
}
func (self *ListViewModel[T]) GetSelected() T {
2022-03-19 00:31:52 +02:00
if self.Len() == 0 {
return Zero[T]()
}
return self.getModel()[self.GetSelectedLineIdx()]
}
func (self *ListViewModel[T]) GetItems() []T {
return self.getModel()
}
2022-03-19 00:31:52 +02:00
func Zero[T any]() T {
return *new(T)
}