2022-03-19 00:31:52 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import "github.com/jesseduffield/lazygit/pkg/gui/context/traits"
|
|
|
|
|
2023-05-27 06:14:43 +02:00
|
|
|
type ListViewModel[T any] struct {
|
2022-03-19 00:31:52 +02:00
|
|
|
*traits.ListCursor
|
|
|
|
getModel func() []T
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:14:43 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:14:43 +02:00
|
|
|
func (self *ListViewModel[T]) Len() int {
|
2022-03-19 00:31:52 +02:00
|
|
|
return len(self.getModel())
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:14:43 +02:00
|
|
|
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()]
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:14:43 +02:00
|
|
|
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)
|
|
|
|
}
|