1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package context
import (
"github.com/jesseduffield/lazygit/pkg/gui/context/traits"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
type HasID interface {
ID() string
}
type ListViewModel[T HasID] struct {
*traits.ListCursor
getModel func() []T
}
func NewListViewModel[T HasID](getModel func() []T) *ListViewModel[T] {
self := &ListViewModel[T]{
getModel: getModel,
}
self.ListCursor = traits.NewListCursor(func() int { return len(getModel()) })
return self
}
func (self *ListViewModel[T]) GetSelected() T {
if self.Len() == 0 {
return Zero[T]()
}
return self.getModel()[self.GetSelectedLineIdx()]
}
func (self *ListViewModel[T]) GetSelectedItemId() string {
if self.Len() == 0 {
return ""
}
return self.GetSelected().ID()
}
func (self *ListViewModel[T]) GetSelectedItems() ([]T, int, int) {
if self.Len() == 0 {
return nil, -1, -1
}
startIdx, endIdx := self.GetSelectionRange()
return self.getModel()[startIdx : endIdx+1], startIdx, endIdx
}
func (self *ListViewModel[T]) GetSelectedItemIds() ([]string, int, int) {
selectedItems, startIdx, endIdx := self.GetSelectedItems()
ids := lo.Map(selectedItems, func(item T, _ int) string {
return item.ID()
})
return ids, startIdx, endIdx
}
func (self *ListViewModel[T]) GetItems() []T {
return self.getModel()
}
func Zero[T any]() T {
return *new(T)
}
func (self *ListViewModel[T]) GetItem(index int) types.HasUrn {
item := self.getModel()[index]
return any(item).(types.HasUrn)
}
|